-
-
Notifications
You must be signed in to change notification settings - Fork 530
/
test_savepoints.py
247 lines (206 loc) · 11.1 KB
/
test_savepoints.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
import sqlite3
from contextlib import suppress
import gevent
import pytest
from rotkehlchen.db.drivers.gevent import ContextError, DBConnection, DBConnectionType
from rotkehlchen.errors.asset import UnknownAsset
def test_unnamed_savepoints():
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
conn.execute('CREATE TABLE a(b INTEGER PRIMARY KEY)')
with conn.savepoint_ctx() as cursor1:
assert len(conn.savepoints) == 1
savepoint1 = next(iter(conn.savepoints))
cursor1.execute('INSERT INTO a VALUES (1)')
cursor2, savepoint2 = conn._enter_savepoint() # also check manual savepoints
assert list(conn.savepoints) == [savepoint1, savepoint2]
cursor2.execute('INSERT INTO a VALUES (2)')
# make sure that 2 was added
assert cursor1.execute('SELECT b FROM a').fetchall() == [(1,), (2,)]
conn.rollback_savepoint()
# check that the second savepoint was NOT released since it was only rolled back
assert list(conn.savepoints) == [savepoint1, savepoint2]
assert cursor1.execute('SELECT b FROM a').fetchall() == [(1,)] # 2 should not be there
cursor1.execute('INSERT INTO a VALUES (3)') # add one more value after the rollback
assert len(conn.savepoints) == 0 # check that we released successfully
# And make sure that the data is saved
assert conn.execute('SELECT b FROM a').fetchall() == [(1,), (3,)]
def test_savepoint_errors():
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
with pytest.raises(ContextError):
conn.release_savepoint()
conn._enter_savepoint('point')
with pytest.raises(ContextError), conn._enter_savepoint('point'):
...
with pytest.raises(ContextError):
conn.rollback_savepoint('abc')
def test_write_transaction_with_savepoint():
"""Test that opening a savepoint within a write transaction in the
same greenlet is okay"""
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
conn.execute('CREATE TABLE a(b INTEGER PRIMARY KEY)')
with conn.write_ctx() as write_cursor:
write_cursor.execute('INSERT INTO a VALUES (1)')
with conn.savepoint_ctx() as savepoint_cursor:
savepoint_cursor.execute('INSERT INTO a VALUES (2)')
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,)]
def test_write_transaction_with_savepoint_other_context():
"""Test that opening a savepoint from a different greenlet while a write
transaction is already open from another greenlet waits for the original to finish"""
def other_context(conn: 'DBConnection', first_run: bool) -> None:
with conn.savepoint_ctx() as savepoint1_cursor:
values = (2,) if first_run else (4,)
savepoint1_cursor.execute('INSERT INTO a VALUES (?)', values)
if first_run:
return
with suppress(ValueError), conn.savepoint_ctx() as savepoint2_cursor:
savepoint2_cursor.execute('INSERT INTO a VALUES (5)')
raise ValueError('Test rollback')
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
conn.execute('CREATE TABLE a(b INTEGER PRIMARY KEY)')
with conn.write_ctx() as write_cursor:
write_cursor.execute('INSERT INTO a VALUES (1)')
greenlet1 = gevent.spawn(other_context, conn, True)
gevent.sleep(.3) # context switch for a bit to let the other greenlet run
assert greenlet1.exception is None
assert greenlet1.dead is False, 'the other greenlet should still run'
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,)], 'other greenlet should not have written to the DB' # noqa: E501
gevent.joinall([greenlet1]) # wait till the other greenlet finishes
with conn.read_ctx() as cursor: # make sure it wrote in the DB
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,)], 'other greenlet should write to the DB' # noqa: E501
# now let's try with the other greenlet also rolling back part of the savepoint
with conn.write_ctx() as write_cursor:
write_cursor.execute('INSERT INTO a VALUES (3)')
greenlet1 = gevent.spawn(other_context, conn, False)
gevent.sleep(.3) # context switch for a bit to let the other greenlet run
assert greenlet1.exception is None
assert greenlet1.dead is False, 'the other greenlet should still run'
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,), (3,)], 'other greenlet should not have written to the DB' # noqa: E501
gevent.joinall([greenlet1]) # wait till the other greenlet finishes
with conn.read_ctx() as cursor: # make sure it wrote in the DB but not the last one
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,), (3,), (4,)], 'other greenlet should write to the DB' # noqa: E501
def test_savepoint_with_write_transaction():
"""Test that a write transaction under a savepoint can still happen by
switching to a savepoint instead"""
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
conn.execute('CREATE TABLE a(b INTEGER PRIMARY KEY)')
with conn.savepoint_ctx() as savepoint_cursor:
savepoint_cursor.execute('INSERT INTO a VALUES (1)')
with conn.write_ctx() as write_cursor:
write_cursor.execute('INSERT INTO a VALUES (2)')
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,)]
with suppress(ValueError), conn.savepoint_ctx() as savepoint_cursor:
savepoint_cursor.execute('INSERT INTO a VALUES (3)')
with conn.write_ctx() as write_cursor:
write_cursor.execute('INSERT INTO a VALUES (4)')
raise ValueError('Test rollback')
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,)]
def test_savepoint_with_write_transaction_other_context():
"""Test that a write transaction after a savepoint but in a different greenlet
does not continue the savepoint but instead waits"""
def other_context(conn) -> None:
with conn.write_ctx() as write_cursor:
write_cursor.execute('INSERT INTO a VALUES (4)')
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
conn.execute('CREATE TABLE a(b INTEGER PRIMARY KEY)')
with conn.savepoint_ctx() as savepoint_cursor:
savepoint_cursor.execute('INSERT INTO a VALUES (1)')
greenlet1 = gevent.spawn(other_context, conn)
gevent.sleep(.3) # context switch for a bit to let the other greenlet run
assert greenlet1.exception is None
assert greenlet1.dead is False, 'the other greenlet should still run'
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,)], 'other greenlet should not have written to the DB' # noqa: E501
gevent.joinall([greenlet1]) # wait till the other greenlet finishes
with conn.read_ctx() as cursor: # make sure it wrote in the DB
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (4,)], 'other greenlet should write to the DB' # noqa: E501
def test_open_savepoint_with_savepoint_other_context():
"""Test that opening a savepoint while a savepoint queue is already open in
another greenlet waits until the first one is completely done"""
def other_context(conn, first_run) -> None:
with conn.savepoint_ctx() as savepoint1_cursor:
values = (2,) if first_run else (4,)
savepoint1_cursor.execute('INSERT INTO a VALUES (?)', values)
if first_run:
return
with suppress(ValueError), conn.savepoint_ctx() as savepoint2_cursor:
savepoint2_cursor.execute('INSERT INTO a VALUES (5)')
raise ValueError('Test rollback')
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
conn.execute('CREATE TABLE a(b INTEGER PRIMARY KEY)')
with conn.savepoint_ctx() as savepoint_cursor:
savepoint_cursor.execute('INSERT INTO a VALUES (1)')
greenlet1 = gevent.spawn(other_context, conn, True)
gevent.sleep(.3) # context switch for a bit to let the other greenlet run
assert greenlet1.exception is None
assert greenlet1.dead is False, 'the other greenlet should still run'
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,)], 'other greenlet should not have written to the DB' # noqa: E501
gevent.joinall([greenlet1]) # wait till the other greenlet finishes
with conn.read_ctx() as cursor: # make sure it wrote in the DB
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,)], 'other greenlet should write to the DB' # noqa: E501
# now let's try with the other greenlet also rolling back part of the savepoint
with conn.savepoint_ctx() as savepoint_cursor:
savepoint_cursor.execute('INSERT INTO a VALUES (3)')
greenlet1 = gevent.spawn(other_context, conn, False)
gevent.sleep(.3) # context switch for a bit to let the other greenlet run
assert greenlet1.exception is None
assert greenlet1.dead is False, 'the other greenlet should still run'
with conn.read_ctx() as cursor:
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,), (3,)], 'other greenlet should not have written to the DB' # noqa: E501
gevent.joinall([greenlet1]) # wait till the other greenlet finishes
with conn.read_ctx() as cursor: # make sure it wrote in the DB but not the last one
assert cursor.execute('SELECT b from a').fetchall() == [(1,), (2,), (3,), (4,)], 'other greenlet should write to the DB' # noqa: E501
def test_rollback_in_savepoints():
"""
Test that savepoints are released when an error is raised. This verifies
that a rollback is always followed up by a release since that is required.
"""
conn = DBConnection(
path=':memory:',
connection_type=DBConnectionType.GLOBAL,
sql_vm_instructions_cb=0,
)
with (
suppress(UnknownAsset),
conn.savepoint_ctx(savepoint_name='mysave') as savepoint_cursor,
):
savepoint_cursor.execute('CREATE TABLE mytable(age INTEGER PRIMARY KEY)')
# raise the error to trigger the except clause in savepoint_ctx
raise UnknownAsset('ETH')
# leaving the with statement should have released the savepoint and trying to release
# again the savepoint should raise an error because we have already released it.
with pytest.raises(sqlite3.OperationalError):
conn.execute('RELEASE SAVEPOINT "mysave"')