Skip to content

Commit

Permalink
Consolidate all the open port seeking methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Tung committed Oct 17, 2017
1 parent 73420f2 commit 713fce9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
23 changes: 6 additions & 17 deletions dss/util/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from elasticsearch import RequestsHttpConnection, Elasticsearch
from requests_aws4auth import AWS4Auth

from . import networking


class AWSV4Sign(requests.auth.AuthBase):
"""
AWS V4 Request Signer for Requests.
Expand Down Expand Up @@ -44,24 +47,10 @@ class ElasticsearchServer:
def __init__(self, startup_timeout_seconds: int=60) -> None:
elasticsearch_binary = os.getenv("DSS_TEST_ES_PATH", "elasticsearch")
self.tempdir = tempfile.TemporaryDirectory()
while True:
port = random.randint(1024, 65535)
transport_port = random.randint(1024, 65535)

# try to check if the randomly assigned ports already have a running service.
conflict = False
for port_to_test in port, transport_port:
try:
sock = socket.create_connection(("localhost", port_to_test), 1)
sock.close()
# there's something there already.
conflict = True
break
except (ConnectionRefusedError, socket.timeout):
# ok this is an open port.
pass
if conflict:
continue
while True:
port = networking.unused_tcp_port()
transport_port = networking.unused_tcp_port()

proc = subprocess.Popen(
[
Expand Down
8 changes: 8 additions & 0 deletions dss/util/networking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import contextlib
import socket


def unused_tcp_port():
with contextlib.closing(socket.socket()) as sock:
sock.bind(('127.0.0.1', 0))
return sock.getsockname()[1]
12 changes: 3 additions & 9 deletions tests/infra/server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import chalice.config
import contextlib
import logging
import os
import socket
import threading
import types

import requests
from chalice.cli import CLIFactory
from chalice.local import LocalDevServer, ChaliceRequestHandler

from dss.util import networking


class SilentHandler(ChaliceRequestHandler):
"""
Expand All @@ -26,7 +26,7 @@ class ThreadedLocalServer(threading.Thread):
"""
def __init__(self):
super().__init__()
self._port = _unused_tcp_port()
self._port = networking.unused_tcp_port()
self._server = None
self._server_ready = threading.Event()
self._chalice_app = None
Expand Down Expand Up @@ -74,9 +74,3 @@ def result(path, **kwargs):
def shutdown(self):
if self._server is not None:
self._server.server.shutdown()


def _unused_tcp_port():
with contextlib.closing(socket.socket()) as sock:
sock.bind(('127.0.0.1', 0))
return sock.getsockname()[1]
16 changes: 2 additions & 14 deletions tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from dss.config import IndexSuffix
from dss.events.handlers.index import process_new_s3_indexable_object, process_new_gs_indexable_object, notify
from dss.hcablobstore import BundleMetadata, BundleFileMetadata, FileMetadata
from dss.util import create_blob_key, UrlBuilder
from dss.util import create_blob_key, networking, UrlBuilder
from dss.util.es import ElasticsearchClient, ElasticsearchServer
from tests import get_version
from tests.es import elasticsearch_delete_index
Expand Down Expand Up @@ -74,7 +74,7 @@ class ESInfo:

def setUpModule():
IndexSuffix.name = __name__.rsplit('.', 1)[-1]
HTTPInfo.port = findOpenPort()
HTTPInfo.port = networking.unused_tcp_port()
HTTPInfo.server = HTTPServer((HTTPInfo.address, HTTPInfo.port), PostTestHandler)
HTTPInfo.thread = threading.Thread(target=HTTPInfo.server.serve_forever)
HTTPInfo.thread.start()
Expand Down Expand Up @@ -565,17 +565,5 @@ def create_index_data(blobstore, bucket_name, manifest):
return index


def findOpenPort() -> int:
while True:
port = randint(1024, 65535)
try:
sock = socket.create_connection((HTTPInfo.address, port), 1)
sock.close()
# there's something there already.
except (ConnectionRefusedError, socket.timeout):
# ok this is an open port.
break
return port

if __name__ == "__main__":
unittest.main()

0 comments on commit 713fce9

Please sign in to comment.