Skip to content

Commit b329d02

Browse files
committed
tests: Workaround connection error race under Windows.
Appveyor test runs periodically fail with ConnectionDoesNotExist instead of the actual connection error. Try to mitigate that.
1 parent 71de129 commit b329d02

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

tests/test_connect.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,25 @@ def tearDown(self):
121121

122122
super().tearDown()
123123

124+
async def _try_connect(self, **kwargs):
125+
# On Windows the server sometimes just closes
126+
# the connection sooner than we receive the
127+
# actual error.
128+
if _system == 'Windows':
129+
for tried in range(3):
130+
try:
131+
return await self.cluster.connect(**kwargs)
132+
except asyncpg.ConnectionDoesNotExistError:
133+
pass
134+
135+
return await self.cluster.connect(**kwargs)
136+
124137
async def test_auth_bad_user(self):
125138
with self.assertRaises(
126139
asyncpg.InvalidAuthorizationSpecificationError):
127-
await self.cluster.connect(user='__nonexistent__',
128-
database='postgres',
129-
loop=self.loop)
140+
await self._try_connect(user='__nonexistent__',
141+
database='postgres',
142+
loop=self.loop)
130143

131144
async def test_auth_trust(self):
132145
conn = await self.cluster.connect(
@@ -137,21 +150,9 @@ async def test_auth_reject(self):
137150
with self.assertRaisesRegex(
138151
asyncpg.InvalidAuthorizationSpecificationError,
139152
'pg_hba.conf rejects connection'):
140-
for tried in range(3):
141-
try:
142-
await self.cluster.connect(
143-
user='reject_user', database='postgres',
144-
loop=self.loop)
145-
except asyncpg.ConnectionDoesNotExistError:
146-
if _system == 'Windows':
147-
# On Windows the server sometimes just closes
148-
# the connection sooner than we receive the
149-
# actual error.
150-
continue
151-
else:
152-
raise
153-
else:
154-
break
153+
await self._try_connect(
154+
user='reject_user', database='postgres',
155+
loop=self.loop)
155156

156157
async def test_auth_password_cleartext(self):
157158
conn = await self.cluster.connect(
@@ -162,7 +163,7 @@ async def test_auth_password_cleartext(self):
162163
with self.assertRaisesRegex(
163164
asyncpg.InvalidPasswordError,
164165
'password authentication failed for user "password_user"'):
165-
await self.cluster.connect(
166+
await self._try_connect(
166167
user='password_user', database='postgres',
167168
password='wrongpassword', loop=self.loop)
168169

@@ -175,7 +176,7 @@ async def test_auth_password_md5(self):
175176
with self.assertRaisesRegex(
176177
asyncpg.InvalidPasswordError,
177178
'password authentication failed for user "md5_user"'):
178-
await self.cluster.connect(
179+
await self._try_connect(
179180
user='md5_user', database='postgres', password='wrongpassword',
180181
loop=self.loop)
181182

0 commit comments

Comments
 (0)