- Notifications
You must be signed in to change notification settings - Fork 417
/
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
importasyncpg
fromasyncpgimport_testbaseastb
ERRNUM='unexpected number of attributes of composite type'
ERRTYP='unexpected data type of composite type'
classTestCacheInvalidation(tb.ConnectedTestCase):
def_get_cached_statements(self, connection=None):
ifconnectionisNone:
connection=self.con
returnlist(connection._stmt_cache.iter_statements())
def_check_statements_are_not_closed(self, statements):
self.assertGreater(len(statements), 0)
self.assertTrue(all(nots.closedforsinstatements))
def_check_statements_are_closed(self, statements):
self.assertGreater(len(statements), 0)
self.assertTrue(all(s.closedforsinstatements))
asyncdeftest_prepare_cache_invalidation_silent(self):
awaitself.con.execute('CREATE TABLE tab1(a int, b int)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, 2)')
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
statements=self._get_cached_statements()
self._check_statements_are_not_closed(statements)
awaitself.con.execute(
'ALTER TABLE tab1 ALTER COLUMN b SET DATA TYPE text')
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
self._check_statements_are_closed(statements)
finally:
awaitself.con.execute('DROP TABLE tab1')
asyncdeftest_prepare_cache_invalidation_in_transaction(self):
awaitself.con.execute('CREATE TABLE tab1(a int, b int)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, 2)')
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
statements=self._get_cached_statements()
self._check_statements_are_not_closed(statements)
awaitself.con.execute(
'ALTER TABLE tab1 ALTER COLUMN b SET DATA TYPE text')
withself.assertRaisesRegex(asyncpg.InvalidCachedStatementError,
'cached statement plan is invalid'):
asyncwithself.con.transaction():
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# This is now OK,
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
finally:
awaitself.con.execute('DROP TABLE tab1')
asyncdeftest_prepare_cache_invalidation_in_pool(self):
pool=awaitself.create_pool(database='postgres',
min_size=2, max_size=2)
awaitself.con.execute('CREATE TABLE tab1(a int, b int)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, 2)')
con1=awaitpool.acquire()
con2=awaitpool.acquire()
result=awaitcon1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, 2))
result=awaitcon2.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)
awaitself.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=awaitcon1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
self._check_statements_are_closed(statements1)
self._check_statements_are_closed(statements2)
asyncwithcon2.transaction():
# This should work, as con1 should have invalidated
# the plan cache.
result=awaitcon2.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, '2'))
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitpool.release(con2)
awaitpool.release(con1)
awaitpool.close()
asyncdeftest_type_cache_invalidation_in_transaction(self):
awaitself.con.execute('CREATE TYPE typ1 AS (x int, y int)')
awaitself.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements=self._get_cached_statements()
self._check_statements_are_not_closed(statements)
asyncwithself.con.transaction():
awaitself.con.execute('ALTER TYPE typ1 ADD ATTRIBUTE c text')
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitself.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# The second request must be correct (cache was dropped):
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
# This is now OK, the cache is actual after the transaction.
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitself.con.execute('DROP TYPE typ1')
asyncdeftest_type_cache_invalidation_in_cancelled_transaction(self):
awaitself.con.execute('CREATE TYPE typ1 AS (x int, y int)')
awaitself.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements=self._get_cached_statements()
self._check_statements_are_not_closed(statements)
try:
asyncwithself.con.transaction():
awaitself.con.execute(
'ALTER TYPE typ1 ADD ATTRIBUTE c text')
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitself.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# The second request must be correct (cache was dropped):
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
raiseUserWarning# Just to generate ROLLBACK
exceptUserWarning:
pass
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitself.con.fetchrow('SELECT * FROM tab1')
# This is now OK, the cache is filled after being dropped.
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitself.con.execute('DROP TYPE typ1')
asyncdeftest_prepared_type_cache_invalidation(self):
awaitself.con.execute('CREATE TYPE typ1 AS (x int, y int)')
awaitself.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
prep=awaitself.con._prepare('SELECT * FROM tab1',
use_cache=True)
result=awaitprep.fetchrow()
self.assertEqual(result, (1, (2, 3)))
statements=self._get_cached_statements()
self._check_statements_are_not_closed(statements)
try:
asyncwithself.con.transaction():
awaitself.con.execute(
'ALTER TYPE typ1 ADD ATTRIBUTE c text')
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitprep.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.
withself.assertRaisesRegex(
asyncpg.InterfaceError,
'the prepared statement is closed'):
awaitprep.fetchrow()
prep=awaitself.con._prepare('SELECT * FROM tab1',
use_cache=True)
# The second PS must be correct (cache was dropped):
result=awaitprep.fetchrow()
self.assertEqual(result, (1, (2, 3, None)))
raiseUserWarning# Just to generate ROLLBACK
exceptUserWarning:
pass
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitprep.fetchrow()
# Reprepare it again after dropping cache.
prep=awaitself.con._prepare('SELECT * FROM tab1',
use_cache=True)
# This is now OK, the cache is filled after being dropped.
result=awaitprep.fetchrow()
self.assertEqual(result, (1, (2, 3)))
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitself.con.execute('DROP TYPE typ1')
asyncdeftest_type_cache_invalidation_on_drop_type_attr(self):
awaitself.con.execute('CREATE TYPE typ1 AS (x int, y int, c text)')
awaitself.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
awaitself.con.execute(
'INSERT INTO tab1 VALUES (1, (2, 3, $1))', 'x')
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, 'x')))
statements=self._get_cached_statements()
self._check_statements_are_not_closed(statements)
awaitself.con.execute('ALTER TYPE typ1 DROP ATTRIBUTE x')
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitself.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# This is now OK, the cache is filled after being dropped.
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (3, 'x')))
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitself.con.execute('DROP TYPE typ1')
asyncdeftest_type_cache_invalidation_on_change_attr(self):
awaitself.con.execute('CREATE TYPE typ1 AS (x int, y int)')
awaitself.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result=awaitself.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.
awaitself.con.execute('ALTER TYPE typ1 DROP ATTRIBUTE y')
awaitself.con.execute('ALTER TYPE typ1 ADD ATTRIBUTE y bigint')
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRTYP):
awaitself.con.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements)
# This is now OK, the cache is filled after being dropped.
result=awaitself.con.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, None)))
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitself.con.execute('DROP TYPE typ1')
asyncdeftest_type_cache_invalidation_in_pool(self):
awaitself.con.execute('CREATE DATABASE testdb')
pool=awaitself.create_pool(database='postgres',
min_size=2, max_size=2)
pool_chk=awaitself.create_pool(database='testdb',
min_size=2, max_size=2)
awaitself.con.execute('CREATE TYPE typ1 AS (x int, y int)')
awaitself.con.execute('CREATE TABLE tab1(a int, b typ1)')
try:
awaitself.con.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
con1=awaitpool.acquire()
con2=awaitpool.acquire()
result=awaitcon1.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3)))
statements1=self._get_cached_statements(con1)
self._check_statements_are_not_closed(statements1)
result=awaitcon2.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=awaitpool_chk.acquire()
awaitcon_chk.execute('CREATE TYPE typ1 AS (x int, y int)')
awaitcon_chk.execute('CREATE TABLE tab1(a int, b typ1)')
awaitcon_chk.execute('INSERT INTO tab1 VALUES (1, (2, 3))')
result=awaitcon_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.
awaitself.con.execute('ALTER TYPE typ1 ADD ATTRIBUTE c text')
awaitcon_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.
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitcon1.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements1)
self._check_statements_are_closed(statements2)
asyncwithcon2.transaction():
# This should work, as con1 should have invalidated all caches.
result=awaitcon2.fetchrow('SELECT * FROM tab1')
self.assertEqual(result, (1, (2, 3, None)))
# After all the con1 uses actual info from renewed cache entry.
result=awaitcon1.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)
withself.assertRaisesRegex(
asyncpg.OutdatedSchemaCacheError, ERRNUM):
awaitcon_chk.fetchrow('SELECT * FROM tab1')
self._check_statements_are_closed(statements_chk)
finally:
awaitself.con.execute('DROP TABLE tab1')
awaitself.con.execute('DROP TYPE typ1')
awaitpool.release(con2)
awaitpool.release(con1)
awaitpool.close()
awaitpool_chk.release(con_chk)
awaitpool_chk.close()
awaitself.con.execute('DROP DATABASE testdb')