- Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathtest_timeout.py
154 lines (123 loc) · 6.34 KB
/
test_timeout.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
# 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
importasyncio
importasyncpg
fromasyncpgimportconnectionaspg_connection
fromasyncpgimport_testbaseastb
MAX_RUNTIME=0.5
classTestTimeout(tb.ConnectedTestCase):
asyncdeftest_timeout_01(self):
formethnamein {'fetch', 'fetchrow', 'fetchval', 'execute'}:
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
meth=getattr(self.con, methname)
awaitmeth('select pg_sleep(10)', timeout=0.02)
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
asyncdeftest_timeout_02(self):
st=awaitself.con.prepare('select pg_sleep(10)')
formethnamein {'fetch', 'fetchrow', 'fetchval'}:
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
meth=getattr(st, methname)
awaitmeth(timeout=0.02)
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
asyncdeftest_timeout_03(self):
task=self.loop.create_task(
self.con.fetch('select pg_sleep(10)', timeout=0.2))
awaitasyncio.sleep(0.05)
task.cancel()
withself.assertRaises(asyncio.CancelledError), \
self.assertRunUnder(MAX_RUNTIME):
awaittask
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
asyncdeftest_timeout_04(self):
st=awaitself.con.prepare('select pg_sleep(10)', timeout=0.1)
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
asyncwithself.con.transaction():
asyncfor_inst.cursor(timeout=0.1): # NOQA
pass
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
st=awaitself.con.prepare('select pg_sleep(10)', timeout=0.1)
asyncwithself.con.transaction():
cur=awaitst.cursor()
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
awaitcur.fetch(1, timeout=0.1)
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
asyncdeftest_timeout_05(self):
# Stress-test timeouts - try to trigger a race condition
# between a cancellation request to Postgres and next
# query (SELECT 1)
for_inrange(500):
withself.assertRaises(asyncio.TimeoutError):
awaitself.con.fetch('SELECT pg_sleep(1)', timeout=1e-10)
self.assertEqual(awaitself.con.fetch('SELECT 1'), [(1,)])
asyncdeftest_timeout_06(self):
asyncwithself.con.transaction():
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
asyncfor_inself.con.cursor( # NOQA
'select pg_sleep(10)', timeout=0.1):
pass
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
asyncwithself.con.transaction():
cur=awaitself.con.cursor('select pg_sleep(10)')
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
awaitcur.fetch(1, timeout=0.1)
asyncwithself.con.transaction():
cur=awaitself.con.cursor('select pg_sleep(10)')
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
awaitcur.forward(1, timeout=1e-10)
asyncwithself.con.transaction():
cur=awaitself.con.cursor('select pg_sleep(10)')
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
awaitcur.fetchrow(timeout=0.1)
asyncwithself.con.transaction():
cur=awaitself.con.cursor('select pg_sleep(10)')
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
awaitcur.fetchrow(timeout=0.1)
withself.assertRaises(asyncpg.InFailedSQLTransactionError):
awaitcur.fetch(1)
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
asyncdeftest_invalid_timeout(self):
forcommand_timeoutin ('a', False, -1):
withself.subTest(command_timeout=command_timeout):
withself.assertRaisesRegex(ValueError,
'invalid command_timeout'):
awaitself.connect(command_timeout=command_timeout)
# Note: negative timeouts are OK for method calls.
formethnamein {'fetch', 'fetchrow', 'fetchval', 'execute'}:
fortimeoutin ('a', False):
withself.subTest(timeout=timeout):
withself.assertRaisesRegex(ValueError, 'invalid timeout'):
awaitself.con.execute('SELECT 1', timeout=timeout)
classTestConnectionCommandTimeout(tb.ConnectedTestCase):
@tb.with_connection_options(command_timeout=0.2)
asyncdeftest_command_timeout_01(self):
formethnamein {'fetch', 'fetchrow', 'fetchval', 'execute'}:
withself.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
meth=getattr(self.con, methname)
awaitmeth('select pg_sleep(10)')
self.assertEqual(awaitself.con.fetch('select 1'), [(1,)])
classSlowPrepareConnection(pg_connection.Connection):
"""Connection class to test timeouts."""
asyncdef_get_statement(self, query, timeout, **kwargs):
awaitasyncio.sleep(0.3)
returnawaitsuper()._get_statement(query, timeout, **kwargs)
classTestTimeoutCoversPrepare(tb.ConnectedTestCase):
@tb.with_connection_options(connection_class=SlowPrepareConnection,
command_timeout=0.3)
asyncdeftest_timeout_covers_prepare_01(self):
formethnamein {'fetch', 'fetchrow', 'fetchval', 'execute'}:
withself.assertRaises(asyncio.TimeoutError):
meth=getattr(self.con, methname)
awaitmeth('select pg_sleep($1)', 0.2)