|
1 | 1 | /* |
2 | | - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2021 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
39 | 39 | import java.util.concurrent.atomic.AtomicReference; |
40 | 40 |
|
41 | 41 | import javax.net.ServerSocketFactory; |
| 42 | +import javax.net.SocketFactory; |
42 | 43 |
|
43 | 44 | import org.apache.commons.logging.Log; |
44 | 45 | import org.apache.commons.logging.LogFactory; |
45 | 46 | import org.junit.Test; |
46 | 47 | import org.mockito.Mockito; |
47 | | - |
48 | 48 | import org.springframework.beans.factory.BeanFactory; |
| 49 | +import org.springframework.context.ApplicationEventPublisher; |
49 | 50 | import org.springframework.context.support.AbstractApplicationContext; |
50 | 51 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
51 | 52 | import org.springframework.core.serializer.DefaultDeserializer; |
|
57 | 58 | import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory; |
58 | 59 | import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory; |
59 | 60 | import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; |
| 61 | +import org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent; |
60 | 62 | import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactory; |
61 | 63 | import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain; |
62 | 64 | import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory; |
| 65 | +import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory; |
63 | 66 | import org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory; |
64 | 67 | import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer; |
65 | 68 | import org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer; |
@@ -1191,4 +1194,38 @@ public void testConnectionException() throws Exception { |
1191 | 1194 | } |
1192 | 1195 | } |
1193 | 1196 |
|
| 1197 | + @Test |
| 1198 | + public void testInterceptedCleanup() throws Exception { |
| 1199 | + final CountDownLatch latch = new CountDownLatch(1); |
| 1200 | + AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0); |
| 1201 | + ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer(); |
| 1202 | + scf.setSerializer(serializer); |
| 1203 | + scf.setDeserializer(serializer); |
| 1204 | + TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter(); |
| 1205 | + adapter.setConnectionFactory(scf); |
| 1206 | + TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); |
| 1207 | + handler.setConnectionFactory(scf); |
| 1208 | + scf.setApplicationEventPublisher(new ApplicationEventPublisher() { |
| 1209 | + |
| 1210 | + @Override |
| 1211 | + public void publishEvent(Object event) { |
| 1212 | + if (event instanceof TcpConnectionCloseEvent) { |
| 1213 | + latch.countDown(); |
| 1214 | + } |
| 1215 | + } |
| 1216 | + }); |
| 1217 | + TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain(); |
| 1218 | + fc.setInterceptors(new TcpConnectionInterceptorFactory[] { |
| 1219 | + newInterceptorFactory(scf.getApplicationEventPublisher()), |
| 1220 | + }); |
| 1221 | + scf.setInterceptorFactoryChain(fc); |
| 1222 | + scf.start(); |
| 1223 | + TestingUtilities.waitListening(scf, null); |
| 1224 | + int port = scf.getPort(); |
| 1225 | + Socket socket = SocketFactory.getDefault().createSocket("localhost", port); |
| 1226 | + socket.close(); |
| 1227 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 1228 | + assertThat(handler.getConnections().isEmpty()).isTrue(); |
| 1229 | + scf.stop(); |
| 1230 | + } |
1194 | 1231 | } |
0 commit comments