@@ -6,11 +6,13 @@ import io.mockk.mockkObject
66import io.mockk.unmockkObject
77import org.assertj.core.api.AbstractIntegerAssert
88import org.assertj.core.api.Assertions.*
9+ import org.assertj.core.api.ListAssert
910import org.assertj.core.api.ThrowableAssert
1011import org.junit.jupiter.api.*
1112import org.junit.jupiter.api.extension.ExtendWith
1213import org.junit.jupiter.params.ParameterizedTest
1314import org.junit.jupiter.params.provider.ValueSource
15+ import redis.embedded.Redis.DEFAULT_REDIS_PORT
1416import java.util.stream.IntStream.range
1517
1618@ExtendWith(MockKExtension ::class )
@@ -77,13 +79,31 @@ internal class PortProviderTest {
7779
7880 @DisplayName(" Multiple valid ports should be returned if all ports are free" )
7981 @ParameterizedTest(name = " {0} valid ports should be returned" )
80- @ValueSource(ints = [5 , 10 , 100 , 10000 ])
82+ @ValueSource(ints = [5 , 10 , 100 ])
8183 fun freePorts_requestingPorts_returnsValidPorts (nOfPorts : Int ) {
8284 givenFreePorts()
8385 whenNextPortsAreRequested(nOfPorts)
8486 thenValidPortsAreReturned(nOfPorts)
8587 }
8688
89+ @Test
90+ @DisplayName(" If port 16379 (bus port) is taken, port 6379 should not be handed out" )
91+ fun defaultBusPortTaken_requestingPort_skipDefaultPort () {
92+ givenFreePorts()
93+ givenFirstBusPortTaken()
94+ whenNextPortIsRequested()
95+ thenValidPortIsReturned().isNotEqualTo(DEFAULT_REDIS_PORT )
96+ }
97+
98+ @Test
99+ @DisplayName(" If a bus port has been handed out, the corresponding Redis port should not be handed out" )
100+ fun busPortHandedOut_requestingPort_skipPortWithHandedOutBusPort () {
101+ givenFreePorts()
102+ givenSentinelPortPreviouslyRequested()
103+ whenNextPortsAreRequested(10000 )
104+ thenValidPortsAreReturned(10000 ).doesNotContain(DEFAULT_REDIS_PORT + BUS_PORT_OFFSET )
105+ }
106+
87107 private fun givenSentinel () {
88108 givenSentinel = true
89109 }
@@ -99,6 +119,16 @@ internal class PortProviderTest {
99119 every { PortChecker .available(any()) } returns false
100120 }
101121
122+ private fun givenFirstBusPortTaken () {
123+ val firstBusPort = DEFAULT_REDIS_PORT + BUS_PORT_OFFSET
124+ every { PortChecker .available(eq(firstBusPort)) } returns false
125+ every { PortChecker .available(neq(firstBusPort)) } returns true
126+ }
127+
128+ private fun givenSentinelPortPreviouslyRequested () {
129+ portProvider.next(true )
130+ }
131+
102132 private fun whenNextPortIsRequested () = whenNextPortsAreRequested(1 )
103133
104134 private fun whenNextPortsAreRequested (nOfPorts : Int ) {
@@ -118,7 +148,7 @@ internal class PortProviderTest {
118148 return assertThat(actualPorts.first())
119149 }
120150
121- private fun thenValidPortsAreReturned (nOfPorts : Int ) {
151+ private fun thenValidPortsAreReturned (nOfPorts : Int ): ListAssert < Int > {
122152 assertThatCode(requestPorts!! ).doesNotThrowAnyException()
123153 assertThat(actualPorts).hasSize(nOfPorts)
124154 assertThat(actualPorts).isNotEmpty.doesNotHaveDuplicates()
@@ -129,5 +159,6 @@ internal class PortProviderTest {
129159 }
130160 // bus port should have been left free
131161 assertThat(actualPorts).allSatisfy { assertThat(actualPorts).doesNotContain(it + 10000 ) }
162+ return assertThat(actualPorts)
132163 }
133164}
0 commit comments