Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
[#258] Minor AvailablePortScanner internal enhancements
Browse files Browse the repository at this point in the history
Groovier loop, better debug output, better test assertion.
  • Loading branch information
szpak committed Feb 9, 2015
1 parent f0a89f6 commit 94a66d5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ class AvailablePortScanner {
}

public <T> T tryToExecuteWithFreePort(Closure<T> closure) {
int counter = maxRetryCount
while (--counter > 0) {
for (i in (1..maxRetryCount)) {
try {
int portToScan = RandomUtils.nextInt(maxPortNumber - minPortNumber) + minPortNumber
checkIfPortIsAvailable(portToScan)
return executeLogicForAvailablePort(portToScan, closure)
} catch (Exception exception) {
log.debug("Failed to execute closure (counter: $counter)", exception)
log.debug("Failed to execute closure (try: $i/$maxRetryCount)", exception)
}
}
throw new NoPortAvailableException(minPortNumber, maxPortNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class AvailablePortScannerSpec extends Specification {
given:
AvailablePortScanner portScanner = new AvailablePortScanner(MIN_PORT, MAX_PORT)
when:
portScanner.tryToExecuteWithFreePort {}
int usedPort = portScanner.tryToExecuteWithFreePort { int port -> port }
then:
noExceptionThrown()
usedPort == MIN_PORT
}

def 'should throw exception when free port number cannot be found'() {
Expand Down Expand Up @@ -47,5 +48,4 @@ class AvailablePortScannerSpec extends Specification {
MAX_PORT | MIN_PORT
MIN_PORT | MIN_PORT
}

}

0 comments on commit 94a66d5

Please sign in to comment.