-
Notifications
You must be signed in to change notification settings - Fork 8
/
map_store_test.go
152 lines (126 loc) · 2.72 KB
/
map_store_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
package throttle
import (
"encoding/json"
"math/rand"
"strconv"
"sync"
"testing"
"time"
)
func seedRandom() {
rand.Seed(time.Now().UnixNano())
}
func sleepRandom() {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
}
func TestSet(t *testing.T) {
store := NewMapStore(accessCount{})
store.Set("KEY", []byte("4"))
value, err := store.Get("KEY")
if err != nil {
t.Errorf(err.Error())
}
expectSame(t, string(value), "4")
}
func TestGet(t *testing.T) {
seedRandom()
store := NewMapStore(accessCount{})
wg := &sync.WaitGroup{}
var values []string
store.Set("KEY", []byte(strconv.FormatInt(int64(50), 10)))
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
sleepRandom()
value, err := store.Get("KEY")
if err != nil {
t.Errorf(err.Error())
}
values = append(values, string(value))
wg.Done()
}()
}
wg.Wait()
for _, val := range values {
expectSame(t, val, "50")
}
}
func TestRead(t *testing.T) {
store := NewMapStore(accessCount{})
wg := &sync.WaitGroup{}
var values []bool
marshalled, err := json.Marshal(accessCount{
64,
time.Now(),
10 * time.Millisecond,
})
if err != nil {
t.Errorf(err.Error())
}
store.Set("KEY", marshalled)
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
value, err := store.Read("KEY")
time.Sleep(10 * time.Millisecond)
if err != nil {
t.Errorf(err.Error())
}
values = append(values, value.IsFresh())
wg.Done()
}()
}
wg.Wait()
for _, val := range values {
expectSame(t, val, false)
}
}
func TestDelete(t *testing.T) {
store := NewMapStore(accessCount{})
wg := &sync.WaitGroup{}
for i := 0; i < 5; i++ {
wg.Add(1)
go func(k int) {
store.Set("KEY", []byte(strconv.FormatInt(int64(k), 10)))
store.Delete("KEY")
wg.Done()
}(i)
}
wg.Wait()
value, err := store.Get("KEY")
if err == nil {
t.Errorf("Expected no key to exist, but did: %v", value)
}
expectSame(t, err.Error(), "Throttle Map Store Error: Key KEY does not exist")
}
func TestCleaning(t *testing.T) {
store := NewMapStore(accessCount{}, &MapStoreOptions{
5 * time.Millisecond,
})
marshalled, err := json.Marshal(accessCount{
64,
time.Now(),
10 * time.Millisecond,
})
if err != nil {
t.Errorf(err.Error())
}
wg := &sync.WaitGroup{}
for i := 0; i < 5; i++ {
wg.Add(1)
go func(k int) {
store.Set("KEY"+strconv.FormatInt(int64(k), 10), marshalled)
wg.Done()
}(i)
}
wg.Wait()
time.Sleep(11 * time.Millisecond)
for i := 0; i < 5; i++ {
value, err := store.Get("KEY" + strconv.FormatInt(int64(i), 10))
if err == nil {
t.Errorf("Expected no key to exist, but did: %v", value)
} else {
expectSame(t, err.Error(), "Throttle Map Store Error: Key KEY"+strconv.FormatInt(int64(i), 10)+" does not exist")
}
}
}