-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
234 lines (171 loc) · 6.83 KB
/
main_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"testing"
)
func TestReadUncommitted(t *testing.T) {
database := newDatabase()
database.defaultIsolation = ReadUncommittedIsolation
c1 := database.newConnection()
c1.mustExecCommand("begin", nil)
c2 := database.newConnection()
c2.mustExecCommand("begin", nil)
c1.mustExecCommand("set", []string{"x", "hey"})
// Update is visible to self.
res := c1.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c1 get x")
// But since read uncommitted, also available to everyone else.
res = c2.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c2 get x")
// And if we delete, that should be respected.
res = c1.mustExecCommand("delete", []string{"x"})
assertEq(res, "", "c1 delete x")
res, err := c1.execCommand("get", []string{"x"})
assertEq(res, "", "c1 sees no x")
assertEq(err.Error(), "cannot get key that does not exist", "c1 sees no x")
res, err = c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 sees no x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 sees no x")
}
func TestReadCommitted(t *testing.T) {
database := newDatabase()
database.defaultIsolation = ReadCommittedIsolation
c1 := database.newConnection()
c1.mustExecCommand("begin", nil)
c2 := database.newConnection()
c2.mustExecCommand("begin", nil)
// Local change is visible locally.
c1.mustExecCommand("set", []string{"x", "hey"})
res := c1.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c1 get x")
// Update not available to this transaction since this is not
// committed.
res, err := c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 get x")
c1.mustExecCommand("commit", nil)
// Now that it's been committed, it's visible in c2.
res = c2.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c2 get x")
c3 := database.newConnection()
c3.mustExecCommand("begin", nil)
// Local change is visible locally.
c3.mustExecCommand("set", []string{"x", "yall"})
res = c3.mustExecCommand("get", []string{"x"})
assertEq(res, "yall", "c3 get x")
// But not on the other commit, again.
res = c2.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c2 get x")
c3.mustExecCommand("abort", nil)
// And still not, if the other transaction aborted.
res = c2.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c2 get x")
// And if we delete it, it should show up deleted locally.
c2.mustExecCommand("delete", []string{"x"})
res, err = c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 get x")
c2.mustExecCommand("commit", nil)
// It should also show up as deleted in new transactions now
// that it has been committed.
c4 := database.newConnection()
c4.mustExecCommand("begin", nil)
res, err = c4.execCommand("get", []string{"x"})
assertEq(res, "", "c4 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c4 get x")
}
func TestRepeatableRead(t *testing.T) {
database := newDatabase()
database.defaultIsolation = RepeatableReadIsolation
c1 := database.newConnection()
c1.mustExecCommand("begin", nil)
c2 := database.newConnection()
c2.mustExecCommand("begin", nil)
// Local change is visible locally.
c1.mustExecCommand("set", []string{"x", "hey"})
res := c1.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c1 get x")
// Update not available to this transaction since this is not
// committed.
res, err := c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 get x")
c1.mustExecCommand("commit", nil)
// Even after committing, it's not visible in an existing
// transaction.
res, err = c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 get x")
// But is available in a new transaction.
c3 := database.newConnection()
c3.mustExecCommand("begin", nil)
res = c3.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c3 get x")
// Local change is visible locally.
c3.mustExecCommand("set", []string{"x", "yall"})
res = c3.mustExecCommand("get", []string{"x"})
assertEq(res, "yall", "c3 get x")
// But not on the other commit, again.
res, err = c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 get x")
c3.mustExecCommand("abort", nil)
// And still not, regardless of abort, because it's an older
// transaction.
res, err = c2.execCommand("get", []string{"x"})
assertEq(res, "", "c2 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c2 get x")
// And again still the aborted set is still not on a new
// transaction.
c4 := database.newConnection()
res = c4.mustExecCommand("begin", nil)
res = c4.mustExecCommand("get", []string{"x"})
assertEq(res, "hey", "c4 get x")
c4.mustExecCommand("delete", []string{"x"})
c4.mustExecCommand("commit", nil)
// But the delete is visible to new transactions now that this
// has been committed.
c5 := database.newConnection()
res = c5.mustExecCommand("begin", nil)
res, err = c5.execCommand("get", []string{"x"})
assertEq(res, "", "c5 get x")
assertEq(err.Error(), "cannot get key that does not exist", "c5 get x")
}
func TestSnapshotIsolation_writewrite_conflict(t *testing.T) {
database := newDatabase()
database.defaultIsolation = SnapshotIsolation
c1 := database.newConnection()
c1.mustExecCommand("begin", nil)
c2 := database.newConnection()
c2.mustExecCommand("begin", nil)
c3 := database.newConnection()
c3.mustExecCommand("begin", nil)
c1.mustExecCommand("set", []string{"x", "hey"})
c1.mustExecCommand("commit", nil)
c2.mustExecCommand("set", []string{"x", "hey"})
res, err := c2.execCommand("commit", nil)
assertEq(res, "", "c2 commit")
assertEq(err.Error(), "write-write conflict", "c2 commit")
// But unrelated keys cause no conflict.
c3.mustExecCommand("set", []string{"y", "no conflict"})
c3.mustExecCommand("commit", nil)
}
func TestSerializableIsolation_readwrite_conflict(t *testing.T) {
database := newDatabase()
database.defaultIsolation = SerializableIsolation
c1 := database.newConnection()
c1.mustExecCommand("begin", nil)
c2 := database.newConnection()
c2.mustExecCommand("begin", nil)
c3 := database.newConnection()
c3.mustExecCommand("begin", nil)
c1.mustExecCommand("set", []string{"x", "hey"})
c1.mustExecCommand("commit", nil)
_, err := c2.execCommand("get", []string{"x"})
assertEq(err.Error(), "cannot get key that does not exist", "c5 get x")
res, err := c2.execCommand("commit", nil)
assertEq(res, "", "c2 commit")
assertEq(err.Error(), "read-write conflict", "c2 commit")
// But unrelated keys cause no conflict.
c3.mustExecCommand("set", []string{"y", "no conflict"})
c3.mustExecCommand("commit", nil)
}