Skip to content

Commit d14693b

Browse files
committed
FIXUP: attempt to fix travis-ci: use SO_LINGER
1 parent 52b7cc9 commit d14693b

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

src/test/java/org/tarantool/AbstractTarantoolConnectorIT.java

+4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ protected TarantoolClient makeClient() {
135135
return new TarantoolClientImpl(socketChannelProvider, makeClientConfig());
136136
}
137137

138+
protected TarantoolClient makeClient(SocketChannelProvider provider) {
139+
return new TarantoolClientImpl(provider, makeClientConfig());
140+
}
141+
138142
protected static TarantoolClientConfig makeClientConfig() {
139143
return fillClientConfig(new TarantoolClientConfig());
140144
}

src/test/java/org/tarantool/ClientReconnectIT.java

+27-10
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,27 @@ public void run() {
217217

218218
/**
219219
* Test concurrent operations, reconnects and close.
220+
*
220221
* Expected situation is nothing gets stuck.
222+
*
223+
* The test sets SO_LINGER to 0 for outgoing connections to avoid producing
224+
* many TIME_WAIT sockets, because an available port range can be
225+
* exhausted.
221226
*/
222227
@Test
223228
public void testLongParallelCloseReconnects() {
224229
int numThreads = 4;
225230
int numClients = 4;
226231
int timeBudget = 30*1000;
227232

233+
SocketChannelProvider provider = new TestSocketChannelProvider(host,
234+
port, RESTART_TIMEOUT).setSoLinger(0);
235+
228236
final AtomicReferenceArray<TarantoolClient> clients =
229237
new AtomicReferenceArray<TarantoolClient>(numClients);
230238

231239
for (int idx = 0; idx < clients.length(); idx++) {
232-
clients.set(idx, makeClient());
240+
clients.set(idx, makeClient(provider));
233241
}
234242

235243
final Random rnd = new Random();
@@ -258,7 +266,7 @@ public void run() {
258266

259267
cli.close();
260268

261-
TarantoolClient next = makeClient();
269+
TarantoolClient next = makeClient(provider);
262270
if (!clients.compareAndSet(idx, cli, next)) {
263271
next.close();
264272
}
@@ -306,14 +314,17 @@ public void run() {
306314
}
307315

308316
/**
309-
* Verify that we don't exhaust a file descriptor limit (and so likely
310-
* don't leak file descriptors) when trying to connect to a non existing
311-
* node.
317+
* Verify that we don't exceed a file descriptor limit (and so likely don't
318+
* leak file descriptors) when trying to connect to a non existing node.
319+
*
320+
* The test sets SO_LINGER to 0 for outgoing connections to avoid producing
321+
* many TIME_WAIT sockets, because an available port range can be
322+
* exhausted.
312323
*/
313324
@Test
314325
public void testReconnectNonExist() throws Exception {
315326
SocketChannelProvider provider = new TestSocketChannelProvider(host,
316-
NON_EXIST_PORT, RESTART_TIMEOUT);
327+
NON_EXIST_PORT, RESTART_TIMEOUT).setSoLinger(0);
317328
TarantoolClientConfig config = makeClientConfig();
318329
config.initTimeoutMillis = 100;
319330
TarantoolClientImpl client = null;
@@ -330,19 +341,25 @@ public void testReconnectNonExist() throws Exception {
330341
}
331342

332343
/**
333-
* Verify that we don't exhaust a file descriptor limit (and so likely
334-
* don't leak file descriptors) when trying to connect to an existing node
335-
* with wrong authentification credentials.
344+
* Verify that we don't exceed a file descriptor limit (and so likely don't
345+
* leak file descriptors) when trying to connect to an existing node with
346+
* wrong authentification credentials.
347+
*
348+
* The test sets SO_LINGER to 0 for outgoing connections to avoid producing
349+
* many TIME_WAIT sockets, because an available port range can be
350+
* exhausted.
336351
*/
337352
@Test
338353
public void testReconnectWrongAuth() throws Exception {
354+
SocketChannelProvider provider = new TestSocketChannelProvider(host,
355+
port, RESTART_TIMEOUT).setSoLinger(0);
339356
TarantoolClientConfig config = makeClientConfig();
340357
config.initTimeoutMillis = 100;
341358
config.password = config.password + 'x';
342359
TarantoolClientImpl client = null;
343360
for (int i = 0; i < 100; ++i) {
344361
try {
345-
client = new TarantoolClientImpl(socketChannelProvider, config);
362+
client = new TarantoolClientImpl(provider, config);
346363
} catch (Exception ignored) {
347364
}
348365
assertNull(client);

src/test/java/org/tarantool/TestSocketChannelProvider.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,39 @@
22

33
import java.net.InetSocketAddress;
44
import java.nio.channels.SocketChannel;
5+
import static java.net.StandardSocketOptions.SO_LINGER;
56

67
/**
78
* Socket channel provider to be used throughout the tests.
89
*/
910
public class TestSocketChannelProvider implements SocketChannelProvider {
1011
String host;
1112
int port;
12-
int restart_timeout;
13+
int restartTimeout;
14+
int soLinger;
1315

14-
public TestSocketChannelProvider(String host, int port, int restart_timeout) {
16+
public TestSocketChannelProvider(String host, int port, int restartTimeout) {
1517
this.host = host;
1618
this.port = port;
17-
this.restart_timeout = restart_timeout;
19+
this.restartTimeout = restartTimeout;
20+
this.soLinger = -1;
21+
}
22+
23+
public TestSocketChannelProvider setSoLinger(int soLinger) {
24+
this.soLinger = soLinger;
25+
return this;
1826
}
1927

2028
@Override
2129
public SocketChannel get(int retryNumber, Throwable lastError) {
22-
long budget = System.currentTimeMillis() + restart_timeout;
30+
long budget = System.currentTimeMillis() + restartTimeout;
2331
while (!Thread.currentThread().isInterrupted()) {
2432
try {
25-
return SocketChannel.open(new InetSocketAddress(host, port));
33+
SocketChannel channel = SocketChannel.open();
34+
if (soLinger >= 0)
35+
channel.setOption(SO_LINGER, soLinger);
36+
channel.connect(new InetSocketAddress(host, port));
37+
return channel;
2638
} catch (Exception e) {
2739
if (budget < System.currentTimeMillis())
2840
throw new RuntimeException(e);

0 commit comments

Comments
 (0)