forked from MagicStack/asyncpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cache_invalidation.py
390 lines (299 loc) · 16.2 KB
/
test_cache_invalidation.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright (C) 2016-present the asyncpg authors and contributors
# <see AUTHORS file>
#
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
import asyncpg
from asyncpg import _testbase as tb
ERRNUM = 'unexpected number of attributes of composite type'
ERRTYP = 'unexpected data type of composite type'
class TestCacheInvalidation(tb.ConnectedTestCase):
def _get_cached_statements(self, connection=None):
if connection is None:
connection = self.con
return list(connection._stmt_cache.iter_statements())
def _check_statements_are_not_closed(self, statements):
self.assertGreater(len(statements), 0)
self.assertTrue(all(not s.closed for s in statements))
def _check_statements_are_closed(self, statements):
self.assertGreater(len(statements), 0)
self.assertTrue(all(s.closed for s in statements))
async def test_prepare_cache_invalidation_silent(self):
await self.con.execute('CREATE TABLE tab1(a int, b int)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, 2)')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
await self.con.execute(
'ALTER TABLE tab1 ALTER COLUMN b SET DATA TYPE text')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
self._check_statements_are_closed(statements)
finally:
await self.con.execute('DROP TABLE tab1')
async def test_prepare_cache_invalidation_in_transaction(self):
await self.con.execute('CREATE TABLE tab1(a int, b int)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, 2)')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
await self.con.execute(
'ALTER TABLE tab1 ALTER COLUMN b SET DATA TYPE text')
with self.assertRaisesRegex(asyncpg.InvalidCachedStatementError,
'cached statement plan is invalid'):
async with self.con.transaction():
result = await self.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# This is now OK,
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
finally:
await self.con.execute('DROP TABLE tab1')
async def test_prepare_cache_invalidation_in_pool(self):
pool = await self.create_pool(database='postgres',
min_size=2, max_size=2)
await self.con.execute('CREATE TABLE tab1(a int, b int)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, 2)')
con1 = await pool.acquire()
con2 = await pool.acquire()
result = await con1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
result = await con2.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
statements1 = self._get_cached_statements(con1)
self._check_statements_are_not_closed(statements1)
statements2 = self._get_cached_statements(con2)
self._check_statements_are_not_closed(statements2)
await self.con.execute(
'ALTER TABLE tab1 ALTER COLUMN b SET DATA TYPE text')
# con1 tries the same plan, will invalidate the cache
# for the entire pool.
result = await con1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
self._check_statements_are_closed(statements1)
self._check_statements_are_closed(statements2)
async with con2.transaction():
# This should work, as con1 should have invalidated
# the plan cache.
result = await con2.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
finally:
await self.con.execute('DROP TABLE tab1')
await pool.release(con2)
await pool.release(con1)
await pool.close()
async def test_type_cache_invalidation_in_transaction(self):
await self.con.execute('CREATE TYPE typ1 AS (x int, y int)')
await self.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
async with self.con.transaction():
await self.con.execute('ALTER TYPE typ1 ADD ATTRIBUTE c text')
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await self.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# The second request must be correct (cache was dropped):
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
# This is now OK, the cache is actual after the transaction.
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
finally:
await self.con.execute('DROP TABLE tab1')
await self.con.execute('DROP TYPE typ1')
async def test_type_cache_invalidation_in_cancelled_transaction(self):
await self.con.execute('CREATE TYPE typ1 AS (x int, y int)')
await self.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
try:
async with self.con.transaction():
await self.con.execute(
'ALTER TYPE typ1 ADD ATTRIBUTE c text')
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await self.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# The second request must be correct (cache was dropped):
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
raise UserWarning # Just to generate ROLLBACK
except UserWarning:
pass
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await self.con.fetchrow('SELECT * FROM tab1')
# This is now OK, the cache is filled after being dropped.
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
finally:
await self.con.execute('DROP TABLE tab1')
await self.con.execute('DROP TYPE typ1')
async def test_prepared_type_cache_invalidation(self):
await self.con.execute('CREATE TYPE typ1 AS (x int, y int)')
await self.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
prep = await self.con._prepare('SELECT * FROM tab1',
use_cache=True)
result = await prep.fetchrow()
self.assertEqual(result, (1, (2, 3)))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
try:
async with self.con.transaction():
await self.con.execute(
'ALTER TYPE typ1 ADD ATTRIBUTE c text')
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await prep.fetchrow()
self._check_statements_are_closed(statements)
# PS has its local cache for types codecs, even after the
# cache cleanup it is not possible to use it.
# That's why it is marked as closed.
with self.assertRaisesRegex(
asyncpg.InterfaceError,
'the prepared statement is closed'):
await prep.fetchrow()
prep = await self.con._prepare('SELECT * FROM tab1',
use_cache=True)
# The second PS must be correct (cache was dropped):
result = await prep.fetchrow()
self.assertEqual(result, (1, (2, 3, None)))
raise UserWarning # Just to generate ROLLBACK
except UserWarning:
pass
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await prep.fetchrow()
# Reprepare it again after dropping cache.
prep = await self.con._prepare('SELECT * FROM tab1',
use_cache=True)
# This is now OK, the cache is filled after being dropped.
result = await prep.fetchrow()
self.assertEqual(result, (1, (2, 3)))
finally:
await self.con.execute('DROP TABLE tab1')
await self.con.execute('DROP TYPE typ1')
async def test_type_cache_invalidation_on_drop_type_attr(self):
await self.con.execute('CREATE TYPE typ1 AS (x int, y int, c text)')
await self.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
await self.con.execute(
'INSERT INTO tab1 VALUES (1, (2, 3, $1))', 'x')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, 'x')))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
await self.con.execute('ALTER TYPE typ1 DROP ATTRIBUTE x')
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await self.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# This is now OK, the cache is filled after being dropped.
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (3, 'x')))
finally:
await self.con.execute('DROP TABLE tab1')
await self.con.execute('DROP TYPE typ1')
async def test_type_cache_invalidation_on_change_attr(self):
await self.con.execute('CREATE TYPE typ1 AS (x int, y int)')
await self.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements = self._get_cached_statements()
self._check_statements_are_not_closed(statements)
# It is slightly artificial, but can take place in transactional
# schema changing. Nevertheless, if the code checks and raises it
# the most probable reason is a difference with the cache type.
await self.con.execute('ALTER TYPE typ1 DROP ATTRIBUTE y')
await self.con.execute('ALTER TYPE typ1 ADD ATTRIBUTE y bigint')
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRTYP):
await self.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# This is now OK, the cache is filled after being dropped.
result = await self.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, None)))
finally:
await self.con.execute('DROP TABLE tab1')
await self.con.execute('DROP TYPE typ1')
async def test_type_cache_invalidation_in_pool(self):
await self.con.execute('CREATE DATABASE testdb')
pool = await self.create_pool(database='postgres',
min_size=2, max_size=2)
pool_chk = await self.create_pool(database='testdb',
min_size=2, max_size=2)
await self.con.execute('CREATE TYPE typ1 AS (x int, y int)')
await self.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
await self.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
con1 = await pool.acquire()
con2 = await pool.acquire()
result = await con1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements1 = self._get_cached_statements(con1)
self._check_statements_are_not_closed(statements1)
result = await con2.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements2 = self._get_cached_statements(con2)
self._check_statements_are_not_closed(statements2)
# Create the same schema in the "testdb", fetch data which caches
# type info.
con_chk = await pool_chk.acquire()
await con_chk.execute('CREATE TYPE typ1 AS (x int, y int)')
await con_chk.execute('CREATE TABLE tab1(a int, b typ1)')
await con_chk.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result = await con_chk.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements_chk = self._get_cached_statements(con_chk)
self._check_statements_are_not_closed(statements_chk)
# Change schema in the databases.
await self.con.execute('ALTER TYPE typ1 ADD ATTRIBUTE c text')
await con_chk.execute('ALTER TYPE typ1 ADD ATTRIBUTE c text')
# con1 tries to get cached type info, fails, but invalidates the
# cache for the entire pool.
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await con1.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements1)
self._check_statements_are_closed(statements2)
async with con2.transaction():
# This should work, as con1 should have invalidated all caches.
result = await con2.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
# After all the con1 uses actual info from renewed cache entry.
result = await con1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
# Check the invalidation is database-specific, i.e. cache entries
# for pool_chk/con_chk was not dropped via pool/con1.
self._check_statements_are_not_closed(statements_chk)
with self.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
await con_chk.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements_chk)
finally:
await self.con.execute('DROP TABLE tab1')
await self.con.execute('DROP TYPE typ1')
await pool.release(con2)
await pool.release(con1)
await pool.close()
await pool_chk.release(con_chk)
await pool_chk.close()
await self.con.execute('DROP DATABASE testdb')