Skip to content

Commit b8fc2af

Browse files
committed
Tweak sme TCP tests to minimize time and memory
(cherry picked from commit 201acf5) # Conflicts: # spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java
1 parent 88be958 commit b8fc2af

File tree

3 files changed

+22
-40
lines changed

3 files changed

+22
-40
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
@@ -297,7 +297,7 @@ public void removeDeadConnection(TcpConnection connection) {
297297
@Test
298298
public void testReadCrLfOverflow() throws Exception {
299299
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
300-
serializer.setMaxMessageSize(1024);
300+
serializer.setMaxMessageSize(16);
301301
final Semaphore semaphore = new Semaphore(0);
302302
final List<TcpConnection> added = new ArrayList<>();
303303
final List<TcpConnection> removed = new ArrayList<>();
@@ -333,14 +333,14 @@ public void removeDeadConnection(TcpConnection connection) {
333333
whileOpen(semaphore, added);
334334
assertThat(added.size()).isEqualTo(1);
335335

336-
assertThat(errorMessageLetch.await(10, TimeUnit.SECONDS)).isTrue();
336+
assertThat(errorMessageLetch.await(20, TimeUnit.SECONDS)).isTrue();
337337

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

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

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

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929

3030
import javax.net.ServerSocketFactory;
3131

32-
import org.junit.Rule;
33-
import org.junit.Test;
32+
import org.junit.jupiter.api.Test;
3433

3534
import org.springframework.beans.factory.BeanFactory;
3635
import org.springframework.context.ApplicationEvent;
@@ -45,12 +44,12 @@
4544
import org.springframework.integration.ip.util.SocketTestUtils;
4645
import org.springframework.integration.ip.util.TestingUtilities;
4746
import org.springframework.integration.support.MessageBuilder;
48-
import org.springframework.integration.test.support.LongRunningIntegrationTest;
4947
import org.springframework.messaging.Message;
5048
import org.springframework.messaging.MessageChannel;
5149
import org.springframework.messaging.support.GenericMessage;
5250

5351
import static org.assertj.core.api.Assertions.assertThat;
52+
import static org.assertj.core.api.Assertions.assertThatIOException;
5453
import static org.assertj.core.api.Assertions.fail;
5554
import static org.mockito.Mockito.mock;
5655

@@ -62,9 +61,6 @@
6261
*/
6362
public class DeserializationTests {
6463

65-
@Rule
66-
public LongRunningIntegrationTest longRunningIntegrationTest = new LongRunningIntegrationTest();
67-
6864
@Test
6965
public void testReadLength() throws Exception {
7066
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
@@ -245,18 +241,11 @@ public void testReadCrLfTimeout() throws Exception {
245241
server.setSoTimeout(10000);
246242
CountDownLatch latch = SocketTestUtils.testSendCrLfOverflow(port);
247243
Socket socket = server.accept();
248-
socket.setSoTimeout(500);
244+
socket.setSoTimeout(100);
249245
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
250-
try {
251-
serializer.deserialize(socket.getInputStream());
252-
fail("Expected timout exception");
253-
}
254-
catch (IOException e) {
255-
if (!e.getMessage().startsWith("Read timed out")) {
256-
e.printStackTrace();
257-
fail("Unexpected IO Error:" + e.getMessage());
258-
}
259-
}
246+
assertThatIOException()
247+
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
248+
.withMessageStartingWith("Read timed out");
260249
server.close();
261250
latch.countDown();
262251
}
@@ -270,17 +259,10 @@ public void testReadCrLfOverflow() throws Exception {
270259
Socket socket = server.accept();
271260
socket.setSoTimeout(5000);
272261
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
273-
serializer.setMaxMessageSize(1024);
274-
try {
275-
serializer.deserialize(socket.getInputStream());
276-
fail("Expected message length exceeded exception");
277-
}
278-
catch (IOException e) {
279-
if (!e.getMessage().startsWith("CRLF not found")) {
280-
e.printStackTrace();
281-
fail("Unexpected IO Error:" + e.getMessage());
282-
}
283-
}
262+
serializer.setMaxMessageSize(16);
263+
assertThatIOException()
264+
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
265+
.withMessageStartingWith("CRLF not found");
284266
server.close();
285267
latch.countDown();
286268
}
@@ -317,7 +299,8 @@ public void deserializationEvents() throws Exception {
317299
assertThat(new String(event.getBuffer()).substring(0, 1)).isEqualTo(new String(new byte[] {7}));
318300
doDeserialize(new ByteArrayLfSerializer(), "Terminator '0xa' not found before max message length: 5");
319301
doDeserialize(new ByteArrayRawSerializer(), "Socket was not closed before max message length: 5");
320-
doDeserialize(new ByteArraySingleTerminatorSerializer((byte) 0xfe), "Terminator '0xfe' not found before max message length: 5");
302+
doDeserialize(new ByteArraySingleTerminatorSerializer((byte) 0xfe),
303+
"Terminator '0xfe' not found before max message length: 5");
321304
doDeserialize(new ByteArrayStxEtxSerializer(), "Expected STX to begin message");
322305
event = doDeserialize(new ByteArrayStxEtxSerializer(),
323306
"Socket closed during message assembly", new byte[] {0x02, 0, 0}, 5);
@@ -389,7 +372,7 @@ public void testTimeoutWhileDecoding(AbstractByteArraySerializer deserializer, S
389372
TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
390373
clientNio.setSerializer(serializer);
391374
clientNio.setDeserializer(deserializer);
392-
clientNio.setSoTimeout(1000);
375+
clientNio.setSoTimeout(500);
393376
clientNio.afterPropertiesSet();
394377
final TcpOutboundGateway out = new TcpOutboundGateway();
395378
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)