简体   繁体   English

是否可以使用wiremock工具模拟连接超时?

[英]Is it possible to simulate connection timeout using wiremock tools?

我知道它可以通过使用withFixedDelay来模拟SocketTimeoutException ,但是ConnectionTimeoutException呢?

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 :是的,可以通过针对 Java API 调用addDelayBeforeProcessingRequests(300)或将以下内容发布到http://<host>:<port>/__admin/socket-delay来使用 WireMock 执行此操作:

{ "milliseconds": 300 }

(Obviously replacing 300 with however many milliseconds you'd like to delay by) (显然,用您想要延迟的毫秒数替换 300)

It seems that the answer to this question has been "No", since version 2.0.8-beta .2.0.8-beta 版本开始,这个问题的答案似乎是否定的。

Tom (author of WireMock) explains why in this GitHub issue : Tom(WireMock 的作者)在这个 GitHub 问题中解释了原因:

It's basically impossible to reliably force connection timeouts in pure Java at the moment.目前基本上不可能在纯 Java 中可靠地强制连接超时。

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.过去的情况是,您可以在对套接字调用.accept()之前注入延迟,但一段时间后停止工作,我猜是由于实现内部的变化。

My recommendation at the moment would be to use a tool that works at the level of the network stack.我目前的建议是使用在网络堆栈级别工作的工具。 iptables ... -j DROP type 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 . iptables ... -j DROP类型的命令可以解决这个问题,或者如果你想要一个自动化程度,你可以使用诸如https://github.com/tomakehurst/saboteurhttps://github.com/ 之类的工具阿列克谢领导的/彭巴

He also goes on to explain that just stopping WireMock doesn't achieve the same thing:他还继续解释说,仅仅停止 WireMock 并不能达到同样的目的:

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 initial SYN packet.关闭 WireMock 不会有同样的效果——当一个端口没有被监听时,你会得到一个 TCP RST (重置)数据包,而当你在超时窗口中没有从服务器得到任何返回时,连接超时发生初始SYN数据包。

Checkout https://github.com/tomakehurst/saboteur which allows you to simulate network issues.查看https://github.com/tomakehurst/saboteur ,它允许您模拟网络问题。 Or you can do it your self with iptables.或者你可以用 iptables 自己做。

java.net.ConnectException essentially means that we can not establish network connection to the intended party. java.net.ConnectException本质上意味着我们无法与目标方建立网络连接。 Considering that, I just stopped my wiremock instance and it worked fine. 考虑到这一点,我刚刚停止了我的wiremock实例,它工作正常。

The implementation of my third party api call catches this exception and re-throws HTTP 500 with the error message(that is what i am trying to assert) 我的第三方api调用的实现捕获此异常并重新抛出HTTP 500错误消息(这是我试图断言)

    @Before
    public void setUp() {
        wireMock.start();
    }

    @After
    public void clean() {
        wireMock.resetMappings();
    }

    @Test
    public void shouldSimulateConnectionTimeout() {
        //given - STOP THE WIREMOCK
        wireMock.stop();

        //when
        Response response = getTarget("external api URL")
                             .request()
                             .post(entity(event, APPLICATION_JSON_TYPE));

        //then
        assertEquals(500, response.getStatus());
        assertEquals("{\"message\" : \"java.net.ConnectException: Connection refused\"}", response.readEntity(String.class));
    }

When using WireMock.Net , adding a delay is also possible.使用WireMock.Net 时,也可以添加延迟。

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)
  )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM