| 606 | } |
| 607 | |
| 608 | func TestCacheStats(t *testing.T) { |
| 609 | t.Parallel() |
| 610 | |
| 611 | // given |
| 612 | cache, _ := New(context.Background(), Config{ |
| 613 | Shards: 8, |
| 614 | LifeWindow: time.Second, |
| 615 | MaxEntriesInWindow: 1, |
| 616 | MaxEntrySize: 256, |
| 617 | }) |
| 618 | |
| 619 | // when |
| 620 | for i := 0; i < 100; i++ { |
| 621 | cache.Set(fmt.Sprintf("key%d", i), []byte("value")) |
| 622 | } |
| 623 | |
| 624 | for i := 0; i < 10; i++ { |
| 625 | value, err := cache.Get(fmt.Sprintf("key%d", i)) |
| 626 | noError(t, err) |
| 627 | assertEqual(t, string(value), "value") |
| 628 | } |
| 629 | for i := 100; i < 110; i++ { |
| 630 | _, err := cache.Get(fmt.Sprintf("key%d", i)) |
| 631 | assertEqual(t, ErrEntryNotFound, err) |
| 632 | } |
| 633 | for i := 10; i < 20; i++ { |
| 634 | err := cache.Delete(fmt.Sprintf("key%d", i)) |
| 635 | noError(t, err) |
| 636 | } |
| 637 | for i := 110; i < 120; i++ { |
| 638 | err := cache.Delete(fmt.Sprintf("key%d", i)) |
| 639 | assertEqual(t, ErrEntryNotFound, err) |
| 640 | } |
| 641 | |
| 642 | // then |
| 643 | stats := cache.Stats() |
| 644 | assertEqual(t, stats.Hits, int64(10)) |
| 645 | assertEqual(t, stats.Misses, int64(10)) |
| 646 | assertEqual(t, stats.DelHits, int64(10)) |
| 647 | assertEqual(t, stats.DelMisses, int64(10)) |
| 648 | } |
| 649 | func TestCacheEntryStats(t *testing.T) { |
| 650 | t.Parallel() |
| 651 | |