TestCacheExpire attempts to add an entry to the cache and verifies that it was added successfully. It then makes sure that on timeout, it's removed and the associated callback is called.
(t *testing.T)
| 49 | // was added successfully. It then makes sure that on timeout, it's removed and |
| 50 | // the associated callback is called. |
| 51 | func (s) TestCacheExpire(t *testing.T) { |
| 52 | const k, v = 1, "1" |
| 53 | c := NewTimeoutCache(testCacheTimeout) |
| 54 | |
| 55 | callbackChan := make(chan struct{}) |
| 56 | c.Add(k, v, func() { close(callbackChan) }) |
| 57 | |
| 58 | if gotV, ok := c.getForTesting(k); !ok || gotV.item != v { |
| 59 | t.Fatalf("After Add(), before timeout, from cache got: %v, %v, want %v, %v", gotV.item, ok, v, true) |
| 60 | } |
| 61 | if l := c.Len(); l != 1 { |
| 62 | t.Fatalf("%d number of items in the cache, want 1", l) |
| 63 | } |
| 64 | |
| 65 | select { |
| 66 | case <-callbackChan: |
| 67 | case <-time.After(testCacheTimeout * 2): |
| 68 | t.Fatalf("timeout waiting for callback") |
| 69 | } |
| 70 | |
| 71 | if _, ok := c.getForTesting(k); ok { |
| 72 | t.Fatalf("After Add(), after timeout, from cache got: _, %v, want _, %v", ok, false) |
| 73 | } |
| 74 | if l := c.Len(); l != 0 { |
| 75 | t.Fatalf("%d number of items in the cache, want 0", l) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // TestCacheRemove attempts to remove an existing entry from the cache and |
| 80 | // verifies that the entry is removed and the associated callback is not |
nothing calls this directly
no test coverage detected