I know that it can simulate SocketTimeoutException by using withFixedDelay, but what about ConnectionTimeoutException?
- 9,821
 - 11
 - 64
 - 77
 
- 2,448
 - 4
 - 23
 - 43
 
5 Answers
Yes it is possible to do this with WireMock by calling addDelayBeforeProcessingRequests(300) against the Java API or posting the following to http://<host>:<port>/__admin/socket-delay:
{ "milliseconds": 300 }
(Obviously replacing 300 with however many milliseconds you'd like to delay by)
- 3,471
 - 21
 - 14
 
- 
                    11addDelayBeforeProcessingRequests() doesn't seem to exist in 2.1.12. Is there an alternative? – heliotrope Sep 11 '16 at 20:34
 - 
                    what would be the json element for this ? – Vaneet Kataria Feb 24 '20 at 10:25
 - 
                    5For those who are looking for those methods like me: This method was removed in version 2.x without any replacement. See https://groups.google.com/d/msg/wiremock-user/KEoBPIohVKU/Rm6lTOPQAAAJ – Walery Strauch Oct 07 '20 at 12:22
 
It seems that the answer to this question has been "No", since version 2.0.8-beta.
Tom (author of WireMock) explains why in this GitHub issue:
It's basically impossible to reliably force connection timeouts in pure Java at the moment.
It used to be the case that you could inject a delay before calling
.accept()on the socket, but that stopped working a while back, I guess due to a change in the implementation internals.My recommendation at the moment would be to use a tool that works at the level of the network stack.
iptables ... -j DROPtype commands will do the trick, or if you want a level of automation over this you can use tools such as https://github.com/tomakehurst/saboteur or https://github.com/alexei-led/pumba.
He also goes on to explain that just stopping WireMock doesn't achieve the same thing:
shutting down WireMock won't have the same effect - when a port is not being listened on, you get a TCP
RST(reset) packet back, whereas a connection timeout happens when you get nothing back from the server in the timeout window after your initialSYNpacket.
- 1
 - 1
 
- 9,821
 - 11
 - 64
 - 77
 
Checkout https://github.com/tomakehurst/saboteur which allows you to simulate network issues. Or you can do it your self with iptables.
- 674
 - 4
 - 7
 
When using WireMock.Net, adding a delay is also possible.
Example:
var server = WireMockServer.Start();
// add a delay of 30 seconds for all requests
server.AddRequestProcessingDelay(TimeSpan.FromSeconds(30));
or
var server = WireMockServer.Start();
server
  .Given(Request.Create().WithPath("/slow"))
  .RespondWith(
    Responses.Create()
      .WithStatusCode(200)
      .WithBody(@"{ ""msg"": ""Hello I'm a little bit slow!"" }")
      .WithDelay(TimeSpan.FromSeconds(10)
  )
);
- 9,335
 - 12
 - 66
 - 121
 
The following code mock a get call to /service-url where it returns only after the duration specified.
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import java.time.Duration;
import static java.lang.Math.toIntExact;
void mockServiceUrlWithDelay(Duration delay) throws JSONException {
        ResponseDefinitionBuilder jsonResponse = aResponse()
                .withStatus(200)
                .withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
                .withBody(new JSONObject() 
                        .put("key", "value") 
                        .toString())
                .withFixedDelay(toIntExact(delay.toMillis()));
        stubFor(get(urlPathEqualTo("/service-url")).willReturn(jsonResponse));
    }
- 4,335
 - 34
 - 27