github.com/maypok86/otter@v1.2.1/internal/core/cache_test.go (about) 1 package core 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/maypok86/otter/internal/generated/node" 8 ) 9 10 func TestCache_SetWithCost(t *testing.T) { 11 size := 10 12 c := NewCache[int, int](Config[int, int]{ 13 Capacity: size, 14 CostFunc: func(key int, value int) uint32 { 15 return uint32(key) 16 }, 17 }) 18 19 goodCost := c.policy.MaxAvailableCost() 20 badCost := goodCost + 1 21 22 added := c.Set(goodCost, 1) 23 if !added { 24 t.Fatalf("Set was dropped, even though it shouldn't have been. Max available cost: %d, actual cost: %d", 25 c.policy.MaxAvailableCost(), 26 c.costFunc(goodCost, 1), 27 ) 28 } 29 added = c.Set(badCost, 1) 30 if added { 31 t.Fatalf("Set wasn't dropped, though it should have been. Max available cost: %d, actual cost: %d", 32 c.policy.MaxAvailableCost(), 33 c.costFunc(badCost, 1), 34 ) 35 } 36 } 37 38 func TestCache_Range(t *testing.T) { 39 size := 10 40 ttl := time.Hour 41 c := NewCache[int, int](Config[int, int]{ 42 Capacity: size, 43 CostFunc: func(key int, value int) uint32 { 44 return 1 45 }, 46 TTL: &ttl, 47 }) 48 49 time.Sleep(3 * time.Second) 50 51 nm := node.NewManager[int, int](node.Config{ 52 WithExpiration: true, 53 WithCost: true, 54 }) 55 56 c.Set(1, 1) 57 c.hashmap.Set(nm.Create(2, 2, 1, 1)) 58 c.Set(3, 3) 59 aliveNodes := 2 60 iters := 0 61 c.Range(func(key, value int) bool { 62 if key != value { 63 t.Fatalf("got unexpected key/value for iteration %d: %d/%d", iters, key, value) 64 return false 65 } 66 iters++ 67 return true 68 }) 69 if iters != aliveNodes { 70 t.Fatalf("got unexpected number of iterations: %d", iters) 71 } 72 } 73 74 func TestCache_Close(t *testing.T) { 75 size := 10 76 c := NewCache[int, int](Config[int, int]{ 77 Capacity: size, 78 CostFunc: func(key int, value int) uint32 { 79 return 1 80 }, 81 }) 82 83 for i := 0; i < size; i++ { 84 c.Set(i, i) 85 } 86 87 if cacheSize := c.Size(); cacheSize != size { 88 t.Fatalf("c.Size() = %d, want = %d", cacheSize, size) 89 } 90 91 c.Close() 92 93 time.Sleep(10 * time.Millisecond) 94 95 if cacheSize := c.Size(); cacheSize != 0 { 96 t.Fatalf("c.Size() = %d, want = %d", cacheSize, 0) 97 } 98 if !c.isClosed { 99 t.Fatalf("cache should be closed") 100 } 101 102 c.Close() 103 104 if cacheSize := c.Size(); cacheSize != 0 { 105 t.Fatalf("c.Size() = %d, want = %d", cacheSize, 0) 106 } 107 if !c.isClosed { 108 t.Fatalf("cache should be closed") 109 } 110 } 111 112 func TestCache_Clear(t *testing.T) { 113 size := 10 114 c := NewCache[int, int](Config[int, int]{ 115 Capacity: size, 116 CostFunc: func(key int, value int) uint32 { 117 return 1 118 }, 119 }) 120 121 for i := 0; i < size; i++ { 122 c.Set(i, i) 123 } 124 125 if cacheSize := c.Size(); cacheSize != size { 126 t.Fatalf("c.Size() = %d, want = %d", cacheSize, size) 127 } 128 129 c.Clear() 130 131 time.Sleep(10 * time.Millisecond) 132 133 if cacheSize := c.Size(); cacheSize != 0 { 134 t.Fatalf("c.Size() = %d, want = %d", cacheSize, 0) 135 } 136 if c.isClosed { 137 t.Fatalf("cache shouldn't be closed") 138 } 139 }