简体   繁体   English

WireMock 有时表现得很奇怪

[英]WireMock behaves weird sometime

In most of the integration tests I'm using spring-boot-test(2.1.9.RELEASE) and spring-cloud-contract-wiremock(2.0.2.RELEASE).在大多数集成测试中,我使用 spring-boot-test(2.1.9.RELEASE) 和 spring-cloud-contract-wiremock(2.0.2.RELEASE)。 The test is starting up WireMock server based on : @AutoConfigureWireMock(port = 0), so I'm not using any WireMockRule or other configuration set-up.该测试基于以下内容启动 WireMock 服务器:@AutoConfigureWireMock(port = 0),因此我没有使用任何 WireMockRule 或其他配置设置。

Sometime the verifying is failing with a really weird error:有时验证失败并出现一个非常奇怪的错误:

com.github.tomakehurst.wiremock.client.VerificationException:` com.github.tomakehurst.wiremock.client.VerificationException: com.github.tomakehurst.wiremock.client.VerificationException: No requests exactly matched. com.github.tomakehurst.wiremock.client.VerificationException:` com.github.tomakehurst.wiremock.client.VerificationException: com.github.tomakehurst.wiremock.client.VerificationException: 没有完全匹配的请求。 Most similar request was: expected:< POST /api/id/delete最相似的请求是:预期:< POST /api/id/delete

but was:< POST /api/id/delete但是是:< POST /api/id/delete

As you can see above the expected endpoint is exactly the same with the actual invocation.正如您在上面看到的,预期端点与实际调用完全相同。

Do you have any ideas ?你有什么想法 ? or Have you seen that before ?或者你以前见过吗? There is an open issue here: https://github.com/tomakehurst/wiremock/issues/706 , but the responses are not very helpful.这里有一个悬而未决的问题: https : //github.com/tomakehurst/wiremock/issues/706 ,但回复不是很有帮助。

I have the same issue on the DELETE, but on local it's working (windows + intelliJ) and on Jenkins (linux) fail.我在 DELETE 上有同样的问题,但在本地它正在工作(windows + intelliJ)和在 Jenkins(linux)上失败。 And you ?你呢 ?

com.github.tomakehurst.wiremock.client.VerificationException: 
No requests exactly matched. Most similar request was:  expected:<
DELETE
/myAPI/api
> but was:<
DELETE
/myAPI/api
>

Edit:编辑:

Solution: I have an asynchronous method in my algorithm and I don't need to wait for it to answer to finish the algo so I have to put a Thread.sleep to be sure that the call is done解决方案:我的算法中有一个异步方法,我不需要等待它回答来完成算法,所以我必须放置一个 Thread.sleep 以确保调用完成

    /**
     * Use It when you have a asyc call in your methode
     * @param delay time to wait
     */
    public void waitingForAsycRequest(int delay) {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }

So after months of randomly failing builds, it seems that the only solution was to wait until the stubs got registered.因此,经过几个月的随机失败构建,似乎唯一的解决方案是等到存根注册。 So the new verify wrapper method looks like this:所以新的验证包装器方法如下所示:

    public static void verify(int count, RequestPatternBuilder requestPatternBuilder) {
    int maxRetries = 5;
    while (count != WireMock.findAll(requestPatternBuilder)
            .size() && maxRetries > 0) {
        Thread.sleep(1000);
        maxRetries--;
    }
    WireMock.verify(count, requestPatternBuilder);
}

And the callers used it like this:调用者是这样使用它的:

        WireMockHelper.verify(1, putRequestedFor(urlMatching((URL.BASE_URL.ACTION).replace("%s", ".*"))));

Finally now we can rely on our IT build pipeline.现在终于可以依靠我们的 IT 构建管道了。 Wish you all only green builds :)祝你们都只有绿色建筑:)

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

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