mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.61k stars 1.08k forks source link

Starting a new MockServer in another test with the same port fails #1097

Closed rafaelcn closed 2 years ago

rafaelcn commented 3 years ago

Describe the issue Whenever I use a MockServer with the same port (necessary, I cannot do using a random port for each unit test) in two or more unit tests the second test to be executed fails. It fails because the first one hasn't stopped the first server using the following procedure:

@AfterClass
public static void stopServer() {
  mockServer.stop();

  var stopped = mockServer.hasStopped(50, 5000, TimeUnit.MILLISECONDS);

  while (!stopped) {
      mockServer.stop();
      stopped = mockServer.hasStopped(50, 5000, TimeUnit.MILLISECONDS);
  }
}

(This issue has some resemblance with this one: https://github.com/mock-server/mockserver/issues/498)

What you are trying to do Trying to write lots of unit tests that on every and each one of them has a mock server using the same port.

MockServer version 5.11.2

To Reproduce

  1. Create two unit tests
  2. Create a MockServer in each one of them with the same port
  3. Use the annotation @AfterClass with the code provided in the description of this issue
  4. Test the entire project and see that the second test to execute will fail because it can't bind another server with the same address even after it apparently stops the server

Expected behaviour The server stop after the class is finished executing and other tests to be executed with no problems.

MockServer Log Couldn't gather the MockServer log.

jamesdbloom commented 2 years ago

Can you please explain more how you are starting MockServer and provide some example code then I should be able to get to the bottom of the problem.

rafaelcn commented 2 years ago

@jamesdbloom, is it still necessary? I was offline when you asked me, but I could still provide the code. Apart from that, I will test the new release to see if it works.

jamesdbloom commented 2 years ago

@rafaelcn I think I have fixed the issue in the SNAPSHOT version and added more tests around this so if you still experience the issue please raise a new issue and reference this one.

I will release the SNAPSHOT in the next week or so.

jpal commented 6 months ago

I have the same problem (version 5.15.0), I have two tests and the second test fails unless I sleep for two seconds in the beginning of the test. On the second test it logs: MockServerEventLog - 9000 started on port: 9000 MockServerEventLog - 9000 creating expectation: ... (I call the server) MockServerEventLog - stopped for port: 9000 (I haven't called mockServer.stop()) (I call the server again during the same test) org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:9000/test/Add": An established connection was aborted by the software in your host machine

The first test is similar but it works through the two calls I make to the (mock)server. I call mockServer.stop(true) after each test.

I tried to make minimal two tests that would replicate this, but they worked fine. Here's the setup for each test (Kotlin):

        mockServer = ClientAndServer.startClientAndServer(9000)
        mockServer!!.`when`(
            HttpRequest.request()
                .withMethod("POST")
        )
            .respond(
                HttpClassCallback.callback().withCallbackClass(RejectAllResponseCallback::class.java)
            )