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
This commit is contained in:
Marcin Grzejszczak
2019-11-05 14:36:48 +01:00
parent 380d2535bd
commit 483b877b99
5 changed files with 67 additions and 8 deletions

View File

@@ -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() {

View File

@@ -272,8 +272,9 @@ class StubRunnerExecutor implements StubFinder {
final List<File> mappings = repository.getStubs();
final Collection<Contract> 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<Contract> contracts) {
if (contracts.isEmpty()) {
return false;

View File

@@ -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;
}

View File

@@ -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
}
}

View File

@@ -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