File: transaction.test.lua

package info (click to toggle)
tarantool 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 85,364 kB
  • sloc: ansic: 513,760; cpp: 69,489; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,173; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (230 lines) | stat: -rw-r--r-- 5,720 bytes parent folder | download | duplicates (3)
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
test_run = require('test_run')
inspector = test_run.new()
engine = inspector:get_cfg('engine')

-- basic transaction tests
space = box.schema.space.create('test', { engine = engine })
index = space:create_index('primary', { type = 'tree', parts = {1, 'unsigned'} })

-- begin/rollback
inspector:cmd("setopt delimiter ';'")
box.begin()
for key = 1, 10 do space:insert({key}) end
box.rollback();
inspector:cmd("setopt delimiter ''");

t = {}
for key = 1, 10 do assert(#space:select({key}) == 0) end
t

-- begin/commit insert
inspector:cmd("setopt delimiter ';'")
box.begin()
for key = 1, 10 do space:insert({key}) end
box.commit();
inspector:cmd("setopt delimiter ''");

t = {}
for key = 1, 10 do table.insert(t, space:select({key})[1]) end
t

-- begin/commit delete
inspector:cmd("setopt delimiter ';'")
box.begin()
for key = 1, 10 do space:delete({key}) end
box.commit();
inspector:cmd("setopt delimiter ''");

t = {}
for key = 1, 10 do assert(#space:select({key}) == 0) end
t
space:drop()


-- multi-space transactions
a = box.schema.space.create('test', { engine = engine })
index = a:create_index('primary', { type = 'tree', parts = {1, 'unsigned'} })
b = box.schema.space.create('test_tmp', { engine = engine })
index = b:create_index('primary', { type = 'tree', parts = {1, 'unsigned'} })

-- begin/rollback
inspector:cmd("setopt delimiter ';'")
box.begin()
for key = 1, 10 do a:insert({key}) end
for key = 1, 10 do b:insert({key}) end
box.rollback();
inspector:cmd("setopt delimiter ''");

t = {}
for key = 1, 10 do assert(#a:select({key}) == 0) end
t
for key = 1, 10 do assert(#b:select({key}) == 0) end
t

-- begin/commit insert
inspector:cmd("setopt delimiter ';'")
box.begin()
for key = 1, 10 do a:insert({key}) end
for key = 1, 10 do b:insert({key}) end
box.commit();
inspector:cmd("setopt delimiter ''");

t = {}
for key = 1, 10 do table.insert(t, a:select({key})[1]) end
t
t = {}
for key = 1, 10 do table.insert(t, b:select({key})[1]) end
t

-- begin/commit delete
inspector:cmd("setopt delimiter ';'")
box.begin()
for key = 1, 10 do a:delete({key}) end
for key = 1, 10 do b:delete({key}) end
box.commit();
inspector:cmd("setopt delimiter ''");

t = {}
for key = 1, 10 do assert(#a:select({key}) == 0) end
t
for key = 1, 10 do assert(#b:select({key}) == 0) end
t
a:drop()
b:drop()

-- ensure findByKey works in empty transaction context
space = box.schema.space.create('test', { engine = engine })
index = space:create_index('primary', { type = 'tree', parts = {1, 'unsigned'} })

inspector:cmd("setopt delimiter ';'")
box.begin()
space:get({0})
box.rollback();
inspector:cmd("setopt delimiter ''");
space:drop()

--
-- gh-857: on_commit/rollback triggers in Lua.
--
s = box.schema.create_space('test')
_ = s:create_index('pk')
call_list = {}
on_commit = {}
inspector:cmd("setopt delimiter ';'")
for i = 1, 3 do
    table.insert(on_commit, function()
        table.insert(call_list, 'on_commit_'..tostring(i))
    end)
end;
function on_rollback()
    table.insert(call_list, 'on_rollback')
end;
inspector:cmd("setopt delimiter ''");

-- A basic test: when a transaction is committed, the appropriate
-- trigger is called, in opposite to its on_rollback vis-a-vis.
box.begin() s:replace{1} box.on_commit(on_commit[1]) box.on_rollback(on_rollback) box.commit()
call_list
call_list = {}

-- The same vice versa.
box.begin() s:replace{2} box.on_commit(on_commit[1]) box.on_rollback(on_rollback) box.rollback()
call_list
call_list = {}

-- Incorrect usage outside of a transaction.
box.on_commit()

-- Incorrect usage inside a transaction.
box.begin() s:replace{3} box.on_rollback(on_rollback) box.on_commit(100)
box.rollback()
call_list
call_list = {}

-- Multiple triggers.
funcs = {}
inspector:cmd("setopt delimiter ';'")
box.begin()
table.insert(funcs, box.on_commit())
box.on_commit(on_commit[1])
table.insert(funcs, box.on_commit())
box.on_commit(on_commit[2])
table.insert(funcs, box.on_commit())
box.on_commit(on_commit[3])
box.on_commit(on_commit[3])
table.insert(funcs, box.on_commit())
box.on_commit(on_commit[3], on_commit[2])
table.insert(funcs, box.on_commit())
box.commit();

for _, list in pairs(funcs) do
    for i, f1 in pairs(list) do
        for j, f2 in pairs(on_commit) do
            if f1 == f2 then
                list[i] = 'on_commit_'..tostring(j)
                break
            end
        end
    end
end;
inspector:cmd("setopt delimiter ''");
-- Yes, the order is reversed. Like all other Lua triggers.
call_list
funcs
--
-- Test on_commit/rollback txn iterators.
--
iter = nil
function steal_iterator(iter_) iter = iter_ end
box.begin() s:replace{1, 1} box.on_commit(steal_iterator) box.commit()
-- Fail. No transaction.
iter()
-- Fail. Not the same transaction.
box.begin() iter()
box.rollback()
-- Test the same for the iterator generator.
a = nil
b = nil
c = nil
function steal_iterator(iter) a, b, c = iter() end
box.begin() s:replace{1, 1} box.on_commit(steal_iterator) box.commit()
a(b, c)
box.begin() a(b, c)
box.rollback()
-- Test appropriate usage.
for i = 1, 5 do s:replace{i} end
stmts = nil
inspector:cmd("setopt delimiter ';'")
function on_commit(iter)
    stmts = {}
    for i, old, new, space in iter() do
        stmts[i] = {old, new, space == s.id or space}
    end
end;

box.begin()
s:replace{1, 1}
s:replace{2, 2}
s:delete{3}
s:replace{6, 6}
s:replace{0, 0}
-- Test read and rolled back statements.
s:get{3}
s:get{1}
pcall(s.insert, s, {1})
box.on_commit(on_commit)
s:replace{7, 7}
-- Test save point rollbacks.
sv = box.savepoint()
s:replace{8, 8}
box.rollback_to_savepoint(sv)
s:replace{9, 9}
box.commit();
inspector:cmd("setopt delimiter ''");
stmts

-- Check empty transaction iteration.
box.begin() box.on_commit(on_commit) box.commit()
stmts

s:drop()