Mitigate some timing and memory concerns in TCP/IP tests
This commit is contained in:
@@ -241,7 +241,7 @@ public class TcpNioConnectionReadTests {
|
||||
@Test
|
||||
public void testReadStxEtxOverflow() throws Exception {
|
||||
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
|
||||
serializer.setMaxMessageSize(1024);
|
||||
serializer.setMaxMessageSize(8);
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
final List<TcpConnection> added = new ArrayList<>();
|
||||
final List<TcpConnection> removed = new ArrayList<>();
|
||||
@@ -284,7 +284,7 @@ public class TcpNioConnectionReadTests {
|
||||
|
||||
assertThat(errorMessageRef.get().getMessage())
|
||||
.satisfiesAnyOf(
|
||||
s -> assertThat(s).contains("ETX not found before max message length: 1024"),
|
||||
s -> assertThat(s).contains("ETX not found before max message length: 8"),
|
||||
s -> assertThat(s).contains("Connection is closed"));
|
||||
|
||||
assertThat(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS)).isTrue();
|
||||
@@ -347,7 +347,6 @@ public class TcpNioConnectionReadTests {
|
||||
|
||||
/**
|
||||
* Tests socket closure when no data received.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testCloseCleanupNoData() throws Exception {
|
||||
@@ -400,7 +399,6 @@ public class TcpNioConnectionReadTests {
|
||||
|
||||
/**
|
||||
* Tests socket closure when no data received.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testCloseCleanupPartialData() throws Exception {
|
||||
@@ -454,7 +452,6 @@ public class TcpNioConnectionReadTests {
|
||||
|
||||
/**
|
||||
* Tests socket closure when mid-message
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testCloseCleanupCrLf() throws Exception {
|
||||
@@ -464,7 +461,6 @@ public class TcpNioConnectionReadTests {
|
||||
|
||||
/**
|
||||
* Tests socket closure when mid-message
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testCloseCleanupStxEtx() throws Exception {
|
||||
@@ -474,7 +470,6 @@ public class TcpNioConnectionReadTests {
|
||||
|
||||
/**
|
||||
* Tests socket closure when mid-message
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testCloseCleanupLengthHeader() throws Exception {
|
||||
|
||||
@@ -46,8 +46,9 @@ 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.assertThatException;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIOException;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -135,13 +136,8 @@ public class DeserializationTests {
|
||||
ByteArrayElasticRawDeserializer serializer = new ByteArrayElasticRawDeserializer();
|
||||
byte[] out = serializer.deserialize(socket.getInputStream());
|
||||
assertThat(new String(out)).as("Data").isEqualTo(SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING);
|
||||
try {
|
||||
serializer.deserialize(socket.getInputStream());
|
||||
fail("Expected end of Stream");
|
||||
}
|
||||
catch (SoftEndOfStreamException e) {
|
||||
// NOSONAR
|
||||
}
|
||||
assertThatExceptionOfType(SoftEndOfStreamException.class)
|
||||
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()));
|
||||
server.close();
|
||||
}
|
||||
|
||||
@@ -171,15 +167,9 @@ public class DeserializationTests {
|
||||
Socket socket = server.accept();
|
||||
socket.setSoTimeout(5000);
|
||||
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
|
||||
try {
|
||||
serializer.deserialize(socket.getInputStream());
|
||||
fail("Expected message length exceeded exception");
|
||||
}
|
||||
catch (IOException e) {
|
||||
if (!e.getMessage().startsWith("Message length")) {
|
||||
fail("Unexpected IO Error:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
assertThatIOException()
|
||||
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
|
||||
.withMessageStartingWith("Message length");
|
||||
server.close();
|
||||
done.countDown();
|
||||
}
|
||||
@@ -193,15 +183,9 @@ public class DeserializationTests {
|
||||
Socket socket = server.accept();
|
||||
socket.setSoTimeout(500);
|
||||
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
|
||||
try {
|
||||
serializer.deserialize(socket.getInputStream());
|
||||
fail("Expected timeout 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();
|
||||
done.countDown();
|
||||
}
|
||||
@@ -215,16 +199,10 @@ public class DeserializationTests {
|
||||
Socket socket = server.accept();
|
||||
socket.setSoTimeout(5000);
|
||||
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
|
||||
serializer.setMaxMessageSize(1024);
|
||||
try {
|
||||
serializer.deserialize(socket.getInputStream());
|
||||
fail("Expected message length exceeded exception");
|
||||
}
|
||||
catch (IOException e) {
|
||||
if (!e.getMessage().startsWith("ETX not found")) {
|
||||
fail("Unexpected IO Error:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
serializer.setMaxMessageSize(8);
|
||||
assertThatIOException()
|
||||
.isThrownBy(() -> serializer.deserialize(socket.getInputStream()))
|
||||
.withMessageStartingWith("ETX not found");
|
||||
server.close();
|
||||
done.countDown();
|
||||
}
|
||||
@@ -310,15 +288,13 @@ public class DeserializationTests {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||
deser.setApplicationEventPublisher(anEvent -> event.set((TcpDeserializationExceptionEvent) anEvent));
|
||||
deser.setMaxMessageSize(mms);
|
||||
try {
|
||||
deser.deserialize(bais);
|
||||
fail("expected exception");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(event.get()).isNotNull();
|
||||
assertThat(event.get().getCause()).isSameAs(e);
|
||||
assertThat(e.getMessage()).contains(expectedMessage);
|
||||
}
|
||||
assertThatException()
|
||||
.isThrownBy(() -> deser.deserialize(bais))
|
||||
.withMessageContaining(expectedMessage)
|
||||
.satisfies((ex) -> {
|
||||
assertThat(event.get()).isNotNull();
|
||||
assertThat(event.get().getCause()).isSameAs(ex);
|
||||
});
|
||||
return event.get();
|
||||
}
|
||||
|
||||
@@ -335,6 +311,8 @@ public class DeserializationTests {
|
||||
private void testTimeoutWhileDecoding(AbstractByteArraySerializer deserializer, String reply) {
|
||||
ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
|
||||
TcpNioServerConnectionFactory serverNio = new TcpNioServerConnectionFactory(0);
|
||||
serverNio.setApplicationEventPublisher(event -> {
|
||||
});
|
||||
ByteArrayLengthHeaderSerializer lengthHeaderSerializer = new ByteArrayLengthHeaderSerializer(1);
|
||||
serverNio.setDeserializer(lengthHeaderSerializer);
|
||||
serverNio.setSerializer(serializer);
|
||||
@@ -348,6 +326,8 @@ public class DeserializationTests {
|
||||
in.start();
|
||||
TestingUtilities.waitListening(serverNio, null);
|
||||
TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
|
||||
clientNio.setApplicationEventPublisher(event -> {
|
||||
});
|
||||
clientNio.setSerializer(serializer);
|
||||
clientNio.setDeserializer(deserializer);
|
||||
clientNio.setSoTimeout(500);
|
||||
@@ -377,7 +357,7 @@ public class DeserializationTests {
|
||||
assertThat(new String((byte[]) message.getPayload())).isEqualTo("Test");
|
||||
String shortReply = reply.substring(0, reply.length() - 1);
|
||||
((MessageChannel) message.getHeaders().getReplyChannel()).send(new GenericMessage<>(shortReply));
|
||||
message = outputChannel.receive(1000);
|
||||
message = outputChannel.receive(100);
|
||||
assertThat(message).isNull();
|
||||
}
|
||||
|
||||
@@ -388,6 +368,8 @@ public class DeserializationTests {
|
||||
ByteArrayLengthHeaderSerializer lengthHeaderSerializer = new ByteArrayLengthHeaderSerializer(1);
|
||||
serverNio.setDeserializer(lengthHeaderSerializer);
|
||||
serverNio.setSerializer(serializer);
|
||||
serverNio.setApplicationEventPublisher(event -> {
|
||||
});
|
||||
serverNio.afterPropertiesSet();
|
||||
TcpInboundGateway in = new TcpInboundGateway();
|
||||
in.setConnectionFactory(serverNio);
|
||||
@@ -401,6 +383,8 @@ public class DeserializationTests {
|
||||
clientNio.setSerializer(serializer);
|
||||
clientNio.setDeserializer(new ByteArrayRawSerializer(true));
|
||||
clientNio.setSoTimeout(1000);
|
||||
clientNio.setApplicationEventPublisher(event -> {
|
||||
});
|
||||
clientNio.afterPropertiesSet();
|
||||
final TcpOutboundGateway out = new TcpOutboundGateway();
|
||||
out.setConnectionFactory(clientNio);
|
||||
|
||||
@@ -202,7 +202,7 @@ public class SocketTestUtils {
|
||||
try (Socket socket = new Socket(InetAddress.getLocalHost(), port)) {
|
||||
OutputStream outputStream = socket.getOutputStream();
|
||||
writeByte(outputStream, 0x02, true);
|
||||
for (int i = 0; i < 1500; i++) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
writeByte(outputStream, 'x', true);
|
||||
}
|
||||
testCompleteLatch.await(10, TimeUnit.SECONDS);
|
||||
|
||||
Reference in New Issue
Block a user