-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharded_cache_test.go
177 lines (140 loc) · 4.09 KB
/
sharded_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cache
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func setupShardedCache() *shardedCache {
sc := newShardedCache(2, DefaultExpiration)
return sc
}
func TestShardedCache_Set(t *testing.T) {
t.Run("Set an item in the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", NoExpiration)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, "value1", val)
})
}
func TestShardedCache_Add(t *testing.T) {
t.Run("Add an item to the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
err := sc.Add("key1", "value1", NoExpiration)
assert.NoError(t, err)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, "value1", val)
})
t.Run("Add an existing item to the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", NoExpiration)
err := sc.Add("key1", "value2", NoExpiration)
assert.Error(t, err)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, "value1", val)
})
}
func TestShardedCache_Replace(t *testing.T) {
t.Run("Replace an existing item in the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", NoExpiration)
err := sc.Replace("key1", "value2", NoExpiration)
assert.NoError(t, err)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, "value2", val)
})
t.Run("Replace a non-existing item in the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
err := sc.Replace("key1", "value1", NoExpiration)
assert.Error(t, err)
})
}
func TestShardedCache_Increment(t *testing.T) {
t.Run("Increment an integer item in the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", 10, NoExpiration)
err := sc.Increment("key1", 5)
assert.NoError(t, err)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, 15, val)
})
}
func TestShardedCache_IncrementFloat(t *testing.T) {
t.Run("Increment a float item in the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", 10.5, NoExpiration)
err := sc.IncrementFloat("key1", 2.5)
assert.NoError(t, err)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, 13.0, val)
})
}
func TestShardedCache_Decrement(t *testing.T) {
t.Run("Decrement an integer item in the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", 10, NoExpiration)
err := sc.Decrement("key1", 5)
assert.NoError(t, err)
val, found := sc.Get("key1")
assert.True(t, found)
assert.Equal(t, 5, val)
})
}
func TestShardedCache_Delete(t *testing.T) {
t.Run("Delete an item from the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", NoExpiration)
sc.Delete("key1")
_, found := sc.Get("key1")
assert.False(t, found)
})
}
func TestShardedCache_DeleteExpired(t *testing.T) {
t.Run("Delete expired items from the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", 1*time.Millisecond)
time.Sleep(2 * time.Millisecond)
sc.DeleteExpired()
_, found := sc.Get("key1")
assert.False(t, found)
})
}
func TestShardedCache_Items(t *testing.T) {
t.Run("Get all items from the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", NoExpiration)
sc.Set("key2", "value2", NoExpiration)
items := sc.Items()
foundKey1 := false
foundKey2 := false
for _, shardItems := range items {
if item, ok := shardItems["key1"]; ok {
assert.Equal(t, "value1", item.Object)
foundKey1 = true
}
if item, ok := shardItems["key2"]; ok {
assert.Equal(t, "value2", item.Object)
foundKey2 = true
}
}
assert.True(t, foundKey1)
assert.True(t, foundKey2)
})
}
func TestShardedCache_Flush(t *testing.T) {
t.Run("Flush all items from the sharded cache", func(t *testing.T) {
sc := setupShardedCache()
sc.Set("key1", "value1", NoExpiration)
sc.Set("key2", "value2", NoExpiration)
sc.Flush()
items := sc.Items()
for _, shardItems := range items {
assert.Empty(t, shardItems)
}
})
}