(t *testing.T)
| 351 | } |
| 352 | |
| 353 | func 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 | |
| 389 | func TestExpire(t *testing.T) { |
| 390 | cache := NewCache(1024) |
nothing calls this directly
no test coverage detected
searching dependent graphs…