Skip to content

Commit

Permalink
Add more tests in AdjacentFreePortFinderTest (#554)
Browse files Browse the repository at this point in the history
* Add test to ensure it finds last two ports in range when all others
are in use
* Add test to ensure it throws exception when all but last port in range
are in use
  • Loading branch information
sleberknight authored Oct 17, 2024
1 parent 171b2b6 commit f4aa3dc
Showing 1 changed file with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -110,6 +111,70 @@ void shouldFindNextAdjacentApplicationAndAdminPorts_WhenSomeAreInUse() {
verify(checker, times(17)).isPortAvailable(anyInt());
}

@Test
void shouldFindLastTwoPortsInRange_WhenAllOthersAreInUse() {
var checker = mock(LocalPortChecker.class);
var portFinder = new AdjacentFreePortFinder(checker);

when(checker.isPortAvailable(anyInt()))
.thenReturn(false) // 9000
.thenReturn(false) // 9001
.thenReturn(false) // 9002
.thenReturn(false) // 9003
.thenReturn(true) // 9004
.thenReturn(true); // 9005

var minPortNumber = 9_000;
var maxPortNumber = 9_005;

var servicePorts = portFinder.find(new AllowablePortRange(minPortNumber, maxPortNumber));

assertAll(
() -> assertThat(servicePorts.applicationPort()).isEqualTo(9_004),
() -> assertThat(servicePorts.adminPort()).isEqualTo(9_005)
);

verify(checker).isPortAvailable(9_000);
verify(checker).isPortAvailable(9_001);
verify(checker).isPortAvailable(9_002);
verify(checker).isPortAvailable(9_003);
verify(checker).isPortAvailable(9_004);
verify(checker).isPortAvailable(9_005);
verifyNoMoreInteractions(checker);
}

@Test
void shouldThrowNoAvailablePortException_WhenAllButLastPortAreInUse() {
var checker = mock(LocalPortChecker.class);
var portFinder = new AdjacentFreePortFinder(checker);

when(checker.isPortAvailable(anyInt()))
.thenReturn(false) // 9000
.thenReturn(false) // 9001
.thenReturn(false) // 9002
.thenReturn(false) // 9003
.thenReturn(false) // 9004
.thenReturn(true); // 9005

var minPortNumber = 9_000;
var maxPortNumber = 9_005;

var portRange = new AllowablePortRange(minPortNumber, maxPortNumber);

assertThatExceptionOfType(NoAvailablePortException.class)
.isThrownBy(() -> portFinder.find(portRange))
.withMessage("Could not find two adjacent open ports between %d and %d",
minPortNumber, maxPortNumber);

verify(checker).isPortAvailable(9_000);
verify(checker).isPortAvailable(9_001);
verify(checker).isPortAvailable(9_002);
verify(checker).isPortAvailable(9_003);
verify(checker).isPortAvailable(9_004);
verify(checker, never()).isPortAvailable(9_005);
verifyNoMoreInteractions(checker);
}

@Test
void shouldThrowNoAvailablePortException_WhenNoPortsCanBeFound() {
var checker = mock(LocalPortChecker.class);
Expand All @@ -126,6 +191,7 @@ void shouldThrowNoAvailablePortException_WhenNoPortsCanBeFound() {
.withMessage("Could not find two adjacent open ports between %d and %d",
minPortNumber, maxPortNumber);

verify(checker, times(portRange.getNumPortsInRange() - 1)).isPortAvailable(anyInt());
int expectedNumberOfInvocations = portRange.getNumPortsInRange() - 1;
verify(checker, times(expectedNumberOfInvocations)).isPortAvailable(anyInt());
}
}

0 comments on commit f4aa3dc

Please sign in to comment.