-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
73 lines (57 loc) · 1.56 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
package inmemory
import (
"testing"
"time"
)
func TestNew(t *testing.T) {
dataStore := New()
if dataStore == nil {
t.Fatal("Couldn't create database.")
}
}
func TestTTLD(t *testing.T) {
client := setupTestClient()
// fill data store
testData(client, 200)
// check size
client.Exec("size", []string{})
if client.reply != "200" {
t.Error("Didn't create dummy data")
t.Errorf("Expected reply: \"200\", got: \"%s\"", client.reply)
}
if client.err != nil {
t.Error("Didn't create dummy data")
t.Errorf("Expected reply: <nil>, got: %#v", client.err)
}
// change TTL of only one item
client.Exec("TTL", []string{"key50", "4"})
time.Sleep(7 * time.Second)
client.Exec("size", []string{})
// check that size decreased by one
if client.reply != "199" {
t.Error("Didn't remove the expired key")
t.Errorf("Expected reply: \"199\", got: \"%s\"", client.reply)
}
if client.err != nil {
t.Error("Didn't remove the expired key")
t.Errorf("Expected reply: <nil>, got: %#v", client.err)
}
}
func TestExec(t *testing.T) {
client := setupTestClient()
// check initial client state
if client.cmd != "" {
t.Errorf("Expected cmd: \"\", got: \"%s\"", client.cmd)
}
client.Exec("set", []string{"x", "42"})
// check client state after command execution
if client.cmd != "SET" {
t.Errorf("Expected cmd: \"SET\", got: \"%s\"", client.cmd)
}
// check nonexistent command
client.Exec("WRONGCOMMAND", []string{})
// client store previous successful request state
if client.cmd != "SET" {
t.Errorf("Expected cmd: \"SET\", got: \"%s\"", client.cmd)
}
}