Skip to content

Commit 201acf5

Browse files
committed
Tweak sme TCP tests to minimize time and memory
**Auto-cherry-pick to `6.3.x` & `6.2.x`**
1 parent d4f136a commit 201acf5

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void removeDeadConnection(TcpConnection connection) {
296296
@Test
297297
public void testReadCrLfOverflow() throws Exception {
298298
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
299-
serializer.setMaxMessageSize(1024);
299+
serializer.setMaxMessageSize(16);
300300
final Semaphore semaphore = new Semaphore(0);
301301
final List<TcpConnection> added = new ArrayList<>();
302302
final List<TcpConnection> removed = new ArrayList<>();
@@ -332,14 +332,14 @@ public void removeDeadConnection(TcpConnection connection) {
332332
whileOpen(semaphore, added);
333333
assertThat(added.size()).isEqualTo(1);
334334

335-
assertThat(errorMessageLetch.await(10, TimeUnit.SECONDS)).isTrue();
335+
assertThat(errorMessageLetch.await(20, TimeUnit.SECONDS)).isTrue();
336336

337337
assertThat(errorMessageRef.get().getMessage())
338338
.satisfiesAnyOf(
339-
s -> assertThat(s).contains("CRLF not found before max message length: 1024"),
339+
s -> assertThat(s).contains("CRLF not found before max message length: 16"),
340340
s -> assertThat(s).contains("Connection is closed"));
341341

342-
assertThat(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS)).isTrue();
342+
assertThat(semaphore.tryAcquire(20, TimeUnit.SECONDS)).isTrue();
343343
assertThat(removed).hasSizeGreaterThan(0);
344344
scf.stop();
345345
done.countDown();
@@ -531,9 +531,9 @@ public void removeDeadConnection(TcpConnection connection) {
531531
scf.stop();
532532
}
533533

534-
private void whileOpen(Semaphore semaphore, final List<TcpConnection> added)
534+
private void whileOpen(Semaphore semaphore, List<TcpConnection> added)
535535
throws InterruptedException {
536-
assertThat(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS)).isTrue();
536+
assertThat(semaphore.tryAcquire(20, TimeUnit.SECONDS)).isTrue();
537537
with().pollInterval(Duration.ofMillis(50)).await("Failed to close socket")
538538
.atMost(Duration.ofSeconds(20))
539539
.until(() -> !added.get(0).isOpen());

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
import javax.net.ServerSocketFactory;
3030

31-
import org.junit.Rule;
32-
import org.junit.Test;
31+
import org.junit.jupiter.api.Test;
3332

3433
import org.springframework.beans.factory.BeanFactory;
3534
import org.springframework.core.serializer.DefaultDeserializer;
@@ -42,12 +41,12 @@
4241
import org.springframework.integration.ip.util.SocketTestUtils;
4342
import org.springframework.integration.ip.util.TestingUtilities;
4443
import org.springframework.integration.support.MessageBuilder;
45-
import org.springframework.integration.test.support.LongRunningIntegrationTest;
4644
import org.springframework.messaging.Message;
4745
import org.springframework.messaging.MessageChannel;
4846
import org.springframework.messaging.support.GenericMessage;
4947

5048
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.assertj.core.api.Assertions.assertThatIOException;
5150
import static org.assertj.core.api.Assertions.fail;
5251
import static org.mockito.Mockito.mock;
5352

@@ -60,9 +59,6 @@
6059
*/
6160
public class DeserializationTests {
6261

63-
@Rule
64-
public LongRunningIntegrationTest longRunningIntegrationTest = new LongRunningIntegrationTest();
65-
6662
@Test
6763
public void testReadLength() throws Exception {
6864
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
@@ -240,17 +236,11 @@ public void testReadCrLfTimeout() throws Exception {
240236
server.setSoTimeout(10000);
241237
CountDownLatch latch = SocketTestUtils.testSendCrLfOverflow(port);
242238
Socket socket = server.accept();
243-
socket.setSoTimeout(500);
239+
socket.setSoTimeout(100);
244240
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
245-
try {
246-
serializer.deserialize(socket.getInputStream());
247-
fail("Expected timout exception");
248-
}
249-
catch (IOException e) {
250-
if (!e.getMessage().startsWith("Read timed out")) {
251-
fail("Unexpected IO Error:" + e.getMessage());
252-
}
253-
}
241+
assertThatIOException()
242+
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
243+
.withMessageStartingWith("Read timed out");
254244
server.close();
255245
latch.countDown();
256246
}
@@ -264,16 +254,10 @@ public void testReadCrLfOverflow() throws Exception {
264254
Socket socket = server.accept();
265255
socket.setSoTimeout(5000);
266256
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
267-
serializer.setMaxMessageSize(1024);
268-
try {
269-
serializer.deserialize(socket.getInputStream());
270-
fail("Expected message length exceeded exception");
271-
}
272-
catch (IOException e) {
273-
if (!e.getMessage().startsWith("CRLF not found")) {
274-
fail("Unexpected IO Error:" + e.getMessage());
275-
}
276-
}
257+
serializer.setMaxMessageSize(16);
258+
assertThatIOException()
259+
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
260+
.withMessageStartingWith("CRLF not found");
277261
server.close();
278262
latch.countDown();
279263
}
@@ -306,7 +290,8 @@ public void deserializationEvents() {
306290
assertThat(new String(event.getBuffer()).substring(0, 1)).isEqualTo(new String(new byte[] {7}));
307291
doDeserialize(new ByteArrayLfSerializer(), "Terminator '0xa' not found before max message length: 5");
308292
doDeserialize(new ByteArrayRawSerializer(), "Socket was not closed before max message length: 5");
309-
doDeserialize(new ByteArraySingleTerminatorSerializer((byte) 0xfe), "Terminator '0xfe' not found before max message length: 5");
293+
doDeserialize(new ByteArraySingleTerminatorSerializer((byte) 0xfe),
294+
"Terminator '0xfe' not found before max message length: 5");
310295
doDeserialize(new ByteArrayStxEtxSerializer(), "Expected STX to begin message");
311296
event = doDeserialize(new ByteArrayStxEtxSerializer(),
312297
"Socket closed during message assembly", new byte[] {0x02, 0, 0}, 5);
@@ -365,7 +350,7 @@ private void testTimeoutWhileDecoding(AbstractByteArraySerializer deserializer,
365350
TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
366351
clientNio.setSerializer(serializer);
367352
clientNio.setDeserializer(deserializer);
368-
clientNio.setSoTimeout(1000);
353+
clientNio.setSoTimeout(500);
369354
clientNio.afterPropertiesSet();
370355
final TcpOutboundGateway out = new TcpOutboundGateway();
371356
out.setConnectionFactory(clientNio);

spring-integration-ip/src/test/java/org/springframework/integration/ip/util/SocketTestUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -325,16 +325,15 @@ public static CountDownLatch testSendCrLfOverflow(final int port) {
325325
Thread thread = new Thread(() -> {
326326
try (Socket socket = new Socket(InetAddress.getLocalHost(), port)) {
327327
OutputStream outputStream = socket.getOutputStream();
328-
for (int i = 0; i < 1500; i++) {
328+
for (int i = 0; i < 20; i++) {
329329
writeByte(outputStream, 'x', true);
330330
}
331-
testCompleteLatch.await(10, TimeUnit.SECONDS);
331+
testCompleteLatch.await(30, TimeUnit.SECONDS);
332332
}
333333
catch (Exception e) {
334334

335335
}
336336
});
337-
thread.setDaemon(true);
338337
thread.start();
339338
return testCompleteLatch;
340339
}

0 commit comments

Comments
 (0)