From d2f0f6322ed189003f5c23d90eefd39eb227e3a0 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 10 Aug 2018 14:35:52 +0200 Subject: [PATCH] Added comments related to the workaround with Java HTTP client; thanks to @tomakehurst --- .../wiremock/StubRunnerWireMockTestExecutionListener.java | 8 ++++++++ .../cloud/contract/wiremock/WireMockConfiguration.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/StubRunnerWireMockTestExecutionListener.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/StubRunnerWireMockTestExecutionListener.java index b29e451bf7..7bd3005ffa 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/StubRunnerWireMockTestExecutionListener.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/StubRunnerWireMockTestExecutionListener.java @@ -45,6 +45,14 @@ public final class StubRunnerWireMockTestExecutionListener extends AbstractTestE } entry.getKey().start(entry.getValue().port); entry.getKey().registerDescriptors(mappings); + /* + Thanks to Tom Akehurst: + I looked at tcpdump while running the failing test. HttpUrlConnection is doing something weird - it's creating a connection in a + previous test case, which works fine, then the usual fin -> fin ack etc. etc. ending handshake happens. But it seems it + isn't discarded, but reused after that. Because the server thinks (rightly) that the connection is closed, it just sends a RST packet. + Calling the admin endpoint just happened to remove the dead connection from the pool. + This also fixes the problem (which using the Java HTTP client): System.setProperty("http.keepAlive", "false"); + */ Assert.isTrue(new RestTemplate().getForEntity("http://localhost:" + entry.getValue().port + "/__admin/mappings", String.class) .getStatusCode().is2xxSuccessful(), "__admin/mappings endpoint wasn't accessible"); } diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java index 457f058634..25a5f2454f 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java @@ -162,6 +162,14 @@ public class WireMockConfiguration implements SmartLifecycle { if (log.isDebugEnabled()) { log.debug("Started WireMock at port [" + this.server.port() + "]. It has [" + this.server.getStubMappings().size() + "] mappings registered"); } + /* + Thanks to Tom Akehurst: + I looked at tcpdump while running the failing test. HttpUrlConnection is doing something weird - it's creating a connection in a + previous test case, which works fine, then the usual fin -> fin ack etc. etc. ending handshake happens. But it seems it + isn't discarded, but reused after that. Because the server thinks (rightly) that the connection is closed, it just sends a RST packet. + Calling the admin endpoint just happened to remove the dead connection from the pool. + This also fixes the problem (which using the Java HTTP client): System.setProperty("http.keepAlive", "false"); + */ Assert.isTrue(new RestTemplate().getForEntity("http://localhost:" + this.server.port() + "/__admin/mappings", String.class) .getStatusCode().is2xxSuccessful(), "__admin/mappings endpoint wasn't accessible"); }