Skip to content

Commit f70bf73

Browse files
Merge pull request #19 from platinummonkey/issue-15
addresses #15 by adding a check for wiremock health status
2 parents 950182e + ec5c923 commit f70bf73

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

wiremock/server/server.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import socket
55

66
import time
7+
8+
import requests
79
from pkg_resources import resource_filename
810
from subprocess import Popen, PIPE, STDOUT
911

@@ -15,12 +17,13 @@ class WireMockServer(object):
1517
DEFAULT_JAVA = "java" # Assume java in PATH
1618
DEFAULT_JAR = resource_filename("wiremock", "server/wiremock-standalone-2.6.0.jar")
1719

18-
def __init__(self, java_path=DEFAULT_JAVA, jar_path=DEFAULT_JAR):
20+
def __init__(self, java_path=DEFAULT_JAVA, jar_path=DEFAULT_JAR, port=None, max_attempts=10):
1921
self.java_path = java_path
2022
self.jar_path = jar_path
21-
self.port = self._get_free_port()
23+
self.port = port or self._get_free_port()
2224
self.__subprocess = None
2325
self.__running = False
26+
self.max_attempts = max_attempts
2427

2528
def __enter__(self):
2629
self.start()
@@ -50,6 +53,20 @@ def start(self):
5053
"\n".join(["returncode: {}".format(self.__subprocess.returncode), "stdout:", str(self.__subprocess.stdout.read())])
5154
)
5255

56+
# Call the /__admin endpoint as a check for running state
57+
attempts = 0
58+
success = False
59+
while attempts < self.max_attempts:
60+
attempts += 1
61+
resp = requests.get("http://localhost:{}/__admin".format(self.port))
62+
if resp.status_code == 200:
63+
success = True
64+
break
65+
time.sleep(0.25)
66+
67+
if not success:
68+
raise WireMockServerNotStartedError("unable to get a successful GET http://localhost:{}/__admin response".format(self.port))
69+
5370
atexit.register(self.stop, raise_on_error=False)
5471
self.__running = True
5572

wiremock/tests/server_tests/server_tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
from subprocess import STDOUT, PIPE
66

7+
import responses
78
from mock import patch, DEFAULT
89
from pkg_resources import resource_filename
910

@@ -56,7 +57,11 @@ def test_get_free_port(self, mock_socket):
5657
@attr("unit", "server")
5758
@patch("wiremock.server.server.atexit")
5859
@patch("wiremock.server.server.Popen")
60+
@responses.activate
5961
def test_start(self, Popen, atexit):
62+
# mock healthy endpoint
63+
responses.add(responses.GET, "http://localhost:{}/__admin".format(self.wm.port), json=[], status=200)
64+
6065
def poll():
6166
Popen.return_value.returncode = None
6267
return None

0 commit comments

Comments
 (0)