Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mock server refusing connections #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pactman/mock/mock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import queue
import traceback
from http.server import BaseHTTPRequestHandler, HTTPServer
from multiprocessing import Process, Queue
from multiprocessing import Process, Queue, SimpleQueue

from .pact_request_handler import PactRequestHandler

Expand All @@ -23,8 +23,10 @@ def __init__(self, pact):
self.pact = pact
self.interactions = Queue()
self.results = Queue()
self.process = Process(target=run_server, args=(pact, self.interactions, self.results))
notify_start = SimpleQueue()
self.process = Process(target=run_server, args=(pact, self.interactions, self.results, notify_start))
self.process.start()
notify_start.get()

def setup(self, interactions):
for interaction in interactions:
Expand All @@ -42,16 +44,17 @@ def terminate(self):
self.process.terminate()


def run_server(pact, interactions, results):
httpd = MockServer(pact, interactions, results)
def run_server(pact, interactions, results, notify_start):
httpd = MockServer(pact, interactions, results, notify_start)
httpd.serve_forever()


class MockServer(HTTPServer):
def __init__(self, pact, interactions, results):
def __init__(self, pact, interactions, results, notify_start):
self.pact = pact
self.incoming_interactions = interactions
self.outgoing_results = results
self.notify_start = notify_start
server_address = ("", pact.port)
super().__init__(server_address, MockHTTPRequestHandler)
self.interactions = []
Expand All @@ -60,6 +63,11 @@ def __init__(self, pact, interactions, results):
self.log.setLevel(logging.DEBUG)
self.log.propagate = False

def server_activate(self):
super().server_activate()
self.notify_start.put(True)
del self.notify_start

class Error(Exception):
pass

Expand Down
36 changes: 36 additions & 0 deletions pactman/test/test_mock_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import socket
from queue import Queue
from unittest.mock import Mock

Expand All @@ -21,8 +22,43 @@ def queue(*a):
)
def test_correct_result_assertion(mocker, results, exception):
mocker.patch("pactman.mock.mock_server.Process", autospec=True)
mocker.patch("pactman.mock.mock_server.SimpleQueue", autospec=True)
s = mock_server.Server(Mock())
s.results = results
with pytest.raises(exception) as e:
s.verify()
assert "spam" in str(e.value)


@pytest.fixture
def unused_port():
with socket.socket() as s:
s.bind(("localhost", 0))
_, port = s.getsockname()[:2]
return port


@pytest.fixture
def a_mock_server(tmpdir, unused_port):
from pactman import Consumer, Provider

pact = Consumer("consumer").has_pact_with(
Provider("provider"),
pact_dir=str(tmpdir),
log_dir=str(tmpdir),
host_name="localhost",
port=unused_port,
)

server = mock_server.Server(pact)
yield server
server.terminate()


def test_mockserver_is_connectable(a_mock_server):

pact = a_mock_server.pact
with socket.socket() as s:
# Will fail with ConnectionRefusedError if the server is not already
# bound and listening
s.connect((pact.host_name, pact.port))