Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
add port conflict detection for travis ci
Browse files Browse the repository at this point in the history
  • Loading branch information
lxyu committed Feb 16, 2016
1 parent 7071681 commit d8fb4c1
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions tests/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from __future__ import absolute_import

import contextlib
import os
import multiprocessing
import time
import tempfile
import os
import pickle
import random
import socket
import tempfile
import thriftpy
import time

try:
import dbm
Expand All @@ -33,6 +35,19 @@
_, db_file = tempfile.mkstemp()


def _get_port():
while True:
port = 20000 + random.randint(1, 9999)
for i in range(5):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', port))
if result == 0:
continue
else:
return port
PORT = _get_port()


class SampleTracker(TrackerBase):
def record(self, header, exception):
db = dbm.open(db_file, 'w')
Expand Down Expand Up @@ -60,7 +75,7 @@ def sleep(self, ms):

def remove(self, name):
person = addressbook.Person(name="mary")
with client(port=26098) as c:
with client(port=PORT) as c:
c.add(person)

return True
Expand All @@ -70,10 +85,10 @@ def get_phonenumbers(self, name, count):
addressbook.PhoneNumber(number='saf')]

def add(self, person):
with client(port=26099) as c:
with client(port=PORT + 1) as c:
c.get_phonenumbers("jane", 1)

with client(port=26099) as c:
with client(port=PORT + 1) as c:
c.ping()
return True

Expand Down Expand Up @@ -109,7 +124,7 @@ def handle(self, client):
otrans.close()


def gen_server(port=26029, tracker=tracker, processor=TTrackedProcessor):
def gen_server(port, tracker=tracker, processor=TTrackedProcessor):
args = [processor, addressbook.AddressBookService, Dispatcher()]
if tracker:
args.insert(1, tracker)
Expand All @@ -123,9 +138,9 @@ def gen_server(port=26029, tracker=tracker, processor=TTrackedProcessor):
return ps, server


@pytest.fixture
@pytest.fixture(scope="module")
def server(request):
ps, ser = gen_server()
ps, ser = gen_server(PORT)
time.sleep(0.15)

def fin():
Expand All @@ -135,9 +150,9 @@ def fin():
return ser


@pytest.fixture
@pytest.fixture(scope="module")
def server1(request):
ps, ser = gen_server(port=26098)
ps, ser = gen_server(PORT + 1)
time.sleep(0.15)

def fin():
Expand All @@ -147,9 +162,9 @@ def fin():
return ser


@pytest.fixture
@pytest.fixture(scope="module")
def server2(request):
ps, ser = gen_server(port=26099)
ps, ser = gen_server(PORT + 2)
time.sleep(0.15)

def fin():
Expand All @@ -159,9 +174,9 @@ def fin():
return ser


@pytest.fixture
@pytest.fixture(scope="module")
def not_tracked_server(request):
ps, ser = gen_server(port=26030, tracker=None, processor=TProcessor)
ps, ser = gen_server(PORT + 3, tracker=None, processor=TProcessor)
time.sleep(0.15)

def fin():
Expand All @@ -172,7 +187,7 @@ def fin():


@contextlib.contextmanager
def client(client_class=TTrackedClient, port=26029):
def client(client_class=TTrackedClient, port=PORT):
socket = TSocket("localhost", port)

try:
Expand Down Expand Up @@ -289,7 +304,7 @@ def test_not_tracked_client_tracked_server(server):


def test_tracked_client_not_tracked_server(not_tracked_server):
with client(port=26030) as c:
with client(port=PORT + 3) as c:
assert c._upgraded is False
c.ping()
c.hello("cat")
Expand Down

0 comments on commit d8fb4c1

Please sign in to comment.