|
| 1 | +package ttlset_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ethersphere/swarm/pss/internal/ttlset" |
| 8 | + "github.com/tilinna/clock" |
| 9 | +) |
| 10 | + |
| 11 | +func TestTTLSet(t *testing.T) { |
| 12 | + var err error |
| 13 | + |
| 14 | + testClock := clock.NewMock(time.Unix(0, 0)) |
| 15 | + |
| 16 | + testEntryTTL := 10 * time.Second |
| 17 | + testSet := ttlset.New(&ttlset.Config{ |
| 18 | + EntryTTL: testEntryTTL, |
| 19 | + Clock: testClock, |
| 20 | + }) |
| 21 | + |
| 22 | + key1 := "some key" |
| 23 | + key2 := "some other key" |
| 24 | + |
| 25 | + // check adding a key to the set |
| 26 | + err = testSet.Add(key1) |
| 27 | + if err != nil { |
| 28 | + t.Fatal((err)) |
| 29 | + } |
| 30 | + |
| 31 | + // check if the key is now there: |
| 32 | + hasKey := testSet.Has(key1) |
| 33 | + if !(hasKey == true) { |
| 34 | + t.Fatal("key1 should've been in the set, but Has() returned false") |
| 35 | + } |
| 36 | + |
| 37 | + // check if Has() returns false when asked about a key that was never added: |
| 38 | + hasKey = testSet.Has("some made up key") |
| 39 | + if !(hasKey == false) { |
| 40 | + t.Fatal("Has() should have returned false when presented with a key that was never added") |
| 41 | + } |
| 42 | + |
| 43 | + // Let some time pass, but not enough to have the key expire: |
| 44 | + testClock.Add(testEntryTTL / 2) |
| 45 | + |
| 46 | + // check if the key is still there: |
| 47 | + hasKey = testSet.Has(key1) |
| 48 | + if !(hasKey == true) { |
| 49 | + t.Fatal("key1 should've been in the set, but Has() returned false") |
| 50 | + } |
| 51 | + |
| 52 | + // Let some time pass well beyond the expiry time, so key1 expires: |
| 53 | + testClock.Add(testEntryTTL * 2) |
| 54 | + |
| 55 | + // Add another key to the set: |
| 56 | + err = testSet.Add(key2) |
| 57 | + if err != nil { |
| 58 | + t.Fatal((err)) |
| 59 | + } |
| 60 | + |
| 61 | + hasKey = testSet.Has(key1) |
| 62 | + if !(hasKey == false) { |
| 63 | + t.Fatal("key1 should've been removed from the set, but Has() returned true") |
| 64 | + } |
| 65 | + |
| 66 | + hasKey = testSet.Has(key2) |
| 67 | + if !(hasKey == true) { |
| 68 | + t.Fatal("key should remain in the set, but Has() returned false") |
| 69 | + } |
| 70 | + |
| 71 | + // Let some time pass well beyond key2's expiry time, so key2 expires: |
| 72 | + testClock.Add(testEntryTTL * 2) |
| 73 | + |
| 74 | + hasKey = testSet.Has(key2) |
| 75 | + if !(hasKey == false) { |
| 76 | + t.Fatal("key2 should have been wiped, but Has() returned true") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestGC(t *testing.T) { |
| 81 | + var err error |
| 82 | + |
| 83 | + testClock := clock.NewMock(time.Unix(0, 0)) |
| 84 | + |
| 85 | + testEntryTTL := 10 * time.Second |
| 86 | + testSet := ttlset.New(&ttlset.Config{ |
| 87 | + EntryTTL: testEntryTTL, |
| 88 | + Clock: testClock, |
| 89 | + }) |
| 90 | + |
| 91 | + key1 := "some key" |
| 92 | + key2 := "some later key" |
| 93 | + |
| 94 | + // check adding a message to the cache |
| 95 | + err = testSet.Add(key1) |
| 96 | + if err != nil { |
| 97 | + t.Fatal((err)) |
| 98 | + } |
| 99 | + |
| 100 | + // move the clock 2 seconds |
| 101 | + testClock.Add(2 * time.Second) |
| 102 | + |
| 103 | + // add a second key which will have a later expiration time |
| 104 | + err = testSet.Add(key2) |
| 105 | + if err != nil { |
| 106 | + t.Fatal((err)) |
| 107 | + } |
| 108 | + |
| 109 | + count := testSet.Count() |
| 110 | + if !(count == 2) { |
| 111 | + t.Fatal("Expected the set to contain 2 keys") |
| 112 | + } |
| 113 | + |
| 114 | + testSet.GC() // attempt a cleanup. This cleanup should not affect any of the two keys, since they are not expired. |
| 115 | + |
| 116 | + count = testSet.Count() |
| 117 | + if !(count == 2) { |
| 118 | + t.Fatal("Expected the set to still contain 2 keys") |
| 119 | + } |
| 120 | + |
| 121 | + //Now, move the clock forward 9 seconds. This will expire key1 but still keep key2 |
| 122 | + testClock.Add(9 * time.Second) |
| 123 | + testSet.GC() // invoke the internal cleaning function, which should wipe only key1 |
| 124 | + count = testSet.Count() |
| 125 | + if !(count == 1) { |
| 126 | + t.Fatal("Expected the set to now have only 1 key") |
| 127 | + } |
| 128 | + |
| 129 | + //Verify if key1 was wiped but key2 persists: |
| 130 | + hasKey := testSet.Has(key1) |
| 131 | + if !(hasKey == false) { |
| 132 | + t.Fatal("Expected the set to have removed key1") |
| 133 | + } |
| 134 | + |
| 135 | + hasKey = testSet.Has(key2) |
| 136 | + if !(hasKey == true) { |
| 137 | + t.Fatal("Expected the set to still contain key2") |
| 138 | + } |
| 139 | + |
| 140 | + //Now, move the clock some more time. This will wipe key2 |
| 141 | + testClock.Add(7 * time.Second) |
| 142 | + testSet.GC() // invoke the internal cleaning function, which should wipe only key1 |
| 143 | + |
| 144 | + count = testSet.Count() |
| 145 | + // verify the map is now empty |
| 146 | + if !(count == 0) { |
| 147 | + t.Fatal("Expected the set to be empty") |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments