-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_test.go
104 lines (98 loc) · 3.61 KB
/
transaction_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
package godis
import (
"testing"
"github.com/tigbox/godis/lib/utils"
"github.com/tigbox/godis/redis/connection"
"github.com/tigbox/godis/redis/reply/asserts"
)
func TestMulti(t *testing.T) {
conn := new(connection.FakeConn)
testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
result := testServer.Exec(conn, utils.ToCmdLine("multi"))
asserts.AssertNotError(t, result)
key := utils.RandString(10)
value := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("set", key, value))
key2 := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("rpush", key2, value))
result = testServer.Exec(conn, utils.ToCmdLine("exec"))
asserts.AssertNotError(t, result)
result = testServer.Exec(conn, utils.ToCmdLine("get", key))
asserts.AssertBulkReply(t, result, value)
result = testServer.Exec(conn, utils.ToCmdLine("lrange", key2, "0", "-1"))
asserts.AssertMultiBulkReply(t, result, []string{value})
if len(conn.GetWatching()) > 0 {
t.Error("watching map should be reset")
}
if len(conn.GetQueuedCmdLine()) > 0 {
t.Error("queue should be reset")
}
}
func TestRollback(t *testing.T) {
conn := new(connection.FakeConn)
testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
result := testServer.Exec(conn, utils.ToCmdLine("multi"))
asserts.AssertNotError(t, result)
key := utils.RandString(10)
value := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("set", key, value))
testServer.Exec(conn, utils.ToCmdLine("rpush", key, value))
result = testServer.Exec(conn, utils.ToCmdLine("exec"))
asserts.AssertErrReply(t, result, "EXECABORT Transaction discarded because of previous errors.")
result = testServer.Exec(conn, utils.ToCmdLine("type", key))
asserts.AssertStatusReply(t, result, "none")
if len(conn.GetWatching()) > 0 {
t.Error("watching map should be reset")
}
if len(conn.GetQueuedCmdLine()) > 0 {
t.Error("queue should be reset")
}
}
func TestDiscard(t *testing.T) {
conn := new(connection.FakeConn)
testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
result := testServer.Exec(conn, utils.ToCmdLine("multi"))
asserts.AssertNotError(t, result)
key := utils.RandString(10)
value := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("set", key, value))
key2 := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("rpush", key2, value))
result = testServer.Exec(conn, utils.ToCmdLine("discard"))
asserts.AssertNotError(t, result)
result = testServer.Exec(conn, utils.ToCmdLine("get", key))
asserts.AssertNullBulk(t, result)
result = testServer.Exec(conn, utils.ToCmdLine("lrange", key2, "0", "-1"))
asserts.AssertMultiBulkReplySize(t, result, 0)
if len(conn.GetWatching()) > 0 {
t.Error("watching map should be reset")
}
if len(conn.GetQueuedCmdLine()) > 0 {
t.Error("queue should be reset")
}
}
func TestWatch(t *testing.T) {
conn := new(connection.FakeConn)
testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
for i := 0; i < 3; i++ {
key := utils.RandString(10)
value := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("watch", key))
testServer.Exec(conn, utils.ToCmdLine("set", key, value))
result := testServer.Exec(conn, utils.ToCmdLine("multi"))
asserts.AssertNotError(t, result)
key2 := utils.RandString(10)
value2 := utils.RandString(10)
testServer.Exec(conn, utils.ToCmdLine("set", key2, value2))
result = testServer.Exec(conn, utils.ToCmdLine("exec"))
asserts.AssertNotError(t, result)
result = testServer.Exec(conn, utils.ToCmdLine("get", key2))
asserts.AssertNullBulk(t, result)
if len(conn.GetWatching()) > 0 {
t.Error("watching map should be reset")
}
if len(conn.GetQueuedCmdLine()) > 0 {
t.Error("queue should be reset")
}
}
}