From 79155bfde290a78a8c25f97c682aa865cda305e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Smyku=C5=82a?= Date: Sat, 16 Apr 2016 16:27:57 +0200 Subject: [PATCH] simplify min max port usage (#233) --- .../accurest/stubrunner/AvailablePortScanner.groovy | 6 +++--- .../stubrunner/AvailablePortScannerSpec.groovy | 13 +++++++++++-- .../stubrunner/StubRunnerExecutorSpec.groovy | 2 +- .../accurest/stubrunner/StubRunnerSpec.groovy | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy index 3b2df9f9b5..db17ee7baa 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/AvailablePortScanner.groovy @@ -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 tryToExecuteWithFreePort(Closure 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]") } } } diff --git a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/AvailablePortScannerSpec.groovy b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/AvailablePortScannerSpec.groovy index 7e7aee8b69..609e746e66 100644 --- a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/AvailablePortScannerSpec.groovy +++ b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/AvailablePortScannerSpec.groovy @@ -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 } diff --git a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy index ce378588a5..8548ea38c4 100644 --- a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy +++ b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerExecutorSpec.groovy @@ -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 diff --git a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerSpec.groovy b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerSpec.groovy index 1d63e28bfd..6735785f70 100644 --- a/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerSpec.groovy +++ b/stub-runner/stub-runner/src/test/groovy/io/codearte/accurest/stubrunner/StubRunnerSpec.groovy @@ -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'() {