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
|
import sys
import time
import signal
import asyncio
import subprocess as sp
from asyncio.queues import Queue
from typing import List, Tuple
import pytest
import psycopg
from psycopg import errors as e
from psycopg._compat import create_task
pytestmark = pytest.mark.asyncio
@pytest.mark.slow
async def test_commit_concurrency(aconn):
# Check the condition reported in psycopg2#103
# Because of bad status check, we commit even when a commit is already on
# its way. We can detect this condition by the warnings.
notices = Queue() # type: ignore[var-annotated]
aconn.add_notice_handler(lambda diag: notices.put_nowait(diag.message_primary))
stop = False
async def committer():
nonlocal stop
while not stop:
await aconn.commit()
await asyncio.sleep(0) # Allow the other worker to work
async def runner():
nonlocal stop
cur = aconn.cursor()
for i in range(1000):
await cur.execute("select %s;", (i,))
await aconn.commit()
# Stop the committer thread
stop = True
await asyncio.gather(committer(), runner())
assert notices.empty(), "%d notices raised" % notices.qsize()
@pytest.mark.slow
async def test_concurrent_execution(aconn_cls, dsn):
async def worker():
cnn = await aconn_cls.connect(dsn)
cur = cnn.cursor()
await cur.execute("select pg_sleep(0.5)")
await cur.close()
await cnn.close()
workers = [worker(), worker()]
t0 = time.time()
await asyncio.gather(*workers)
assert time.time() - t0 < 0.8, "something broken in concurrency"
@pytest.mark.slow
@pytest.mark.timing
@pytest.mark.crdb_skip("notify")
async def test_notifies(aconn_cls, aconn, dsn):
nconn = await aconn_cls.connect(dsn, autocommit=True)
npid = nconn.pgconn.backend_pid
async def notifier():
cur = nconn.cursor()
await asyncio.sleep(0.25)
await cur.execute("notify foo, '1'")
await asyncio.sleep(0.25)
await cur.execute("notify foo, '2'")
await nconn.close()
async def receiver():
await aconn.set_autocommit(True)
cur = aconn.cursor()
await cur.execute("listen foo")
gen = aconn.notifies()
async for n in gen:
ns.append((n, time.time()))
if len(ns) >= 2:
await gen.aclose()
ns: List[Tuple[psycopg.Notify, float]] = []
t0 = time.time()
workers = [notifier(), receiver()]
await asyncio.gather(*workers)
assert len(ns) == 2
n, t1 = ns[0]
assert n.pid == npid
assert n.channel == "foo"
assert n.payload == "1"
assert t1 - t0 == pytest.approx(0.25, abs=0.05)
n, t1 = ns[1]
assert n.pid == npid
assert n.channel == "foo"
assert n.payload == "2"
assert t1 - t0 == pytest.approx(0.5, abs=0.05)
async def canceller(aconn, errors):
try:
await asyncio.sleep(0.5)
aconn.cancel()
except Exception as exc:
errors.append(exc)
@pytest.mark.slow
@pytest.mark.crdb_skip("cancel")
async def test_cancel(aconn):
async def worker():
cur = aconn.cursor()
with pytest.raises(e.QueryCanceled):
await cur.execute("select pg_sleep(2)")
errors: List[Exception] = []
workers = [worker(), canceller(aconn, errors)]
t0 = time.time()
await asyncio.gather(*workers)
t1 = time.time()
assert not errors
assert 0.0 < t1 - t0 < 1.0
# still working
await aconn.rollback()
cur = aconn.cursor()
await cur.execute("select 1")
assert await cur.fetchone() == (1,)
@pytest.mark.slow
@pytest.mark.crdb_skip("cancel")
async def test_cancel_stream(aconn):
async def worker():
cur = aconn.cursor()
with pytest.raises(e.QueryCanceled):
async for row in cur.stream("select pg_sleep(2)"):
pass
errors: List[Exception] = []
workers = [worker(), canceller(aconn, errors)]
t0 = time.time()
await asyncio.gather(*workers)
t1 = time.time()
assert not errors
assert 0.0 < t1 - t0 < 1.0
# still working
await aconn.rollback()
cur = aconn.cursor()
await cur.execute("select 1")
assert await cur.fetchone() == (1,)
@pytest.mark.slow
@pytest.mark.crdb_skip("pg_terminate_backend")
async def test_identify_closure(aconn_cls, dsn):
async def closer():
await asyncio.sleep(0.2)
await conn2.execute(
"select pg_terminate_backend(%s)", [aconn.pgconn.backend_pid]
)
aconn = await aconn_cls.connect(dsn)
conn2 = await aconn_cls.connect(dsn)
try:
t = create_task(closer())
t0 = time.time()
try:
with pytest.raises(psycopg.OperationalError):
await aconn.execute("select pg_sleep(1.0)")
t1 = time.time()
assert 0.2 < t1 - t0 < 0.4
finally:
await asyncio.gather(t)
finally:
await aconn.close()
await conn2.close()
@pytest.mark.slow
@pytest.mark.subprocess
@pytest.mark.skipif(
sys.platform == "win32", reason="don't know how to Ctrl-C on Windows"
)
@pytest.mark.crdb_skip("cancel")
async def test_ctrl_c(dsn):
script = f"""\
import signal
import asyncio
import psycopg
async def main():
ctrl_c = False
loop = asyncio.get_event_loop()
async with await psycopg.AsyncConnection.connect({dsn!r}) as conn:
loop.add_signal_handler(signal.SIGINT, conn.cancel)
cur = conn.cursor()
try:
await cur.execute("select pg_sleep(2)")
except psycopg.errors.QueryCanceled:
ctrl_c = True
assert ctrl_c, "ctrl-c not received"
assert (
conn.info.transaction_status == psycopg.pq.TransactionStatus.INERROR
), f"transaction status: {{conn.info.transaction_status!r}}"
await conn.rollback()
assert (
conn.info.transaction_status == psycopg.pq.TransactionStatus.IDLE
), f"transaction status: {{conn.info.transaction_status!r}}"
await cur.execute("select 1")
assert (await cur.fetchone()) == (1,)
asyncio.run(main())
"""
if sys.platform == "win32":
creationflags = sp.CREATE_NEW_PROCESS_GROUP
sig = signal.CTRL_C_EVENT
else:
creationflags = 0
sig = signal.SIGINT
proc = sp.Popen([sys.executable, "-s", "-c", script], creationflags=creationflags)
with pytest.raises(sp.TimeoutExpired):
outs, errs = proc.communicate(timeout=1)
proc.send_signal(sig)
proc.communicate()
assert proc.returncode == 0
|