-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
59 lines (50 loc) · 870 Bytes
/
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
package fastcache
import (
"errors"
"fmt"
"sync"
"testing"
)
func TestCache(t *testing.T) {
c, err := NewCache(64*MB, &Config{
MemoryType: GO,
})
if err != nil {
panic(err)
}
var wg sync.WaitGroup
n := 1024
wg.Add(n)
for i := 0; i < n; i++ {
i := i
go func() {
defer wg.Done()
key := []byte(fmt.Sprintf("key_%d", i))
value := key
err = c.Set(key, value)
if err != nil {
panic(err)
}
exists := c.Has(key)
if !exists {
panic("key must exists")
}
v, err := c.Get(key)
if err != nil {
panic(err)
}
if string(v) != string(key) {
panic(fmt.Errorf("get key: %s value: %s not equals", key, v))
}
err = c.Delete(key)
if err != nil {
panic(err)
}
_, err = c.Get(key)
if err == nil || !errors.Is(err, ErrNotFound) {
panic("expect ErrNotFound")
}
}()
}
wg.Wait()
}