File tree 1 file changed +13
-2
lines changed
1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -251,14 +251,25 @@ def instance(self):
251
251
CLIENT_SECRETS_INFO , scopes = self .SCOPES
252
252
)
253
253
254
+ def is_port_in_use (self , port , host = "localhost" ):
255
+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
256
+ return s .connect_ex ((host , port )) == 0
257
+
254
258
@pytest .fixture
255
259
def port (self ):
256
260
# Creating a new server at the same port will result in
257
261
# a 'Address already in use' error for a brief
258
262
# period of time after the socket has been closed.
259
- # Work around this in the tests by choosing a random port.
263
+ # Work around this in the tests by choosing a different port each time .
260
264
# https://stackoverflow.com/questions/6380057/python-binding-socket-address-already-in-use
261
- yield random .randrange (60400 , 60900 )
265
+ random_port = - 1
266
+ for _ in range (10 ):
267
+ random_port = random .randrange (60400 , 60900 )
268
+ if not self .is_port_in_use (random_port ):
269
+ break
270
+ else :
271
+ raise OSError ("Could not find a free port" )
272
+ yield random_port
262
273
263
274
@pytest .fixture
264
275
def socket (self , port ):
You can’t perform that action at this time.
0 commit comments