From 483b877b99f4aaee180f5b9cc6506e46549d84a7 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 5 Nov 2019 14:36:48 +0100 Subject: [PATCH] StubRunnerWireMockTestExecutionListener marks context as dirty even when random port used without this change we weren't marking properly that the stub should run with a random port with this change we do reflect random port picking fixes gh-1093 --- .../HttpServerStubConfiguration.java | 18 ++++++++++++- .../stubrunner/StubRunnerExecutor.java | 11 +++++--- .../wiremock/WireMockHttpServerStub.java | 15 ++++++++--- .../WireMockHttpServerStubAccessor.groovy | 25 +++++++++++++++++++ .../spring/StubRunnerConfigurationSpec.groovy | 6 +++++ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStubAccessor.groovy diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/HttpServerStubConfiguration.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/HttpServerStubConfiguration.java index c5c8365807..b481c56aed 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/HttpServerStubConfiguration.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/HttpServerStubConfiguration.java @@ -44,17 +44,33 @@ public final class HttpServerStubConfiguration { */ public final Integer port; + /** + * Is port a random one or was it fixed. + */ + public boolean randomPort; + public HttpServerStubConfiguration(HttpServerStubConfigurer configurer, StubRunnerOptions stubRunnerOptions, StubConfiguration stubConfiguration, Integer port) { + this(configurer, stubRunnerOptions, stubConfiguration, port, randomPort(port)); + } + + public HttpServerStubConfiguration(HttpServerStubConfigurer configurer, + StubRunnerOptions stubRunnerOptions, StubConfiguration stubConfiguration, + Integer port, boolean randomPort) { this.configurer = configurer; this.stubRunnerOptions = stubRunnerOptions; this.stubConfiguration = stubConfiguration; this.port = port; + this.randomPort = randomPort; + } + + private static boolean randomPort(Integer port) { + return port == null || port == 0; } public boolean isRandomPort() { - return this.port == null || this.port == 0; + return randomPort(this.port); } public String toColonSeparatedDependencyNotation() { diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java index a999b669ec..373b39f953 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerExecutor.java @@ -272,8 +272,9 @@ class StubRunnerExecutor implements StubFinder { final List mappings = repository.getStubs(); final Collection contracts = repository.contracts; Integer port = stubRunnerOptions.port(stubConfiguration); + boolean randomPort = randomPort(port); HttpServerStubConfiguration configuration = new HttpServerStubConfiguration( - configurer, stubRunnerOptions, stubConfiguration, port); + configurer, stubRunnerOptions, stubConfiguration, port, randomPort); if (!hasRequest(contracts) && mappings.isEmpty()) { if (log.isDebugEnabled()) { log.debug("There are no HTTP related contracts. Won't start any servers"); @@ -282,7 +283,7 @@ class StubRunnerExecutor implements StubFinder { new NoOpHttpServerStub()).start(configuration); return this.stubServer; } - if (port != null && port >= 0) { + if (!randomPort) { this.stubServer = new StubServer(stubConfiguration, mappings, contracts, httpServerStub()).start(configuration); } @@ -295,7 +296,7 @@ class StubRunnerExecutor implements StubFinder { httpServerStub()).start( new HttpServerStubConfiguration(configurer, stubRunnerOptions, stubConfiguration, - availablePort)); + availablePort, true)); } }); } @@ -303,6 +304,10 @@ class StubRunnerExecutor implements StubFinder { return this.stubServer; } + private boolean randomPort(Integer port) { + return port == null || port == 0; + } + private boolean hasRequest(Collection contracts) { if (contracts.isEmpty()) { return false; diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java index 046f96491f..d3fcb1a7fd 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java @@ -130,9 +130,9 @@ public class WireMockHttpServerStub implements HttpServerStub { } return this; } - int port = SocketUtils.findAvailableTcpPort(); - HttpServerStub serverStub = start(defaultConfiguration(port)); - cacheStubServer(true, port); + HttpServerStubConfiguration configuration = defaultConfiguration(); + HttpServerStub serverStub = start(configuration); + cacheStubServer(configuration.randomPort, configuration.port); return serverStub; } @@ -142,6 +142,13 @@ public class WireMockHttpServerStub implements HttpServerStub { null, port); } + private HttpServerStubConfiguration defaultConfiguration() { + int port = SocketUtils.findAvailableTcpPort(); + return new HttpServerStubConfiguration( + HttpServerStubConfigurer.NoOpHttpServerStubConfigurer.INSTANCE, null, + null, port, true); + } + @Override public HttpServerStub start(int port) { return start(defaultConfiguration(port)); @@ -175,7 +182,7 @@ public class WireMockHttpServerStub implements HttpServerStub { + " Started WireMock at [" + (this.https ? "https" : "http") + "] port [" + port + "]"); } - cacheStubServer(false, port); + cacheStubServer(configuration.randomPort, port); return this; } diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStubAccessor.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStubAccessor.groovy new file mode 100644 index 0000000000..82b3690440 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStubAccessor.groovy @@ -0,0 +1,25 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.provider.wiremock + +class WireMockHttpServerStubAccessor { + + static boolean everyPortRandom() { + assert WireMockHttpServerStub.SERVERS.every { it.value.random } + return true + } +} diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/StubRunnerConfigurationSpec.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/StubRunnerConfigurationSpec.groovy index f6457e443e..9016d3a993 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/StubRunnerConfigurationSpec.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/StubRunnerConfigurationSpec.groovy @@ -33,6 +33,7 @@ import org.springframework.boot.test.context.SpringBootTest import org.springframework.cloud.contract.stubrunner.HttpServerStubConfiguration import org.springframework.cloud.contract.stubrunner.StubFinder import org.springframework.cloud.contract.stubrunner.StubNotFoundException +import org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubAccessor import org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubConfigurer import org.springframework.context.annotation.Configuration import org.springframework.core.env.Environment @@ -75,6 +76,11 @@ class StubRunnerConfigurationSpec extends Specification { System.clearProperty("stubrunner.classifier") } + def 'should mark all ports as random'() { + expect: + WireMockHttpServerStubAccessor.everyPortRandom() + } + def 'should start WireMock servers'() { expect: 'WireMocks are running' stubFinder.findStubUrl('org.springframework.cloud.contract.verifier.stubs', 'loanIssuance') != null