MCPcopy
hub / github.com/coocood/freecache / TestGetWithExpirationAndBuf

Function TestGetWithExpirationAndBuf

cache_test.go:353–387  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

351}
352
353func TestGetWithExpirationAndBuf(t *testing.T) {
354 cache := NewCache(1024)
355 key := []byte("abcd")
356 val := []byte("efgh")
357 err := cache.Set(key, val, 2)
358 if err != nil {
359 t.Error("err should be nil", err.Error())
360 }
361
362 buf := make([]byte, 0, len(val))
363 res, expiry, err := cache.GetWithExpirationAndBuf(key, buf)
364 var expireTime time.Time
365 startTime := time.Now()
366 for {
367 _, _, err := cache.GetWithExpirationAndBuf(key, buf)
368 expireTime = time.Now()
369 if err != nil {
370 break
371 }
372 if time.Now().Unix() > int64(expiry+1) {
373 break
374 }
375 time.Sleep(1 * time.Millisecond)
376 }
377 if time.Second > expireTime.Sub(startTime) || 3*time.Second < expireTime.Sub(startTime) {
378 t.Error("Cache should expire within a second of the expire time")
379 }
380
381 if err != nil {
382 t.Error("err should be nil", err.Error())
383 }
384 if !bytes.Equal(val, res) {
385 t.Fatalf("%s should be the same as %s but isn't", res, val)
386 }
387}
388
389func TestExpire(t *testing.T) {
390 cache := NewCache(1024)

Callers

nothing calls this directly

Calls 4

SetMethod · 0.95
NewCacheFunction · 0.85
NowMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…