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

Function TestGetWithExpiration

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

Source from the content-addressed store, hash-verified

203}
204
205func TestGetWithExpiration(t *testing.T) {
206 cache := NewCache(1024)
207 key := []byte("abcd")
208 val := []byte("efgh")
209 err := cache.Set(key, val, 2)
210 if err != nil {
211 t.Error("err should be nil", err.Error())
212 }
213
214 res, expiry, err := cache.GetWithExpiration(key)
215 var expireTime time.Time
216 startTime := time.Now()
217 for {
218 _, _, err := cache.GetWithExpiration(key)
219 expireTime = time.Now()
220 if err != nil {
221 break
222 }
223 if time.Now().Unix() > int64(expiry+1) {
224 break
225 }
226 time.Sleep(1 * time.Millisecond)
227 }
228 if time.Second > expireTime.Sub(startTime) || 3*time.Second < expireTime.Sub(startTime) {
229 t.Error("Cache should expire within a second of the expire time")
230 }
231
232 if err != nil {
233 t.Error("err should be nil", err.Error())
234 }
235 if !bytes.Equal(val, res) {
236 t.Fatalf("%s should be the same as %s but isn't", res, val)
237 }
238}
239
240func TestPeekWithExpiration(t *testing.T) {
241 now := uint32(1000)

Callers

nothing calls this directly

Calls 4

SetMethod · 0.95
GetWithExpirationMethod · 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…