-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvstore_test.go
201 lines (196 loc) · 3.81 KB
/
kvstore_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package doorman
import (
"sync"
"testing"
"time"
"github.com/benbjohnson/clock"
"go.uber.org/zap"
)
func Test_memstore_TTL(t *testing.T) {
tests := []struct {
name string
key string
getkey string
value string
duration time.Duration
putErr bool
getErr bool
deleted bool
skip time.Duration
}{
{
name: "simple put",
key: "mykey",
value: "myvalue",
duration: 1 * time.Hour,
putErr: false,
getErr: false,
skip: 0,
},
{
name: "simple put with timeout",
key: "mykey",
value: "",
duration: 1 * time.Hour,
putErr: false,
getErr: true,
deleted: true,
skip: 2 * time.Hour,
},
{
name: "simple put wrong get-key",
key: "mykey",
getkey: "mywrongkey",
value: "",
duration: 1 * time.Hour,
putErr: false,
getErr: true,
skip: 2 * time.Hour,
},
}
lg := zap.NewNop()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := clock.NewMock()
ms := newMemstore(mock, StoreSettings{})
err := ms.PutTTL(lg, tt.key, tt.value, tt.duration)
if (err != nil) != tt.putErr {
t.Errorf("memstore.Put() returned error: %v", err)
}
if tt.skip > 0 {
mock.Add(tt.skip)
}
getkey := tt.key
if tt.getkey != "" {
getkey = tt.getkey
}
v, err := ms.GetTTL(lg, getkey)
if (err != nil) != tt.getErr {
t.Errorf("memstore.Get() returned error: %v", err)
}
if v != tt.value {
t.Errorf("memstore.Get() returned %v, want %v", v, tt.value)
}
if tt.deleted {
if _, has := ms.data[getkey]; has {
t.Errorf("memstore.Get() should have deleted the key: %v", getkey)
}
}
})
}
}
func Test_memstore_GetPut(t *testing.T) {
tests := []struct {
name string
key string
getkey string
value string
putErr bool
getErr bool
delete bool
}{
{
name: "simple put",
key: "mykey",
value: "myvalue",
putErr: false,
getErr: false,
},
{
name: "deleted key",
key: "mykey",
value: "myvalue",
putErr: false,
getErr: false,
delete: true,
},
{
name: "simple put wrong get-key",
key: "mykey",
getkey: "mywrongkey",
value: "",
putErr: false,
getErr: true,
},
}
lg := zap.NewNop()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := clock.NewMock()
ms := newMemstore(mock, StoreSettings{})
err := ms.Put(lg, tt.key, tt.value)
if (err != nil) != tt.putErr {
t.Errorf("memstore.Put() returned error: %v", err)
}
getkey := tt.key
if tt.getkey != "" {
getkey = tt.getkey
}
v, err := ms.Get(lg, getkey)
if (err != nil) != tt.getErr {
t.Errorf("memstore.Get() returned error: %v", err)
}
if v != tt.value {
t.Errorf("memstore.Get() returned %v, want %v", v, tt.value)
}
if tt.delete {
ms.Del(lg, getkey)
if has := ms.Has(lg, getkey); has {
t.Errorf("memstore.Del() should have deleted the key: %v", getkey)
}
}
})
}
}
func Test_YesNoWaiter(t *testing.T) {
tests := []struct {
name string
cmd string
yes bool
}{
{
name: "say yes",
cmd: "yes",
yes: true,
},
{
name: "say YES",
cmd: "YES",
yes: true,
},
{
name: "say no",
cmd: "no",
yes: false,
},
{
name: "say random",
cmd: "random",
yes: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := newYesNo()
cmd := yesno(tt.cmd)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
yn, err := w.WaitFor()
if err != nil {
t.Errorf("wait returned error: %v", err)
return
}
if *yn != cmd {
t.Errorf("wait returned %v, want %v", yn, cmd)
}
if yn.Yes() != tt.yes {
t.Errorf("yes returned %v, want %v", yn.Yes(), tt.yes)
}
}()
w.Say(cmd)
wg.Wait()
})
}
}