|
19 | 19 | import io.netty.buffer.Unpooled; |
20 | 20 | import io.netty.channel.EventLoopGroup; |
21 | 21 | import io.netty.channel.MultiThreadIoEventLoopGroup; |
| 22 | +import io.netty.channel.embedded.EmbeddedChannel; |
22 | 23 | import io.netty.channel.local.LocalIoHandler; |
23 | 24 | import io.netty.handler.codec.http2.Http2Connection.Endpoint; |
24 | 25 | import io.netty.handler.codec.http2.Http2Stream.State; |
|
34 | 35 | import org.mockito.invocation.InvocationOnMock; |
35 | 36 | import org.mockito.stubbing.Answer; |
36 | 37 |
|
| 38 | +import javax.annotation.Nonnull; |
37 | 39 | import java.util.concurrent.CountDownLatch; |
38 | 40 | import java.util.concurrent.TimeUnit; |
39 | 41 |
|
40 | 42 | import static java.lang.Integer.MAX_VALUE; |
41 | 43 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
42 | 45 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
43 | 46 | import static org.junit.jupiter.api.Assertions.assertNull; |
44 | 47 | import static org.junit.jupiter.api.Assertions.assertThrows; |
@@ -700,6 +703,183 @@ public void execute() throws Throwable { |
700 | 703 | }); |
701 | 704 | } |
702 | 705 |
|
| 706 | + @Test |
| 707 | + public void defaultSettingsShouldEnforceMaxConcurrentStreamsOnRemoteEndpoint() throws Exception { |
| 708 | + // Build a server handler using default settings (no explicit maxConcurrentStreams override). |
| 709 | + Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder() |
| 710 | + .frameListener(new Http2FrameAdapter()) |
| 711 | + .build(); |
| 712 | + |
| 713 | + EmbeddedChannel channel = new EmbeddedChannel(handler); |
| 714 | + try { |
| 715 | + // Feed the client connection preface. |
| 716 | + assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf())); |
| 717 | + |
| 718 | + ByteBuf clientSettings = clientSettingsWithoutMaxConcurrentStreams(); |
| 719 | + assertFalse(channel.writeInbound(clientSettings)); |
| 720 | + |
| 721 | + Http2Connection connection = handler.connection(); |
| 722 | + |
| 723 | + // The server's default (SMALLEST_MAX_CONCURRENT_STREAMS = 100) must be |
| 724 | + // enforced on the remote endpoint — the one that tracks client-initiated streams. |
| 725 | + assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS, |
| 726 | + connection.remote().maxActiveStreams()); |
| 727 | + |
| 728 | + // Create exactly the maximum allowed client-initiated (odd-numbered) streams. |
| 729 | + for (int id = 1; id < Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2; id += 2) { |
| 730 | + connection.remote().createStream(id, true); |
| 731 | + } |
| 732 | + assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS, |
| 733 | + connection.numActiveStreams()); |
| 734 | + |
| 735 | + // The next stream must be refused. |
| 736 | + final int nextStreamId = Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2 + 1; |
| 737 | + Http2Exception e = assertThrows(Http2Exception.class, new Executable() { |
| 738 | + @Override |
| 739 | + public void execute() throws Throwable { |
| 740 | + handler.connection().remote().createStream(nextStreamId, true); |
| 741 | + } |
| 742 | + }); |
| 743 | + assertEquals(Http2Error.REFUSED_STREAM, e.error()); |
| 744 | + } finally { |
| 745 | + channel.finishAndReleaseAll(); |
| 746 | + } |
| 747 | + } |
| 748 | + |
| 749 | + @Test |
| 750 | + public void customMaxConcurrentStreamsShouldBeEnforcedOnRemoteEndpoint() throws Exception { |
| 751 | + final int maxConcurrentStreams = 150; |
| 752 | + |
| 753 | + Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder() |
| 754 | + .frameListener(new Http2FrameAdapter()) |
| 755 | + .initialSettings(new Http2Settings().maxConcurrentStreams(maxConcurrentStreams)) |
| 756 | + .build(); |
| 757 | + |
| 758 | + EmbeddedChannel channel = new EmbeddedChannel(handler); |
| 759 | + try { |
| 760 | + assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf())); |
| 761 | + |
| 762 | + ByteBuf clientSettings = clientSettingsWithoutMaxConcurrentStreams(); |
| 763 | + assertFalse(channel.writeInbound(clientSettings)); |
| 764 | + |
| 765 | + Http2Connection connection = handler.connection(); |
| 766 | + |
| 767 | + assertEquals(maxConcurrentStreams, connection.remote().maxActiveStreams()); |
| 768 | + |
| 769 | + // Create exactly the configured limit of client-initiated streams. |
| 770 | + for (int id = 1; id < maxConcurrentStreams * 2; id += 2) { |
| 771 | + connection.remote().createStream(id, true); |
| 772 | + } |
| 773 | + assertEquals(maxConcurrentStreams, connection.numActiveStreams()); |
| 774 | + |
| 775 | + // The next stream must be refused. |
| 776 | + final int nextStreamId = maxConcurrentStreams * 2 + 1; |
| 777 | + Http2Exception e = assertThrows(Http2Exception.class, new Executable() { |
| 778 | + @Override |
| 779 | + public void execute() throws Throwable { |
| 780 | + handler.connection().remote().createStream(nextStreamId, true); |
| 781 | + } |
| 782 | + }); |
| 783 | + assertEquals(Http2Error.REFUSED_STREAM, e.error()); |
| 784 | + } finally { |
| 785 | + channel.finishAndReleaseAll(); |
| 786 | + } |
| 787 | + } |
| 788 | + |
| 789 | + @Test |
| 790 | + public void defaultSettingsShouldEnforceMaxConcurrentStreamsOnRemoteEndpointWithCodec() |
| 791 | + throws Exception { |
| 792 | + DefaultHttp2Connection connection = new DefaultHttp2Connection(true); |
| 793 | + DefaultHttp2FrameWriter frameWriter = new DefaultHttp2FrameWriter(); |
| 794 | + Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter); |
| 795 | + Http2ConnectionDecoder decoder = |
| 796 | + new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader()); |
| 797 | + |
| 798 | + Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder() |
| 799 | + .frameListener(new Http2FrameAdapter()) |
| 800 | + .codec(decoder, encoder) |
| 801 | + .build(); |
| 802 | + |
| 803 | + EmbeddedChannel channel = new EmbeddedChannel(handler); |
| 804 | + try { |
| 805 | + assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf())); |
| 806 | + assertFalse(channel.writeInbound(clientSettingsWithoutMaxConcurrentStreams())); |
| 807 | + |
| 808 | + assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS, |
| 809 | + connection.remote().maxActiveStreams()); |
| 810 | + |
| 811 | + for (int id = 1; id < Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2; id += 2) { |
| 812 | + connection.remote().createStream(id, true); |
| 813 | + } |
| 814 | + assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS, connection.numActiveStreams()); |
| 815 | + |
| 816 | + final int nextStreamId = Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2 + 1; |
| 817 | + Http2Exception e = assertThrows(Http2Exception.class, new Executable() { |
| 818 | + @Override |
| 819 | + public void execute() throws Throwable { |
| 820 | + connection.remote().createStream(nextStreamId, true); |
| 821 | + } |
| 822 | + }); |
| 823 | + assertEquals(Http2Error.REFUSED_STREAM, e.error()); |
| 824 | + } finally { |
| 825 | + channel.finishAndReleaseAll(); |
| 826 | + } |
| 827 | + } |
| 828 | + |
| 829 | + @Test |
| 830 | + public void customMaxConcurrentStreamsShouldBeEnforcedOnRemoteEndpointWithCodec() |
| 831 | + throws Exception { |
| 832 | + final int maxConcurrentStreams = 150; |
| 833 | + |
| 834 | + DefaultHttp2Connection connection = new DefaultHttp2Connection(true); |
| 835 | + DefaultHttp2FrameWriter frameWriter = new DefaultHttp2FrameWriter(); |
| 836 | + Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter); |
| 837 | + Http2ConnectionDecoder decoder = |
| 838 | + new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader()); |
| 839 | + |
| 840 | + Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder() |
| 841 | + .frameListener(new Http2FrameAdapter()) |
| 842 | + .initialSettings(new Http2Settings().maxConcurrentStreams(maxConcurrentStreams)) |
| 843 | + .codec(decoder, encoder) |
| 844 | + .build(); |
| 845 | + |
| 846 | + EmbeddedChannel channel = new EmbeddedChannel(handler); |
| 847 | + try { |
| 848 | + assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf())); |
| 849 | + assertFalse(channel.writeInbound(clientSettingsWithoutMaxConcurrentStreams())); |
| 850 | + |
| 851 | + assertEquals(maxConcurrentStreams, connection.remote().maxActiveStreams()); |
| 852 | + |
| 853 | + for (int id = 1; id < maxConcurrentStreams * 2; id += 2) { |
| 854 | + connection.remote().createStream(id, true); |
| 855 | + } |
| 856 | + assertEquals(maxConcurrentStreams, connection.numActiveStreams()); |
| 857 | + |
| 858 | + final int nextStreamId = maxConcurrentStreams * 2 + 1; |
| 859 | + Http2Exception e = assertThrows(Http2Exception.class, new Executable() { |
| 860 | + @Override |
| 861 | + public void execute() throws Throwable { |
| 862 | + connection.remote().createStream(nextStreamId, true); |
| 863 | + } |
| 864 | + }); |
| 865 | + assertEquals(Http2Error.REFUSED_STREAM, e.error()); |
| 866 | + } finally { |
| 867 | + channel.finishAndReleaseAll(); |
| 868 | + } |
| 869 | + } |
| 870 | + |
| 871 | + @Nonnull |
| 872 | + private static ByteBuf clientSettingsWithoutMaxConcurrentStreams() { |
| 873 | + ByteBuf clientSettings = Unpooled.buffer(); |
| 874 | + clientSettings.writeMedium(6); // Payload length: one 6-byte setting |
| 875 | + clientSettings.writeByte(0x4); // Frame type: SETTINGS |
| 876 | + clientSettings.writeByte(0x0); // Flags |
| 877 | + clientSettings.writeInt(0x0); // Stream 0 |
| 878 | + clientSettings.writeShort(0x4); // SETTINGS_INITIAL_WINDOW_SIZE |
| 879 | + clientSettings.writeInt(65535); |
| 880 | + return clientSettings; |
| 881 | + } |
| 882 | + |
703 | 883 | private static void incrementAndGetStreamShouldSucceed(Endpoint<?> endpoint) throws Http2Exception { |
704 | 884 | Http2Stream streamA = endpoint.createStream(endpoint.incrementAndGetNextStreamId(), true); |
705 | 885 | Http2Stream streamB = endpoint.createStream(streamA.id() + 2, true); |
|