Skip to content

Commit 39ee2fe

Browse files
committed
Remove redundant logic.
JAVA-5856
1 parent 0ef564d commit 39ee2fe

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

driver-core/src/test/functional/com/mongodb/internal/connection/TlsChannelStreamFunctionalTest.java

+30-44
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import com.mongodb.connection.SslSettings;
2323
import com.mongodb.internal.TimeoutContext;
2424
import com.mongodb.internal.TimeoutSettings;
25-
import org.junit.jupiter.api.AfterEach;
26-
import org.junit.jupiter.api.BeforeEach;
2725
import org.junit.jupiter.params.ParameterizedTest;
2826
import org.junit.jupiter.params.provider.ValueSource;
2927
import org.mockito.MockedStatic;
@@ -37,12 +35,13 @@
3735
import java.nio.channels.SocketChannel;
3836
import java.util.concurrent.TimeUnit;
3937

38+
import static com.mongodb.internal.connection.OperationContext.simpleOperationContext;
4039
import static java.lang.String.format;
41-
import static java.util.concurrent.TimeUnit.SECONDS;
42-
import static org.junit.Assert.assertThrows;
40+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4341
import static org.junit.jupiter.api.Assertions.assertFalse;
4442
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
4543
import static org.junit.jupiter.api.Assertions.assertNotNull;
44+
import static org.junit.jupiter.api.Assertions.assertThrows;
4645
import static org.junit.jupiter.api.Assertions.assertTrue;
4746
import static org.junit.jupiter.api.Assertions.fail;
4847
import static org.mockito.Mockito.atLeast;
@@ -51,56 +50,39 @@
5150
class TlsChannelStreamFunctionalTest {
5251
private static final SslSettings SSL_SETTINGS = SslSettings.builder().enabled(true).build();
5352
private static final String UNREACHABLE_PRIVATE_IP_ADDRESS = "10.255.255.1";
54-
private ServerSocket serverSocket;
55-
private int port;
56-
57-
@BeforeEach
58-
void setUp() throws IOException {
59-
serverSocket = new ServerSocket(0, 1);
60-
port = serverSocket.getLocalPort();
61-
}
62-
63-
@AfterEach
64-
@SuppressWarnings("try")
65-
void cleanUp() throws IOException {
66-
try (ServerSocket ignored = serverSocket) {
67-
//ignored
68-
}
69-
}
53+
private static final int UNREACHABLE_PORT = 65333;
7054

7155
@ParameterizedTest
7256
@ValueSource(ints = {500, 1000, 2000})
73-
void shouldInterruptConnectionEstablishmentWhenConnectionTimeoutExpires(final int connectTimeout) throws IOException {
57+
void shouldInterruptConnectionEstablishmentWhenConnectionTimeoutExpires(final int connectTimeoutMs) throws IOException {
7458
//given
75-
try (TlsChannelStreamFactoryFactory factory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver());
59+
try (StreamFactoryFactory streamFactoryFactory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver());
7660
MockedStatic<SocketChannel> socketChannelMockedStatic = Mockito.mockStatic(SocketChannel.class)) {
7761
SingleResultSpyCaptor<SocketChannel> singleResultSpyCaptor = new SingleResultSpyCaptor<>();
7862
socketChannelMockedStatic.when(SocketChannel::open).thenAnswer(singleResultSpyCaptor);
7963

80-
StreamFactory streamFactory = factory.create(SocketSettings.builder()
81-
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
64+
StreamFactory streamFactory = streamFactoryFactory.create(SocketSettings.builder()
65+
.connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
8266
.build(), SSL_SETTINGS);
8367

84-
Stream stream = streamFactory.create(new ServerAddress(UNREACHABLE_PRIVATE_IP_ADDRESS, port));
68+
Stream stream = streamFactory.create(new ServerAddress(UNREACHABLE_PRIVATE_IP_ADDRESS, UNREACHABLE_PORT));
8569
long connectOpenStart = System.nanoTime();
8670

8771
//when
72+
OperationContext operationContext = createOperationContext(connectTimeoutMs);
8873
MongoSocketOpenException mongoSocketOpenException = assertThrows(MongoSocketOpenException.class, () ->
89-
stream.open(OperationContext
90-
.simpleOperationContext(new TimeoutContext(TimeoutSettings.DEFAULT
91-
.withConnectTimeoutMS(connectTimeout)))));
74+
stream.open(operationContext));
9275

9376
//then
9477
long elapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - connectOpenStart);
9578
// Allow for some timing imprecision due to test overhead.
9679
int maximumAcceptableTimeoutOvershoot = 300;
9780

98-
assertInstanceOf(InterruptedByTimeoutException.class, mongoSocketOpenException.getCause(),
99-
"Actual cause: " + mongoSocketOpenException.getCause());
100-
assertFalse(connectTimeout > elapsedMs,
101-
format("Connection timed-out sooner than expected. ConnectTimeoutMS: %d, elapsedMs: %d", connectTimeout, elapsedMs));
102-
assertTrue(elapsedMs - connectTimeout < maximumAcceptableTimeoutOvershoot,
103-
format("Connection timeout overshoot time %d ms should be within %d ms", elapsedMs - connectTimeout,
81+
assertInstanceOf(InterruptedByTimeoutException.class, mongoSocketOpenException.getCause());
82+
assertFalse(connectTimeoutMs > elapsedMs,
83+
format("Connection timed-out sooner than expected. ConnectTimeoutMS: %d, elapsedMs: %d", connectTimeoutMs, elapsedMs));
84+
assertTrue(elapsedMs - connectTimeoutMs < maximumAcceptableTimeoutOvershoot,
85+
format("Connection timeout overshoot time %d ms should be within %d ms", elapsedMs - connectTimeoutMs,
10486
maximumAcceptableTimeoutOvershoot));
10587

10688
SocketChannel actualSpySocketChannel = singleResultSpyCaptor.getResult();
@@ -111,30 +93,30 @@ void shouldInterruptConnectionEstablishmentWhenConnectionTimeoutExpires(final in
11193

11294
@ParameterizedTest
11395
@ValueSource(ints = {0, 500, 1000, 2000})
114-
void shouldEstablishConnection(final int connectTimeout) throws IOException, InterruptedException {
96+
void shouldEstablishConnection(final int connectTimeoutMs) throws IOException, InterruptedException {
11597
//given
116-
try (TlsChannelStreamFactoryFactory factory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver());
117-
MockedStatic<SocketChannel> socketChannelMockedStatic = Mockito.mockStatic(SocketChannel.class)) {
98+
try (StreamFactoryFactory streamFactoryFactory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver());
99+
MockedStatic<SocketChannel> socketChannelMockedStatic = Mockito.mockStatic(SocketChannel.class);
100+
ServerSocket serverSocket = new ServerSocket(0, 1)) {
118101
SingleResultSpyCaptor<SocketChannel> singleResultSpyCaptor = new SingleResultSpyCaptor<>();
119102
socketChannelMockedStatic.when(SocketChannel::open).thenAnswer(singleResultSpyCaptor);
120103

121-
StreamFactory streamFactory = factory.create(SocketSettings.builder()
122-
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
104+
StreamFactory streamFactory = streamFactoryFactory.create(SocketSettings.builder()
105+
.connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
123106
.build(), SSL_SETTINGS);
124107

125-
Stream stream = streamFactory.create(new ServerAddress(serverSocket.getInetAddress(), port));
108+
Stream stream = streamFactory.create(new ServerAddress(serverSocket.getInetAddress(), serverSocket.getLocalPort()));
126109
try {
127110
//when
128-
stream.open(OperationContext.simpleOperationContext(
129-
new TimeoutContext(TimeoutSettings.DEFAULT.withConnectTimeoutMS(connectTimeout))));
111+
stream.open(createOperationContext(connectTimeoutMs));
130112

131113
//then
132114
SocketChannel actualSpySocketChannel = singleResultSpyCaptor.getResult();
133115
assertNotNull(actualSpySocketChannel, "SocketChannel was not opened");
134116
assertTrue(actualSpySocketChannel.isConnected());
135117

136118
// Wait to verify that socket was not closed by timeout.
137-
SECONDS.sleep(3);
119+
MILLISECONDS.sleep(connectTimeoutMs * 2L);
138120
assertTrue(actualSpySocketChannel.isConnected());
139121
assertFalse(stream.isClosed());
140122
} finally {
@@ -151,7 +133,7 @@ public T getResult() {
151133
}
152134

153135
@Override
154-
public T answer(InvocationOnMock invocationOnMock) throws Throwable {
136+
public T answer(final InvocationOnMock invocationOnMock) throws Throwable {
155137
if (result != null) {
156138
fail(invocationOnMock.getMethod().getName() + " was called more then once");
157139
}
@@ -160,4 +142,8 @@ public T answer(InvocationOnMock invocationOnMock) throws Throwable {
160142
return result;
161143
}
162144
}
145+
146+
private static OperationContext createOperationContext(final int connectTimeoutMs) {
147+
return simpleOperationContext(new TimeoutContext(TimeoutSettings.DEFAULT.withConnectTimeoutMS(connectTimeoutMs)));
148+
}
163149
}

0 commit comments

Comments
 (0)