@@ -121,12 +121,25 @@ def tearDown(self):
121
121
122
122
super ().tearDown ()
123
123
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
+
124
137
async def test_auth_bad_user (self ):
125
138
with self .assertRaises (
126
139
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 )
130
143
131
144
async def test_auth_trust (self ):
132
145
conn = await self .cluster .connect (
@@ -137,21 +150,9 @@ async def test_auth_reject(self):
137
150
with self .assertRaisesRegex (
138
151
asyncpg .InvalidAuthorizationSpecificationError ,
139
152
'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 )
155
156
156
157
async def test_auth_password_cleartext (self ):
157
158
conn = await self .cluster .connect (
@@ -162,7 +163,7 @@ async def test_auth_password_cleartext(self):
162
163
with self .assertRaisesRegex (
163
164
asyncpg .InvalidPasswordError ,
164
165
'password authentication failed for user "password_user"' ):
165
- await self .cluster . connect (
166
+ await self ._try_connect (
166
167
user = 'password_user' , database = 'postgres' ,
167
168
password = 'wrongpassword' , loop = self .loop )
168
169
@@ -175,7 +176,7 @@ async def test_auth_password_md5(self):
175
176
with self .assertRaisesRegex (
176
177
asyncpg .InvalidPasswordError ,
177
178
'password authentication failed for user "md5_user"' ):
178
- await self .cluster . connect (
179
+ await self ._try_connect (
179
180
user = 'md5_user' , database = 'postgres' , password = 'wrongpassword' ,
180
181
loop = self .loop )
181
182
0 commit comments