Skip to content

Commit

Permalink
switch to urllib requesting to see if port open
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Jun 24, 2021
1 parent 30c6e39 commit 20e593a
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions sdk/core/azure-core/tests/testserver_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,20 @@
import subprocess
import sys
import random
try:
import http.client as httpclient
except ImportError:
import httplib as httpclient
from six.moves import urllib

def is_port_open(port_num):
conn = httpclient.HTTPSConnection("localhost:{}".format(port_num))
def is_port_available(port_num):
req = urllib.request.Request("http://localhost:{}/health".format(port_num))
try:
conn.request("GET", "/health")
return False
return urllib.request.urlopen(req).code != 200
except Exception as e:
return True

def get_port():
count = 3
for _ in range(count):
port_num = random.randrange(3000, 5000)
if is_port_open(port_num):
if is_port_available(port_num):
return port_num
raise TypeError("Tried {} times, can't find an open port".format(count))

Expand All @@ -65,10 +61,12 @@ def start_testserver():
else:
#On linux, have to set shell=True
child_process = subprocess.Popen(cmd, shell=True, preexec_fn=os.setsid, env=dict(os.environ))
time.sleep(2)
if not child_process.returncode is None:
raise ValueError("Didn't start!")
return child_process
count = 5
for _ in range(count):
if not is_port_available(port):
return child_process
time.sleep(1)
raise ValueError("Didn't start!")

def terminate_testserver(process):
if os.name == 'nt':
Expand Down

0 comments on commit 20e593a

Please sign in to comment.