Skip to content

Commit

Permalink
try harder to allocate unbound port
Browse files Browse the repository at this point in the history
  • Loading branch information
leifj committed Jun 30, 2015
1 parent 3fc595a commit e2154a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/pyff/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def run_cmdline(script, *args):
sys.exit(rv)
""" % (sys.executable, pyffversion, script))
print starter
os.chmod(starter, 0700)

argv.insert(0, starter)
Expand Down
25 changes: 22 additions & 3 deletions src/pyff/test/test_md_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
from pyff import __version__ as pyffversion
from pyff.utils import parse_xml, root, validate_document

import random
import socket

# range of ports where available ports can be found
PORT_RANGE = [33000,60000]
MAX_PORT_TRIES = 100

def find_unbound_port(i=0):
"""
Returns an unbound port number on 127.0.0.1.
"""
if i > MAX_PORT_TRIES:
raise ValueError("Unable to find an unused port after %d tries" % i)
port = random.randint(*PORT_RANGE)
print port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(("127.0.0.1", port))
except socket.error:
port = find_unbound_port(i+1)
return port

class PyFFDTest(PipeLineTest):
"""
Expand All @@ -43,7 +64,7 @@ def setUpClass(cls):
with open(cls.mdx, "w") as fd:
fd.write(cls.mdx_template.render(ctx=cls))
cls.curdir = os.getcwd()
cls.port = random.randrange(50000, 60000)
cls.port = find_unbound_port()
cls.pyffd_thread = Thread(target=run_pyffd,
name="pyffd-test",
args=["--loglevel=DEBUG",
Expand All @@ -55,7 +76,6 @@ def setUpClass(cls):
'-p', cls.pidfile,
"--terminator",
cls.mdx])
print cls.pyffd_thread
cls.pyffd_thread.start()
sleep(1)
for i in range(0,10):
Expand Down Expand Up @@ -90,7 +110,6 @@ def test_stats(self):

def test_alias_ndn(self):
r = requests.get("http://127.0.0.1:%s/ndn.xml" % self.port)
print r
assert (r.status_code == 200)
#assert (r.encoding == 'utf8')
t = parse_xml(StringIO(r.content))
Expand Down

0 comments on commit e2154a5

Please sign in to comment.