Tweak sme TCP tests to minimize time and memory

**Auto-cherry-pick to `6.3.x` & `6.2.x`**
This commit is contained in:
Artem Bilan
2024-07-24 10:49:09 -04:00
parent d4f136a939
commit 201acf5405
3 changed files with 22 additions and 38 deletions

View File

@@ -296,7 +296,7 @@ public class TcpNioConnectionReadTests {
@Test
public void testReadCrLfOverflow() throws Exception {
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
serializer.setMaxMessageSize(1024);
serializer.setMaxMessageSize(16);
final Semaphore semaphore = new Semaphore(0);
final List<TcpConnection> added = new ArrayList<>();
final List<TcpConnection> removed = new ArrayList<>();
@@ -332,14 +332,14 @@ public class TcpNioConnectionReadTests {
whileOpen(semaphore, added);
assertThat(added.size()).isEqualTo(1);
assertThat(errorMessageLetch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(errorMessageLetch.await(20, TimeUnit.SECONDS)).isTrue();
assertThat(errorMessageRef.get().getMessage())
.satisfiesAnyOf(
s -> assertThat(s).contains("CRLF not found before max message length: 1024"),
s -> assertThat(s).contains("CRLF not found before max message length: 16"),
s -> assertThat(s).contains("Connection is closed"));
assertThat(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS)).isTrue();
assertThat(semaphore.tryAcquire(20, TimeUnit.SECONDS)).isTrue();
assertThat(removed).hasSizeGreaterThan(0);
scf.stop();
done.countDown();
@@ -531,9 +531,9 @@ public class TcpNioConnectionReadTests {
scf.stop();
}
private void whileOpen(Semaphore semaphore, final List<TcpConnection> added)
private void whileOpen(Semaphore semaphore, List<TcpConnection> added)
throws InterruptedException {
assertThat(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS)).isTrue();
assertThat(semaphore.tryAcquire(20, TimeUnit.SECONDS)).isTrue();
with().pollInterval(Duration.ofMillis(50)).await("Failed to close socket")
.atMost(Duration.ofSeconds(20))
.until(() -> !added.get(0).isOpen());

View File

@@ -28,8 +28,7 @@ import java.util.concurrent.atomic.AtomicReference;
import javax.net.ServerSocketFactory;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.serializer.DefaultDeserializer;
@@ -42,12 +41,12 @@ import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionF
import org.springframework.integration.ip.util.SocketTestUtils;
import org.springframework.integration.ip.util.TestingUtilities;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.support.LongRunningIntegrationTest;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
@@ -60,9 +59,6 @@ import static org.mockito.Mockito.mock;
*/
public class DeserializationTests {
@Rule
public LongRunningIntegrationTest longRunningIntegrationTest = new LongRunningIntegrationTest();
@Test
public void testReadLength() throws Exception {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
@@ -240,17 +236,11 @@ public class DeserializationTests {
server.setSoTimeout(10000);
CountDownLatch latch = SocketTestUtils.testSendCrLfOverflow(port);
Socket socket = server.accept();
socket.setSoTimeout(500);
socket.setSoTimeout(100);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
try {
serializer.deserialize(socket.getInputStream());
fail("Expected timout exception");
}
catch (IOException e) {
if (!e.getMessage().startsWith("Read timed out")) {
fail("Unexpected IO Error:" + e.getMessage());
}
}
assertThatIOException()
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
.withMessageStartingWith("Read timed out");
server.close();
latch.countDown();
}
@@ -264,16 +254,10 @@ public class DeserializationTests {
Socket socket = server.accept();
socket.setSoTimeout(5000);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
serializer.setMaxMessageSize(1024);
try {
serializer.deserialize(socket.getInputStream());
fail("Expected message length exceeded exception");
}
catch (IOException e) {
if (!e.getMessage().startsWith("CRLF not found")) {
fail("Unexpected IO Error:" + e.getMessage());
}
}
serializer.setMaxMessageSize(16);
assertThatIOException()
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
.withMessageStartingWith("CRLF not found");
server.close();
latch.countDown();
}
@@ -306,7 +290,8 @@ public class DeserializationTests {
assertThat(new String(event.getBuffer()).substring(0, 1)).isEqualTo(new String(new byte[] {7}));
doDeserialize(new ByteArrayLfSerializer(), "Terminator '0xa' not found before max message length: 5");
doDeserialize(new ByteArrayRawSerializer(), "Socket was not closed before max message length: 5");
doDeserialize(new ByteArraySingleTerminatorSerializer((byte) 0xfe), "Terminator '0xfe' not found before max message length: 5");
doDeserialize(new ByteArraySingleTerminatorSerializer((byte) 0xfe),
"Terminator '0xfe' not found before max message length: 5");
doDeserialize(new ByteArrayStxEtxSerializer(), "Expected STX to begin message");
event = doDeserialize(new ByteArrayStxEtxSerializer(),
"Socket closed during message assembly", new byte[] {0x02, 0, 0}, 5);
@@ -365,7 +350,7 @@ public class DeserializationTests {
TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
clientNio.setSerializer(serializer);
clientNio.setDeserializer(deserializer);
clientNio.setSoTimeout(1000);
clientNio.setSoTimeout(500);
clientNio.afterPropertiesSet();
final TcpOutboundGateway out = new TcpOutboundGateway();
out.setConnectionFactory(clientNio);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -325,16 +325,15 @@ public class SocketTestUtils {
Thread thread = new Thread(() -> {
try (Socket socket = new Socket(InetAddress.getLocalHost(), port)) {
OutputStream outputStream = socket.getOutputStream();
for (int i = 0; i < 1500; i++) {
for (int i = 0; i < 20; i++) {
writeByte(outputStream, 'x', true);
}
testCompleteLatch.await(10, TimeUnit.SECONDS);
testCompleteLatch.await(30, TimeUnit.SECONDS);
}
catch (Exception e) {
}
});
thread.setDaemon(true);
thread.start();
return testCompleteLatch;
}