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