44import socket
55
66import time
7+
8+ import requests
79from pkg_resources import resource_filename
810from 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
0 commit comments