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

Method TestCacheClearWithoutCallback

internal/cache/timeoutCache_test.go:119–169  ·  view source on GitHub ↗

TestCacheClearWithoutCallback attempts to clear all entries from the cache and verifies that the associated callbacks are not invoked.

(t *testing.T)

Source from the content-addressed store, hash-verified

117// TestCacheClearWithoutCallback attempts to clear all entries from the cache
118// and verifies that the associated callbacks are not invoked.
119func (s) TestCacheClearWithoutCallback(t *testing.T) {
120 var values []string
121 const itemCount = 3
122 for i := 0; i < itemCount; i++ {
123 values = append(values, strconv.Itoa(i))
124 }
125 c := NewTimeoutCache(testCacheTimeout)
126
127 done := make(chan struct{})
128 defer close(done)
129 callbackChan := make(chan struct{}, itemCount)
130
131 for i, v := range values {
132 callbackChanTemp := make(chan struct{})
133 c.Add(i, v, func() { close(callbackChanTemp) })
134 go func() {
135 select {
136 case <-callbackChanTemp:
137 callbackChan <- struct{}{}
138 case <-done:
139 }
140 }()
141 }
142
143 for i, v := range values {
144 if got, ok := c.getForTesting(i); !ok || got.item != v {
145 t.Fatalf("After Add(), before timeout, from cache got: %v, %v, want %v, %v", got.item, ok, v, true)
146 }
147 }
148 if l := c.Len(); l != itemCount {
149 t.Fatalf("%d number of items in the cache, want %d", l, itemCount)
150 }
151
152 time.Sleep(testCacheTimeout / 2)
153 c.Clear(false)
154
155 for i := range values {
156 if _, ok := c.getForTesting(i); ok {
157 t.Fatalf("After Add(), before timeout, after Remove(), from cache got: _, %v, want _, %v", ok, false)
158 }
159 }
160 if l := c.Len(); l != 0 {
161 t.Fatalf("%d number of items in the cache, want 0", l)
162 }
163
164 select {
165 case <-callbackChan:
166 t.Fatalf("unexpected callback after Clear")
167 case <-time.After(testCacheTimeout * 2):
168 }
169}
170
171// TestCacheClearWithCallback attempts to clear all entries from the cache and
172// verifies that the associated callbacks are invoked.

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected