|
16 | 16 |
|
17 | 17 | package org.springframework.integration.ip.tcp.connection;
|
18 | 18 |
|
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
19 | 21 | import static org.junit.Assert.assertEquals;
|
20 | 22 | import static org.junit.Assert.assertNotNull;
|
21 | 23 | import static org.mockito.Mockito.doAnswer;
|
22 | 24 | import static org.mockito.Mockito.mock;
|
23 | 25 | import static org.mockito.Mockito.when;
|
24 | 26 |
|
25 | 27 | import java.io.ByteArrayOutputStream;
|
| 28 | +import java.io.IOException; |
26 | 29 | import java.io.InputStream;
|
27 | 30 | import java.io.PipedInputStream;
|
28 | 31 | import java.io.PipedOutputStream;
|
29 | 32 | import java.net.Socket;
|
30 | 33 | import java.nio.ByteBuffer;
|
31 | 34 | import java.nio.channels.SocketChannel;
|
| 35 | +import java.util.concurrent.CountDownLatch; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
32 | 38 | import java.util.concurrent.atomic.AtomicReference;
|
33 | 39 |
|
| 40 | +import javax.net.SocketFactory; |
| 41 | + |
34 | 42 | import org.apache.commons.logging.Log;
|
35 | 43 | import org.junit.Test;
|
36 | 44 | import org.mockito.Mockito;
|
37 | 45 |
|
38 | 46 | import org.springframework.beans.DirectFieldAccessor;
|
| 47 | +import org.springframework.context.ApplicationEventPublisher; |
39 | 48 | import org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream;
|
40 | 49 | import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer;
|
41 | 50 | import org.springframework.integration.ip.tcp.serializer.MapJsonSerializer;
|
| 51 | +import org.springframework.integration.ip.tcp.serializer.SoftEndOfStreamException; |
42 | 52 | import org.springframework.integration.support.MessageBuilder;
|
43 | 53 | import org.springframework.integration.support.converter.MapMessageConverter;
|
44 | 54 | import org.springframework.integration.test.util.TestUtils;
|
@@ -137,4 +147,30 @@ public void transferHeaders() throws Exception {
|
137 | 147 | assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
|
138 | 148 | }
|
139 | 149 |
|
| 150 | + @Test |
| 151 | + public void socketClosedNextRead() throws InterruptedException, IOException { |
| 152 | + TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0); |
| 153 | + AtomicInteger port = new AtomicInteger(); |
| 154 | + CountDownLatch latch = new CountDownLatch(1); |
| 155 | + ApplicationEventPublisher publisher = ev -> { |
| 156 | + if (ev instanceof TcpConnectionServerListeningEvent) { |
| 157 | + port.set(((TcpConnectionServerListeningEvent) ev).getPort()); |
| 158 | + latch.countDown(); |
| 159 | + } |
| 160 | + }; |
| 161 | + server.setApplicationEventPublisher(publisher); |
| 162 | + server.registerListener(message -> { |
| 163 | + return false; |
| 164 | + }); |
| 165 | + server.afterPropertiesSet(); |
| 166 | + server.start(); |
| 167 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 168 | + Socket socket = SocketFactory.getDefault().createSocket("localhost", port.get()); |
| 169 | + TcpNetConnection connection = new TcpNetConnection(socket, false, false, publisher, "socketClosedNextRead"); |
| 170 | + socket.close(); |
| 171 | + assertThatThrownBy(() -> connection.getPayload()) |
| 172 | + .isInstanceOf(SoftEndOfStreamException.class); |
| 173 | + server.stop(); |
| 174 | + } |
| 175 | + |
140 | 176 | }
|
0 commit comments