-
Notifications
You must be signed in to change notification settings - Fork 0
/
multilevel_test.go
121 lines (99 loc) · 2.87 KB
/
multilevel_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
package cache
import (
"context"
"errors"
"sync"
"testing"
"time"
)
func TestMultiLevel(t *testing.T) {
t.Run("not found", func(t *testing.T) {
multiLvl := newMultiLevel(t)
val, err := multiLvl.Get(context.Background(), "one")
if !errors.Is(err, ErrNotGet) {
t.Errorf("could not match not found error. got: %s", err)
}
if val != "" {
t.Errorf("could not match default value, got: %s", val)
}
})
t.Run("find set value", func(t *testing.T) {
multiLvl := newMultiLevel(t)
const k = "key"
want := "value"
if err := multiLvl.Set(context.Background(), k, want, NoExpiration); err != nil {
t.Fatalf("could not set item: %s", err)
}
got, err := multiLvl.Get(context.Background(), k)
if err != nil {
t.Fatalf("could not get item: %s", err)
}
if got != want {
t.Errorf("could not match value, got: %s. want:%s", got, want)
}
})
t.Run("delete set value", func(t *testing.T) {
multiLvl := newMultiLevel(t)
const k = "key"
want := "value"
if err := multiLvl.Set(context.Background(), k, want, NoExpiration); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := multiLvl.Delete(context.Background(), k); err != nil {
t.Fatalf("could not delete item: %s", err)
}
val, err := multiLvl.Get(context.Background(), k)
if !errors.Is(err, ErrNotGet) {
t.Errorf("could not match not found error. got: %s", err)
}
if val != "" {
t.Errorf("could not match default value, got: %s", val)
}
})
t.Run("concurrent set, get, and delete", func(t *testing.T) {
multiLvl := newMultiLevel(t)
const c = 100
wg := sync.WaitGroup{}
wg.Add(c)
for i := 0; i < 100; i++ {
go func() {
defer wg.Done()
_, _ = multiLvl.Get(context.Background(), "one")
_ = multiLvl.Set(context.Background(), "two", "two", time.Second)
_ = multiLvl.Delete(context.Background(), "two")
}()
}
wg.Wait()
})
t.Run("get expired value", func(t *testing.T) {
multiLvl := newMultiLevel(t)
const k = "key"
want := "value"
if err := multiLvl.Set(context.Background(), k, want, time.Millisecond); err != nil {
t.Fatalf("could not set item: %s", err)
}
time.Sleep(time.Millisecond)
val, err := multiLvl.Get(context.Background(), k)
if !errors.Is(err, ErrNotGet) {
t.Errorf("could not match not found error. got: %s", err)
}
if val != "" {
t.Errorf("could not match default value, got: %s", val)
}
})
}
func newMultiLevel(t *testing.T) *MultiLevel[string, string] {
t.Helper()
inmem1 := NewInMemory[string, string](time.Second, 5)
inmem2 := NewInMemory[string, string](time.Second, 5)
multiLlv := NewMultiLevel[string, string](inmem1, 10*time.Second, inmem2, 10*time.Second)
t.Cleanup(func() {
if err := inmem1.Close(); err != nil {
t.Errorf("could not close inmem1 level: %s", err)
}
if err := inmem2.Close(); err != nil {
t.Errorf("could not close inmem2 level: %s", err)
}
})
return multiLlv
}