simplify min max port usage (#233)

This commit is contained in:
Mariusz Smykuła
2016-04-16 16:27:57 +02:00
parent e70c0c3a3b
commit 79155bfde2
4 changed files with 16 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ class AvailablePortScanner {
}
private void checkPortRanges(int minPortNumber, int maxPortNumber) {
if (minPortNumber >= maxPortNumber) {
if (minPortNumber > maxPortNumber) {
throw new InvalidPortRange(minPortNumber, maxPortNumber)
}
}
@@ -34,7 +34,7 @@ class AvailablePortScanner {
public <T> T tryToExecuteWithFreePort(Closure<T> closure) {
for (i in (1..maxRetryCount)) {
try {
int numberOfPortsToBind = maxPortNumber - minPortNumber
int numberOfPortsToBind = maxPortNumber - minPortNumber + 1
int portToScan = new Random().nextInt(numberOfPortsToBind) + minPortNumber
checkIfPortIsAvailable(portToScan)
return executeLogicForAvailablePort(portToScan, closure)
@@ -67,7 +67,7 @@ class AvailablePortScanner {
static class InvalidPortRange extends RuntimeException {
protected InvalidPortRange(int lowerBound, int upperBound) {
super("Invalid bounds exceptions, min port [$lowerBound] is greater or equal to max port [$upperBound]")
super("Invalid bounds exceptions, min port [$lowerBound] is greater to max port [$upperBound]")
}
}
}

View File

@@ -15,6 +15,16 @@ class AvailablePortScannerSpec extends Specification {
int usedPort = portScanner.tryToExecuteWithFreePort { int port -> port }
then:
noExceptionThrown()
usedPort == MIN_PORT || MAX_PORT
}
def 'should execute given closure with the available port from specified range'() {
given:
AvailablePortScanner portScanner = new AvailablePortScanner(MIN_PORT, MIN_PORT)
when:
int usedPort = portScanner.tryToExecuteWithFreePort { int port -> port }
then:
noExceptionThrown()
usedPort == MIN_PORT
}
@@ -23,10 +33,9 @@ class AvailablePortScannerSpec extends Specification {
new AvailablePortScanner(minPort, maxPort, MAX_RETRY_COUNT_FOR_NEGATIVE_SCENARIOS)
then:
def ex = thrown(AvailablePortScanner.InvalidPortRange)
ex.message == "Invalid bounds exceptions, min port [$minPort] is greater or equal to max port [$maxPort]"
ex.message == "Invalid bounds exceptions, min port [$minPort] is greater to max port [$maxPort]"
where:
minPort | maxPort
MIN_PORT | MIN_PORT
MAX_PORT | MIN_PORT
}

View File

@@ -6,7 +6,7 @@ class StubRunnerExecutorSpec extends Specification {
static final URL EXPECTED_STUB_URL = new URL('http://localhost:8999')
static final int MIN_PORT = 8999
static final int MAX_PORT = 9000
static final int MAX_PORT = 8999
private AvailablePortScanner portScanner
private StubRepository repository

View File

@@ -5,7 +5,7 @@ import spock.lang.Specification
class StubRunnerSpec extends Specification {
private static final int MIN_PORT = 8111
private static final int MAX_PORT = 8112
private static final int MAX_PORT = 8111
private static final URL EXPECTED_STUB_URL = new URL("http://localhost:$MIN_PORT")
def 'should provide stub URL for provided groupid and artifactId'() {