forked from Code-Hex/go-generics-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
123 lines (105 loc) · 2.44 KB
/
cache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cache_test
import (
"math/rand"
"sync"
"testing"
"time"
cache "github.com/gekatateam/go-generics-cache"
"github.com/gekatateam/go-generics-cache/policy/clock"
"github.com/gekatateam/go-generics-cache/policy/fifo"
"github.com/gekatateam/go-generics-cache/policy/lfu"
"github.com/gekatateam/go-generics-cache/policy/lru"
"github.com/gekatateam/go-generics-cache/policy/mru"
)
func TestMultiThreadIncr(t *testing.T) {
nc := cache.NewNumber[string, int]()
nc.Set("counter", 0)
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
_ = nc.Increment("counter", 1)
wg.Done()
}()
}
wg.Wait()
if counter, _ := nc.Get("counter"); counter != 100 {
t.Errorf("want %v but got %v", 100, counter)
}
}
func TestMultiThreadDecr(t *testing.T) {
nc := cache.NewNumber[string, int]()
nc.Set("counter", 100)
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
_ = nc.Decrement("counter", 1)
wg.Done()
}()
}
wg.Wait()
if counter, _ := nc.Get("counter"); counter != 0 {
t.Errorf("want %v but got %v", 0, counter)
}
}
func TestMultiThread(t *testing.T) {
cases := []struct {
name string
policy cache.Option[int, int]
}{
{
name: "LRU",
policy: cache.AsLRU[int, int](lru.WithCapacity(10)),
},
{
name: "MRU",
policy: cache.AsMRU[int, int](mru.WithCapacity(10)),
},
{
name: "FIFO",
policy: cache.AsFIFO[int, int](fifo.WithCapacity(10)),
},
{
name: "Clock",
policy: cache.AsClock[int, int](clock.WithCapacity(10)),
},
{
name: "LFU",
policy: cache.AsLFU[int, int](lfu.WithCapacity(10)),
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
c := cache.New(tc.policy)
var wg sync.WaitGroup
for i := int64(0); i < 100; i++ {
wg.Add(1)
go func(i int64) {
defer wg.Done()
m := rand.New(rand.NewSource(i))
for n := 0; n < 100; n++ {
key := m.Intn(100000)
c.Set(key, m.Intn(100000))
c.Get(key)
}
}(i)
}
wg.Wait()
})
}
}
func TestCallJanitor(t *testing.T) {
c := cache.New(
cache.WithJanitorInterval[string, int](100 * time.Millisecond),
)
c.Set("1", 10, cache.WithExpiration(10*time.Millisecond))
c.Set("2", 20, cache.WithExpiration(20*time.Millisecond))
c.Set("3", 30, cache.WithExpiration(30*time.Millisecond))
<-time.After(300 * time.Millisecond)
keys := c.Keys()
if len(keys) != 0 {
t.Errorf("want items is empty but got %d", len(keys))
}
}