python-wiremock icon indicating copy to clipboard operation
python-wiremock copied to clipboard

Random port assignment erroring during concurrency

Open andrewkruse opened this issue 2 years ago • 1 comments
trafficstars

Proposal

When a bunch of stuff is going on at the same time on a computer, the random port assignment fails because it is being taken. Instead of using python to find a random port and then launching the java server on it, perhaps make the java server bind a random port and pull that back up.

The lines in question: https://github.com/wiremock/python-wiremock/blob/master/wiremock/server/server.py#L116-L121

My work around is essentially putting that _get_free_port in a loop until something succeeds...

Reproduction steps

On GitHub actions I have two pipelines that run concurrently with Wiremock. The port assignment fails -- it's already being reserved by something else by the time the java server comes up and runs.

References

My resolution in my testing looks something like this. Essentially try again for a bit so that two people standing it up at once don't fail.

@pytest.fixture()
def wiremock():
    initialized = False
    attempts = 0
    while attempts < 10 and not initialized:
        try:
            s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
            s.bind(("localhost", 0))
            address, port = s.getsockname()
            s.close()

            with WireMockServer(port=port) as wm:
                Config.base_url = 'http://localhost:{}/__admin'.format(wm.port)
                initialized = True
                yield wm
        except exceptions.WireMockServerNotStartedError as e:
            logging.error(e)
        attempts += 1

andrewkruse avatar Jun 02 '23 14:06 andrewkruse

@andrewkruse thanks for raising this issue. I think this could be supported natively within the WireMockServer class, we'd be more than happy to take a PR if you fancy having a go. Otherwise I will try and come to this soon. We are working on a bunch of new features to support different strategies for running the wiremock server like (https://github.com/wiremock/python-wiremock/pull/71) so it might be a little while before I get to it.

mikeywaites avatar Jun 11 '23 19:06 mikeywaites