-
Notifications
You must be signed in to change notification settings - Fork 4
/
cache_test.go
104 lines (87 loc) · 1.91 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
package main
import (
"log"
"sort"
"strconv"
"sync"
"testing"
"gopkg.in/go-playground/assert.v1"
)
type testItem struct {
Foo string
Id int
}
func TestCache(t *testing.T) {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
c, err := newCache()
if err != nil {
t.Fatal(err)
}
t.Run("Internal tests", func(t *testing.T) {
t.Run("Set a key", func(t *testing.T) {
var wg sync.WaitGroup
for ix := 0; ix < 4; ix++ {
wg.Add(1)
go func(val int) {
defer wg.Done()
if err := c.Set("table", strconv.Itoa(val), val); err != nil {
t.Fatal(err)
}
}(ix)
}
wg.Wait()
// Should run the next few gets in Parallel
t.Parallel()
for ix := 0; ix < 4; ix++ {
t.Run("Get a key", func(t *testing.T) {
var val int
r, err := c.Get("table", strconv.Itoa(ix))
if err != nil {
t.Fatal(err)
}
if !r.Exists() {
t.Fatal("Value", ix, "is missing")
}
if err := r.Unmarshal(&val); err != nil {
t.Fatal(err)
} else {
assert.Equal(t, ix, val)
}
})
}
})
})
t.Run("Insert a random struct", func(t *testing.T) {
if err := c.Set("test", "uid1", &testItem{Foo: "foo", Id: 1}); err != nil {
t.Fatal(err)
}
t.Run("Fetch the struct", func(t *testing.T) {
var ret testItem
res, err := c.Get("test", "uid1")
if err != nil {
t.Fatal(err)
}
if err := res.Unmarshal(&ret); err != nil {
t.Fatal(err)
} else {
assert.Equal(t, ret.Foo, "foo")
assert.Equal(t, ret.Id, 1)
}
})
})
t.Run("List values for table", func(t *testing.T) {
c.Set("test1", "uid1", nil)
c.Set("test1", "uid2", nil)
c.Set("test2", "uid3", nil)
items, err := c.List("test1")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(items), 2)
expected := []string{"uid1", "uid2"}
sort.Slice(items, func(i, j int) bool {
return items[i] < items[j]
})
assert.Equal(t, items, expected)
})
}