forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetex_test.go
142 lines (136 loc) · 6.24 KB
/
getex_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
package tests
import (
"strconv"
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestGetEx(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()
Etime5 := strconv.FormatInt(time.Now().Unix()+5, 10)
Etime10 := strconv.FormatInt(time.Now().Unix()+10, 10)
testCases := []struct {
name string
commands []string
expected []interface{}
assert_type []string
delay []time.Duration
}{
{
name: "GetEx Simple Value",
commands: []string{"SET foo bar", "GETEX foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", "bar", int64(-1)},
assert_type: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
{
name: "GetEx Non-Existent Key",
commands: []string{"GETEX foo", "TTL foo"},
expected: []interface{}{"(nil)", int64(-2)},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "GetEx with EX option",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo ex 2", "TTL foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "bar", int64(2), "(nil)", int64(-2)},
assert_type: []string{"equal", "equal", "equal", "equal", "assert", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 2 * time.Second, 0},
},
{
name: "GetEx with PX option",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo px 2000", "TTL foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "bar", int64(2), "(nil)", int64(-2)},
assert_type: []string{"equal", "equal", "equal", "equal", "assert", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 2 * time.Second, 0},
},
{
name: "GetEx with EX option and invalid value",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo ex -1", "TTL foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "ERR invalid expire time in 'getex' command", int64(-1), "bar", int64(-1)},
assert_type: []string{"equal", "equal", "equal", "equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 0, 0},
},
{
name: "GetEx with PX option and invalid value",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo px -1", "TTL foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "ERR invalid expire time in 'getex' command", int64(-1), "bar", int64(-1)},
assert_type: []string{"equal", "equal", "equal", "equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 0, 0},
},
{
name: "GetEx with EXAT option",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo exat " + Etime5, "TTL foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "bar", int64(5), "(nil)", int64(-2)},
assert_type: []string{"equal", "equal", "equal", "equal", "assert", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 5 * time.Second, 0},
},
{
name: "GetEx with PXAT option",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo pxat " + Etime10 + "000", "TTL foo", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "bar", int64(5), "(nil)", int64(-2)},
assert_type: []string{"equal", "equal", "equal", "equal", "assert", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 5 * time.Second, 0},
},
{
name: "GetEx with EXAT option and invalid value",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo exat 123123", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "bar", "(nil)", int64(-2)},
assert_type: []string{"equal", "equal", "equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 0},
},
{
name: "GetEx with PXAT option and invalid value",
commands: []string{"SET foo bar", "GETEX foo", "TTL foo", "GETEX foo pxat 123123", "GETEX foo", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(-1), "bar", "(nil)", int64(-2)},
assert_type: []string{"equal", "equal", "equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0, 0},
},
{
name: "GetEx with Persist option",
commands: []string{"SET foo bar", "GETEX foo ex 2", "TTL foo", "GETEX foo persist", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(2), "bar", int64(-1)},
assert_type: []string{"equal", "equal", "assert", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0, 0},
},
{
name: "GetEx with multiple expiry options",
commands: []string{"SET foo bar", "GETEX foo ex 2 px 123123", "TTL foo", "GETEX foo"},
expected: []interface{}{"OK", "ERR syntax error", int64(-1), "bar"},
assert_type: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
{
name: "GetEx with persist and ex options",
commands: []string{"SET foo bar", "GETEX foo ex 2", "TTL foo", "GETEX foo persist ex 2", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(2), "ERR syntax error", int64(2)},
assert_type: []string{"equal", "equal", "assert", "equal", "assert"},
delay: []time.Duration{0, 0, 0, 0, 0},
},
{
name: "GetEx with persist and px options",
commands: []string{"SET foo bar", "GETEX foo px 2000", "TTL foo", "GETEX foo px 2000 persist", "TTL foo"},
expected: []interface{}{"OK", "bar", int64(2), "ERR syntax error", int64(2)},
assert_type: []string{"equal", "equal", "assert", "equal", "assert"},
delay: []time.Duration{0, 0, 0, 0, 0},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// deleteTestKeys([]string{"foo"}, store)
fireCommand(conn, "DEL foo")
for i, cmd := range tc.commands {
if tc.delay[i] > 0 {
time.Sleep(tc.delay[i])
}
result := fireCommand(conn, cmd)
if tc.assert_type[i] == "equal" {
assert.DeepEqual(t, tc.expected[i], result)
} else if tc.assert_type[i] == "assert" {
assert.Assert(t, result.(int64) <= tc.expected[i].(int64), "Expected %v to be less than or equal to %v", result, tc.expected[i])
}
}
})
}
}