MCPcopy
hub / github.com/grpc/grpc-go / TestCacheExpire

Method TestCacheExpire

internal/cache/timeoutCache_test.go:51–77  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

49// was added successfully. It then makes sure that on timeout, it's removed and
50// the associated callback is called.
51func (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

Callers

nothing calls this directly

Calls 5

AddMethod · 0.95
getForTestingMethod · 0.95
LenMethod · 0.95
NewTimeoutCacheFunction · 0.85
FatalfMethod · 0.65

Tested by

no test coverage detected