Skip to content

Commit 6d51c37

Browse files
committed
python: exercise reconnect with a real broken psycopg connection
1 parent 050fe9d commit 6d51c37

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

sdks/python/tests/test_connection_defaults.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import asyncio
22

3+
import psycopg
4+
from psycopg import pq
5+
36
import absurd_sdk
47

58

@@ -49,29 +52,22 @@ def test_async_absurd_falls_back_to_default_absurd_uri(monkeypatch):
4952
assert client._owned_conn is True
5053

5154

52-
class MockConnection:
53-
broken = False
54-
55-
async def close(self):
56-
pass
57-
58-
59-
def test_async_absurd_reconnects_only_after_an_interrupted_connection(monkeypatch):
55+
def test_async_absurd_reconnects_after_an_interrupted_connection(monkeypatch):
6056
made = []
6157

6258
async def connect(dsn, autocommit=True):
63-
made.append(MockConnection())
59+
# Connecting to a nonexistent socket fails immediately, producing a
60+
# real psycopg connection that is `broken`: status BAD without a
61+
# clean close(), the same state a dropped connection leaves behind.
62+
made.append(psycopg.AsyncConnection(pq.PGconn.connect(b"host=/nonexistent")))
6463
return made[-1]
6564

6665
monkeypatch.setattr(absurd_sdk.AsyncConnection, "connect", connect)
6766
client = absurd_sdk.AsyncAbsurd("postgresql://localhost/absurd")
6867

6968
async def run():
7069
await client._ensure_connected()
71-
made[0].broken = True
72-
await client._ensure_connected() # interrupted: replaced
73-
await client.close()
74-
await client._ensure_connected() # closed cleanly: not resurrected
70+
await client._ensure_connected()
7571

7672
asyncio.run(run())
7773

0 commit comments

Comments
 (0)