|
22 | 22 | import io.netty.channel.ChannelOutboundHandler; |
23 | 23 | import io.netty.channel.embedded.EmbeddedChannel; |
24 | 24 |
|
| 25 | +import io.netty.handler.codec.http.DefaultFullHttpRequest; |
25 | 26 | import io.netty.handler.codec.http.DefaultHttpContent; |
26 | 27 | import io.netty.handler.codec.http.DefaultHttpRequest; |
27 | 28 | import io.netty.handler.codec.http.HttpClientCodec; |
|
44 | 45 | import org.junit.jupiter.api.Test; |
45 | 46 |
|
46 | 47 | import java.util.ArrayDeque; |
47 | | -import java.util.ArrayList; |
48 | 48 | import java.util.Queue; |
49 | 49 | import java.util.concurrent.TimeUnit; |
50 | 50 |
|
@@ -512,21 +512,73 @@ public void testSimultaneousCloseDoesNotLeakOriginalPromise() throws Exception { |
512 | 512 | assertTrue(client.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE))); |
513 | 513 | assertFalse(server.writeInbound(client.<ByteBuf>readOutbound())); |
514 | 514 |
|
515 | | - // Both echo writes are stalled, but only a single deadline should govern the close handshake. |
516 | | - assertEquals(2, stalledWrites.size()); |
517 | | - // The second echo (triggered by the peer's inbound frame) is the one whose promise was cascaded |
518 | | - // from the original closeSent instead of overwriting it. |
519 | | - Promise<Void> peerEchoWrite = new ArrayList<Promise<Void>>(stalledWrites).get(1); |
| 515 | + // We already sent a close frame, so no second close frame must be written in response to the peer's |
| 516 | + // one. A single deadline governs the close handshake. |
| 517 | + assertEquals(1, stalledWrites.size()); |
520 | 518 | assertTrue(server.isOpen()); |
521 | 519 |
|
522 | 520 | server.advanceTimeBy(forceCloseTimeoutMillis * 2, TimeUnit.MILLISECONDS); |
523 | 521 | server.runScheduledPendingTasks(); |
524 | 522 |
|
525 | 523 | assertFalse(server.isOpen()); |
526 | | - // Neither promise is left hanging forever: the application's own close promise completes, and so |
527 | | - // does the promise cascaded from it for the peer's own (redundant) close frame. |
| 524 | + // The application's own close promise is not left hanging forever. |
528 | 525 | assertTrue(applicationClosePromise.isDone()); |
529 | | - assertTrue(peerEchoWrite.isDone()); |
| 526 | + |
| 527 | + client.close(); |
| 528 | + assertFalse(client.finishAndReleaseAll()); |
| 529 | + assertFalse(server.finishAndReleaseAll()); |
| 530 | + } |
| 531 | + |
| 532 | + @Test |
| 533 | + public void testServerInitiatedCloseHandshakeWithInboundCloseFrame() throws Exception { |
| 534 | + // Reproducer from https://github.com/netty/netty/issues/17776 |
| 535 | + EmbeddedChannel channel = new EmbeddedChannel( |
| 536 | + new HttpServerCodec(), new HttpObjectAggregator(65536), new WebSocketServerProtocolHandler("/ws")); |
| 537 | + |
| 538 | + FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/ws"); |
| 539 | + request.headers() |
| 540 | + .set(HttpHeaderNames.HOST, "localhost") |
| 541 | + .set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET) |
| 542 | + .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE) |
| 543 | + .set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==") |
| 544 | + .set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13"); |
| 545 | + assertFalse(channel.writeInbound(request)); |
| 546 | + ReferenceCountUtil.release(channel.readOutbound()); |
| 547 | + |
| 548 | + assertTrue(channel.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE))); |
| 549 | + ReferenceCountUtil.release(channel.readOutbound()); |
| 550 | + assertTrue(channel.isOpen()); |
| 551 | + |
| 552 | + // The "client" responds with a close frame, which must not try to reuse the already completed |
| 553 | + // closeSent promise but just close the connection. |
| 554 | + assertFalse(channel.writeInbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE))); |
| 555 | + assertFalse(channel.isOpen()); |
| 556 | + assertNull(channel.readOutbound()); |
| 557 | + |
| 558 | + assertFalse(channel.finishAndReleaseAll()); |
| 559 | + } |
| 560 | + |
| 561 | + @Test |
| 562 | + public void testServerInitiatedCloseHandshake() throws Exception { |
| 563 | + EmbeddedChannel client = createClient(); |
| 564 | + EmbeddedChannel server = createServer(); |
| 565 | + |
| 566 | + assertFalse(server.writeInbound(client.<ByteBuf>readOutbound())); |
| 567 | + assertFalse(client.writeInbound(server.<ByteBuf>readOutbound())); |
| 568 | + |
| 569 | + // The server initiates the close handshake. |
| 570 | + assertTrue(server.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE))); |
| 571 | + CloseWebSocketFrame closeMessage = decode(server.<ByteBuf>readOutbound(), CloseWebSocketFrame.class); |
| 572 | + assertEquals(WebSocketCloseStatus.NORMAL_CLOSURE.code(), closeMessage.statusCode()); |
| 573 | + closeMessage.release(); |
| 574 | + assertTrue(server.isOpen()); |
| 575 | + |
| 576 | + // The peer responds with its own close frame, which must complete the handshake by closing the |
| 577 | + // connection without sending another close frame. |
| 578 | + assertTrue(client.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE))); |
| 579 | + assertFalse(server.writeInbound(client.<ByteBuf>readOutbound())); |
| 580 | + assertFalse(server.isOpen()); |
| 581 | + assertNull(server.readOutbound()); |
530 | 582 |
|
531 | 583 | client.close(); |
532 | 584 | assertFalse(client.finishAndReleaseAll()); |
|