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

Method TestCacheClearWithCallback

internal/cache/timeoutCache_test.go:173–230  ·  view source on GitHub ↗

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

(t *testing.T)

Source from the content-addressed store, hash-verified

171// TestCacheClearWithCallback attempts to clear all entries from the cache and
172// verifies that the associated callbacks are invoked.
173func (s) TestCacheClearWithCallback(t *testing.T) {
174 var values []string
175 const itemCount = 3
176 for i := 0; i < itemCount; i++ {
177 values = append(values, strconv.Itoa(i))
178 }
179 c := NewTimeoutCache(time.Hour)
180
181 testDone := make(chan struct{})
182 defer close(testDone)
183
184 var wg sync.WaitGroup
185 wg.Add(itemCount)
186 for i, v := range values {
187 callbackChanTemp := make(chan struct{})
188 c.Add(i, v, func() { close(callbackChanTemp) })
189 go func() {
190 defer wg.Done()
191 select {
192 case <-callbackChanTemp:
193 case <-testDone:
194 }
195 }()
196 }
197
198 allGoroutineDone := make(chan struct{}, itemCount)
199 go func() {
200 wg.Wait()
201 close(allGoroutineDone)
202 }()
203
204 for i, v := range values {
205 if got, ok := c.getForTesting(i); !ok || got.item != v {
206 t.Fatalf("After Add(), before timeout, from cache got: %v, %v, want %v, %v", got.item, ok, v, true)
207 }
208 }
209 if l := c.Len(); l != itemCount {
210 t.Fatalf("%d number of items in the cache, want %d", l, itemCount)
211 }
212
213 time.Sleep(testCacheTimeout / 2)
214 c.Clear(true)
215
216 for i := range values {
217 if _, ok := c.getForTesting(i); ok {
218 t.Fatalf("After Add(), before timeout, after Remove(), from cache got: _, %v, want _, %v", ok, false)
219 }
220 }
221 if l := c.Len(); l != 0 {
222 t.Fatalf("%d number of items in the cache, want 0", l)
223 }
224
225 select {
226 case <-allGoroutineDone:
227 case <-time.After(testCacheTimeout * 2):
228 t.Fatalf("timeout waiting for all callbacks")
229 }
230}

Callers

nothing calls this directly

Calls 9

AddMethod · 0.95
getForTestingMethod · 0.95
LenMethod · 0.95
ClearMethod · 0.95
NewTimeoutCacheFunction · 0.85
WaitMethod · 0.80
AddMethod · 0.65
FatalfMethod · 0.65
DoneMethod · 0.45

Tested by

no test coverage detected