| 116 | } |
| 117 | |
| 118 | func TestTTLList(t *testing.T) { |
| 119 | testObjs := []testStoreObject{ |
| 120 | {id: "foo", val: "bar"}, |
| 121 | {id: "foo1", val: "bar1"}, |
| 122 | {id: "foo2", val: "bar2"}, |
| 123 | } |
| 124 | expireKeys := sets.NewString(testObjs[0].id, testObjs[2].id) |
| 125 | deleteChan := make(chan string, len(testObjs)) |
| 126 | defer close(deleteChan) |
| 127 | |
| 128 | ttlStore := NewFakeExpirationStore( |
| 129 | testStoreKeyFunc, deleteChan, |
| 130 | &FakeExpirationPolicy{ |
| 131 | NeverExpire: sets.NewString(testObjs[1].id), |
| 132 | RetrieveKeyFunc: func(obj interface{}) (string, error) { |
| 133 | return obj.(*TimestampedEntry).Obj.(testStoreObject).id, nil |
| 134 | }, |
| 135 | }, |
| 136 | clock.RealClock{}, |
| 137 | ) |
| 138 | for _, obj := range testObjs { |
| 139 | err := ttlStore.Add(obj) |
| 140 | if err != nil { |
| 141 | t.Errorf("Unable to add obj %#v", obj) |
| 142 | } |
| 143 | } |
| 144 | listObjs := ttlStore.List() |
| 145 | if len(listObjs) != 1 || !reflect.DeepEqual(listObjs[0], testObjs[1]) { |
| 146 | t.Errorf("List returned unexpected results %#v", listObjs) |
| 147 | } |
| 148 | |
| 149 | // Make sure all our deletes come through in an acceptable rate (1/100ms) |
| 150 | for expireKeys.Len() != 0 { |
| 151 | select { |
| 152 | case delKey := <-deleteChan: |
| 153 | if !expireKeys.Has(delKey) { |
| 154 | t.Errorf("Unexpected delete for key %s", delKey) |
| 155 | } |
| 156 | expireKeys.Delete(delKey) |
| 157 | case <-time.After(wait.ForeverTestTimeout): |
| 158 | t.Errorf("Unexpected timeout waiting on delete") |
| 159 | return |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestTTLPolicy(t *testing.T) { |
| 165 | fakeTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) |