| 1 | package gcache |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestARCGet(t *testing.T) { |
| 10 | size := 1000 |
| 11 | gc := buildTestCache(t, TYPE_ARC, size) |
| 12 | testSetCache(t, gc, size) |
| 13 | testGetCache(t, gc, size) |
| 14 | } |
| 15 | |
| 16 | func TestLoadingARCGet(t *testing.T) { |
| 17 | size := 1000 |
| 18 | numbers := 1000 |
| 19 | testGetCache(t, buildTestLoadingCache(t, TYPE_ARC, size, loader), numbers) |
| 20 | } |
| 21 | |
| 22 | func TestARCLength(t *testing.T) { |
| 23 | gc := buildTestLoadingCacheWithExpiration(t, TYPE_ARC, 2, time.Millisecond) |
| 24 | gc.Get("test1") |
| 25 | gc.Get("test2") |
| 26 | gc.Get("test3") |
| 27 | length := gc.Len(true) |
| 28 | expectedLength := 2 |
| 29 | if length != expectedLength { |
| 30 | t.Errorf("Expected length is %v, not %v", expectedLength, length) |
| 31 | } |
| 32 | time.Sleep(time.Millisecond) |
| 33 | gc.Get("test4") |
| 34 | length = gc.Len(true) |
| 35 | expectedLength = 1 |
| 36 | if length != expectedLength { |
| 37 | t.Errorf("Expected length is %v, not %v", expectedLength, length) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestARCEvictItem(t *testing.T) { |
| 42 | cacheSize := 10 |
| 43 | numbers := cacheSize + 1 |
| 44 | gc := buildTestLoadingCache(t, TYPE_ARC, cacheSize, loader) |
| 45 | |
| 46 | for i := 0; i < numbers; i++ { |
| 47 | _, err := gc.Get(fmt.Sprintf("Key-%d", i)) |
| 48 | if err != nil { |
| 49 | t.Errorf("Unexpected error: %v", err) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestARCPurgeCache(t *testing.T) { |
| 55 | cacheSize := 10 |
| 56 | purgeCount := 0 |
| 57 | gc := New(cacheSize). |
| 58 | ARC(). |
| 59 | LoaderFunc(loader). |
| 60 | PurgeVisitorFunc(func(k, v interface{}) { |
| 61 | purgeCount++ |
| 62 | }). |
| 63 | Build() |
| 64 | |
| 65 | for i := 0; i < cacheSize; i++ { |
| 66 | _, err := gc.Get(fmt.Sprintf("Key-%d", i)) |
| 67 | if err != nil { |
| 68 | t.Errorf("Unexpected error: %v", err) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | gc.Purge() |
| 73 | |
| 74 | if purgeCount != cacheSize { |
| 75 | t.Errorf("failed to purge everything") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestARCGetIFPresent(t *testing.T) { |
| 80 | testGetIFPresent(t, TYPE_ARC) |
| 81 | } |
| 82 | |
| 83 | func TestARCHas(t *testing.T) { |
| 84 | gc := buildTestLoadingCacheWithExpiration(t, TYPE_ARC, 2, 10*time.Millisecond) |
| 85 | |
| 86 | for i := 0; i < 10; i++ { |
| 87 | t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 88 | gc.Get("test1") |
| 89 | gc.Get("test2") |
| 90 | |
| 91 | if gc.Has("test0") { |
| 92 | t.Fatal("should not have test0") |
| 93 | } |
| 94 | if !gc.Has("test1") { |
| 95 | t.Fatal("should have test1") |
| 96 | } |
| 97 | if !gc.Has("test2") { |
| 98 | t.Fatal("should have test2") |
| 99 | } |
| 100 | |
| 101 | time.Sleep(20 * time.Millisecond) |
| 102 | |
| 103 | if gc.Has("test0") { |
| 104 | t.Fatal("should not have test0") |
| 105 | } |
| 106 | if gc.Has("test1") { |
| 107 | t.Fatal("should not have test1") |
| 108 | } |
| 109 | if gc.Has("test2") { |
| 110 | t.Fatal("should not have test2") |
| 111 | } |
| 112 | }) |
| 113 | } |
| 114 | } |