Skip to content

Commit b8ac9f6

Browse files
committed
Add Functional tests and remove TODOs.
JAVA-5856
1 parent 839e51d commit b8ac9f6

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

driver-core/src/main/com/mongodb/internal/connection/TlsChannelStreamFactoryFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ void start() {
174174
selectorThread.start();
175175
}
176176

177-
178177
void register(final SocketRegistration registration) {
179178
pendingRegistrations.add(registration);
180179
selector.wakeup();
@@ -249,13 +248,10 @@ private void scheduleTimeoutInterruption(final AsyncCompletionHandler<Void> hand
249248
private void closeAndTimeout(final AsyncCompletionHandler<Void> handler, final SocketChannel socketChannel) {
250249
// We check if this stream was closed before timeout exception.
251250
boolean streamClosed = isClosed();
252-
253-
//TODO refactor ths draft
254251
InterruptedByTimeoutException timeoutException = new InterruptedByTimeoutException();
255252
try {
256253
socketChannel.close();
257254
} catch (Exception e) {
258-
//TODO should ignore this exception? We seem to do so in other places
259255
timeoutException.addSuppressed(e);
260256
}
261257

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.internal.connection;
18+
19+
import com.mongodb.MongoSocketOpenException;
20+
import com.mongodb.ServerAddress;
21+
import com.mongodb.connection.SocketSettings;
22+
import com.mongodb.connection.SslSettings;
23+
import com.mongodb.internal.TimeoutContext;
24+
import com.mongodb.internal.TimeoutSettings;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
30+
import java.io.IOException;
31+
import java.net.ServerSocket;
32+
import java.nio.channels.InterruptedByTimeoutException;
33+
import java.util.concurrent.TimeUnit;
34+
35+
import static com.mongodb.assertions.Assertions.assertTrue;
36+
import static org.junit.Assert.assertThrows;
37+
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
39+
40+
class TlsChannelStreamFunctionalTest {
41+
private static final SslSettings SSL_SETTINGS = SslSettings.builder().enabled(true).build();
42+
private static final String UNREACHABLE_PRIVATE_IP_ADDRESS = "10.255.255.1";
43+
private ServerSocket serverSocket;
44+
private int port;
45+
46+
@BeforeEach
47+
void setUp() throws IOException {
48+
serverSocket = new ServerSocket(0, 1);
49+
port = serverSocket.getLocalPort();
50+
}
51+
52+
@AfterEach
53+
void cleanUp() throws IOException {
54+
try (ServerSocket ignore = serverSocket) {
55+
}
56+
}
57+
58+
@ParameterizedTest
59+
@ValueSource(ints = {500, 1000, 2000})
60+
void shouldInterruptConnectionEstablishmentWhenConnectionTimeoutExpires(final int connectTimeout) {
61+
//given
62+
try (TlsChannelStreamFactoryFactory factory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver())) {
63+
StreamFactory streamFactory = factory.create(SocketSettings.builder()
64+
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
65+
.build(), SSL_SETTINGS);
66+
67+
Stream stream = streamFactory.create(new ServerAddress(UNREACHABLE_PRIVATE_IP_ADDRESS, port));
68+
long connectOpenStart = System.nanoTime();
69+
70+
//when
71+
MongoSocketOpenException mongoSocketOpenException = assertThrows(MongoSocketOpenException.class, () ->
72+
stream.open(OperationContext
73+
.simpleOperationContext(new TimeoutContext(TimeoutSettings.DEFAULT
74+
.withConnectTimeoutMS(connectTimeout)))));
75+
76+
//then
77+
long elapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - connectOpenStart);
78+
assertInstanceOf(InterruptedByTimeoutException.class, mongoSocketOpenException.getCause(),
79+
"Actual cause: " + mongoSocketOpenException.getCause());
80+
long diff = elapsedMs - connectTimeout;
81+
assertFalse(diff < 0,
82+
String.format("Connection timed-out sooner than expected. Difference: %d ms", diff));
83+
// Allowed difference, with test overhead setup is 300MS.
84+
int epsilon = 300;
85+
assertTrue(diff < epsilon,
86+
String.format("Elapsed time %d ms should be within %d ms of the connect timeout", elapsedMs, epsilon));
87+
}
88+
}
89+
90+
@ParameterizedTest
91+
@ValueSource(ints = {0, 500, 1000, 2000})
92+
void shouldEstablishConnection(final int connectTimeout) throws IOException {
93+
try (TlsChannelStreamFactoryFactory factory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver())) {
94+
//given
95+
StreamFactory streamFactory = factory.create(SocketSettings.builder()
96+
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
97+
.build(), SSL_SETTINGS);
98+
99+
Stream stream = streamFactory.create(new ServerAddress("localhost", port));
100+
101+
try {
102+
//when
103+
stream.open(OperationContext.simpleOperationContext(
104+
new TimeoutContext(TimeoutSettings.DEFAULT.withConnectTimeoutMS(connectTimeout))));
105+
106+
//then
107+
assertFalse(stream.isClosed());
108+
} finally {
109+
stream.close();
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)