Wiremock returns 503 when you stop and start the server

1,810 views
Skip to first unread message

Balajee Mohan

unread,
Jul 25, 2017, 10:55:37 PM7/25/17
to wiremock-user
Hi All,

I'm using wiremock to write BDD tests.
In some of the tests, I stop the wiremock server to test a service unavailable scenario.
Soon after the test, I start the server using WireMockServer.start()

I check that start and stop methods are synchronous and return only after the server has been started or stopped respectively.
In start especially, I can see a Thread.sleep for 100 milliseconds.

However, I still see some test cases intermittently failing because of a 503 error from wiremock.
Can someone tell me why this is happening?

Regards,
Balajee

Balajee Mohan

unread,
Jul 26, 2017, 4:06:57 AM7/26/17
to wiremock-user
An update.

Wiremock didn't actually return a 503 but it took too long to respond.
The default timeout is 30 seconds. It took longer than 30 seconds to respond.
Any idea why this is happening when you stop and start the server?

Tom Akehurst

unread,
Jul 26, 2017, 5:01:33 AM7/26/17
to wiremock-user
It seems like there's some missing information here. A timeout wouldn't cause a 503 (or any status code to be returned at all).

Can you post the code that replicates the fault?

Balajee Mohan

unread,
Jul 26, 2017, 10:47:11 PM7/26/17
to wiremock-user
Hi Tom,

The scenario is as below:

* The system under test (SUT) is a rest server owning some resource
* An api is exposed on the rest server to update the resource
* Before updating the resource, the server must authenticate the user making the request
* Wiremock is used to mock the authentication server (which again exposes Rest apis)
* There is a BDD test to validate the behavior when the dependent service is down
* At the start of the test (JUnit Before), the wiremock server is stopped using Wiremockserver.stop() -> This call seems to be synchronous as it does a JettyServer.stop and also join waiting for all Threads in Threadloop to shutdown to ensure no more incoming connections are entertained
* At the end of the test the server is started again, using mockserver.start()
* In the test, the behavior is checked by invoking the update api using Spring's RestTemplate
* When the api is invoked, the service internal makes a call to the dependent server and in this case, intermittently, though the server is down, the call to authentication server is never returned
* the client (resttemplate of unit test in this case), does not receive any response from the server for 30 seconds and thus returns a 503 - stating service unavailable

Tom Akehurst

unread,
Jul 27, 2017, 7:34:23 AM7/27/17
to wiremock-user
Why are you stopping the WireMock server before the test starts? Seems like starting it would make more sense here.

Balajee Mohan

unread,
Jul 28, 2017, 12:37:08 AM7/28/17
to wiremock-user
The test scenario is to test the behavior when a dependent service is down. The dependent service is being stubbed using wiremock. So before the test begins, the mockserver is stopped.
Nonetheless, why is the call to the mock service not returning after stopping it? (even though stop is a synchronous call)

Tom Akehurst

unread,
Jul 28, 2017, 6:45:45 AM7/28/17
to wiremock-user
Without a runnable test case to replicate the problem I can only speculate.

Where I've seen this issue in the past it's often been because the HTTP client connection pool isn't doing stale checking, so it is hanging when pooled connections are closed by the server, as happens on stop().

Balajee Mohan

unread,
Jul 31, 2017, 11:02:27 PM7/31/17
to wiremock-user
I figured the issue Tom.

It was because of connection pooling.
We were using rest template with HttpComponentsClientHttpRequestFactory that was pooling the connections.
So it was not performing the three way handshake when using the pooled connections.
Without the handshake, the thread presumes a non existent server to be available and keeps waiting for it to response.
The client that initiated the request doesn't wait beyond the time out and returns a 503.

Thanks a ton & have fun.
And a suggestion.
Can you please improve the java docs of the code. Some methods do not have any comments on top of them and sometimes thats hard to understand.

Cheers,
Reply all
Reply to author
Forward
0 new messages