|
68 | 68 | import java.security.spec.MGF1ParameterSpec; |
69 | 69 | import java.security.spec.PSSParameterSpec; |
70 | 70 | import java.util.ArrayList; |
| 71 | +import java.util.Collections; |
71 | 72 | import java.util.HashMap; |
72 | 73 | import java.util.HashSet; |
73 | 74 | import java.util.List; |
|
80 | 81 | import java.util.concurrent.LinkedBlockingQueue; |
81 | 82 | import java.util.concurrent.TimeUnit; |
82 | 83 | import java.util.concurrent.atomic.AtomicBoolean; |
| 84 | +import java.util.concurrent.atomic.AtomicInteger; |
83 | 85 | import java.util.concurrent.atomic.AtomicReference; |
84 | 86 | import java.util.function.Consumer; |
85 | 87 |
|
86 | | -import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
87 | | -import static org.junit.jupiter.api.Assertions.assertSame; |
88 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
89 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
90 | | -import static org.junit.jupiter.api.Assertions.fail; |
91 | 88 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 89 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
92 | 90 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
93 | 91 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
94 | 92 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 93 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 94 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 95 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 96 | +import static org.junit.jupiter.api.Assertions.fail; |
95 | 97 |
|
96 | 98 | public class QuicChannelConnectTest extends AbstractQuicTest { |
97 | 99 |
|
@@ -1861,6 +1863,110 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { |
1861 | 1863 | } |
1862 | 1864 | } |
1863 | 1865 |
|
| 1866 | + @ParameterizedTest |
| 1867 | + @MethodSource("newSslTaskExecutors") |
| 1868 | + @Timeout(10) |
| 1869 | + public void testConnectionCloseFrameDeliveredWhenParentClosedImmediately(Executor executor) throws Throwable { |
| 1870 | + int numConnections = 10; |
| 1871 | + int streamsPerConnection = 50; |
| 1872 | + |
| 1873 | + CountDownLatch serverStreamsLatch = new CountDownLatch(numConnections * streamsPerConnection); |
| 1874 | + List<QuicChannel> serverConnections = Collections.synchronizedList(new ArrayList<>(numConnections)); |
| 1875 | + |
| 1876 | + Channel server = QuicTestUtils.newServer(executor, new ChannelInboundHandler() { |
| 1877 | + @Override |
| 1878 | + public boolean isSharable() { |
| 1879 | + return true; |
| 1880 | + } |
| 1881 | + |
| 1882 | + @Override |
| 1883 | + public void channelActive(ChannelHandlerContext ctx) { |
| 1884 | + serverConnections.add((QuicChannel) ctx.channel()); |
| 1885 | + ctx.fireChannelActive(); |
| 1886 | + } |
| 1887 | + }, new ChannelInboundHandler() { |
| 1888 | + @Override |
| 1889 | + public boolean isSharable() { |
| 1890 | + return true; |
| 1891 | + } |
| 1892 | + |
| 1893 | + @Override |
| 1894 | + public void channelActive(ChannelHandlerContext ctx) { |
| 1895 | + serverStreamsLatch.countDown(); |
| 1896 | + ctx.fireChannelActive(); |
| 1897 | + } |
| 1898 | + }); |
| 1899 | + |
| 1900 | + server.pipeline().addFirst(new ChannelOutboundHandler() { |
| 1901 | + // simulate delay for writing and flushing packets so it would race with closing. |
| 1902 | + @Override |
| 1903 | + public void write(ChannelHandlerContext ctx, Object msg, CompletionHandler<Void> callback) { |
| 1904 | + ctx.executor().execute(() -> ctx.write(msg, callback)); |
| 1905 | + } |
| 1906 | + |
| 1907 | + @Override |
| 1908 | + public void flush(ChannelHandlerContext ctx) { |
| 1909 | + ctx.executor().execute(ctx::flush); |
| 1910 | + } |
| 1911 | + }); |
| 1912 | + InetSocketAddress address = (InetSocketAddress) server.localAddress(); |
| 1913 | + Channel channel = QuicTestUtils.newClient(executor); |
| 1914 | + |
| 1915 | + CountDownLatch clientConnectionsLatch = new CountDownLatch(numConnections); |
| 1916 | + try { |
| 1917 | + for (int i = 0; i < numConnections; i++) { |
| 1918 | + QuicChannelBootstrap quicChannelBootstrap = QuicTestUtils.newQuicChannelBootstrap(channel); |
| 1919 | + quicChannelBootstrap.handler(new ChannelInboundHandler() { |
| 1920 | + @Override |
| 1921 | + public boolean isSharable() { |
| 1922 | + return true; |
| 1923 | + } |
| 1924 | + |
| 1925 | + @Override |
| 1926 | + public void userEventTriggered(ChannelHandlerContext ctx, |
| 1927 | + Object evt) { |
| 1928 | + if (evt instanceof QuicConnectionCloseEvent) { |
| 1929 | + clientConnectionsLatch.countDown(); |
| 1930 | + } |
| 1931 | + ctx.fireUserEventTriggered(evt); |
| 1932 | + } |
| 1933 | + }); |
| 1934 | + QuicChannel quicChannel = quicChannelBootstrap.streamHandler(new ChannelInboundHandler() { }) |
| 1935 | + .remoteAddress(address) |
| 1936 | + .connect() |
| 1937 | + .get(); |
| 1938 | + |
| 1939 | + for (int j = 0; j < streamsPerConnection; j++) { |
| 1940 | + QuicStreamChannel stream = |
| 1941 | + quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, new ChannelInboundHandler() { }) |
| 1942 | + .sync().getNow(); |
| 1943 | + stream.writeAndFlush(Unpooled.directBuffer().writeZero(1)).sync(); |
| 1944 | + } |
| 1945 | + } |
| 1946 | + |
| 1947 | + assertTrue(serverStreamsLatch.await(5, TimeUnit.SECONDS), "Server did not receive all streams"); |
| 1948 | + assertEquals(numConnections, serverConnections.size()); |
| 1949 | + |
| 1950 | + AtomicInteger closedConnections = new AtomicInteger(); |
| 1951 | + server.executor().execute(() -> { |
| 1952 | + for (int i = 0; i < numConnections; i++) { |
| 1953 | + serverConnections.get(i).close().addListener(v -> { |
| 1954 | + if (closedConnections.incrementAndGet() == numConnections) { |
| 1955 | + server.close(); |
| 1956 | + } |
| 1957 | + }); |
| 1958 | + } |
| 1959 | + }); |
| 1960 | + |
| 1961 | + assertTrue(clientConnectionsLatch.await(5, TimeUnit.SECONDS), |
| 1962 | + "Not all clients received QuicConnectionCloseEvent"); |
| 1963 | + } finally { |
| 1964 | + server.close().sync(); |
| 1965 | + channel.close().sync(); |
| 1966 | + shutdown(executor); |
| 1967 | + } |
| 1968 | + } |
| 1969 | + |
1864 | 1970 | private static final class ChannelStateVerifyHandler extends QuicChannelValidationHandler { |
1865 | 1971 | @Override |
1866 | 1972 | public void channelActive(ChannelHandlerContext ctx) { |
|