### Question:
Http2FrameCodec extends Http2ConnectionHandler { DefaultHttp2FrameStream newStream() { return new DefaultHttp2FrameStream(); } Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings); @Override final void handlerAdded(ChannelHandlerContext ctx); @Override final void userEventTriggered(ChannelHandlerContext ctx, Object evt); @Override void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise);  }### Answer:
@Test(timeout = 5000) public void newOutboundStream() { final Http2FrameStream stream = frameCodec.newStream(); assertNotNull(stream); assertFalse(isStreamIdValid(stream.id())); final Promise<Void> listenerExecuted = new DefaultPromise<Void>(GlobalEventExecutor.INSTANCE); channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers(), false).stream(stream)) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { assertTrue(future.isSuccess()); assertTrue(isStreamIdValid(stream.id())); listenerExecuted.setSuccess(null); } } ); ByteBuf data = Unpooled.buffer().writeZero(100); ChannelFuture f = channel.writeAndFlush(new DefaultHttp2DataFrame(data).stream(stream)); assertTrue(f.isSuccess()); listenerExecuted.syncUninterruptibly(); assertTrue(listenerExecuted.isSuccess()); }
@Test public void newOutboundStreamsShouldBeBuffered() throws Exception { setUp(Http2FrameCodecBuilder.forServer().encoderEnforceMaxConcurrentStreams(true), new Http2Settings().maxConcurrentStreams(1)); Http2FrameStream stream1 = frameCodec.newStream(); Http2FrameStream stream2 = frameCodec.newStream(); ChannelPromise promise1 = channel.newPromise(); ChannelPromise promise2 = channel.newPromise(); channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream1), promise1); channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream2), promise2); assertTrue(isStreamIdValid(stream1.id())); channel.runPendingTasks(); assertTrue(isStreamIdValid(stream2.id())); assertTrue(promise1.syncUninterruptibly().isSuccess()); assertFalse(promise2.isDone()); frameInboundWriter.writeInboundSettings(new Http2Settings().maxConcurrentStreams(2)); channel.flush(); assertTrue(promise2.syncUninterruptibly().isSuccess()); }
@Test public void multipleNewOutboundStreamsShouldBeBuffered() throws Exception { setUp(Http2FrameCodecBuilder.forServer().encoderEnforceMaxConcurrentStreams(true), new Http2Settings().maxConcurrentStreams(1)); Http2FrameStream stream1 = frameCodec.newStream(); Http2FrameStream stream2 = frameCodec.newStream(); Http2FrameStream stream3 = frameCodec.newStream(); ChannelPromise promise1 = channel.newPromise(); ChannelPromise promise2 = channel.newPromise(); ChannelPromise promise3 = channel.newPromise(); channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream1), promise1); channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream2), promise2); channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream3), promise3); assertTrue(isStreamIdValid(stream1.id())); channel.runPendingTasks(); assertTrue(isStreamIdValid(stream2.id())); assertTrue(promise1.syncUninterruptibly().isSuccess()); assertFalse(promise2.isDone()); assertFalse(promise3.isDone()); frameInboundWriter.writeInboundSettings(new Http2Settings().maxConcurrentStreams(2)); channel.flush(); assertTrue(promise2.syncUninterruptibly().isSuccess()); assertFalse(promise3.isDone()); frameInboundWriter.writeInboundSettings(new Http2Settings().maxConcurrentStreams(3)); channel.flush(); assertTrue(promise3.syncUninterruptibly().isSuccess()); assertFalse(channel.finishAndReleaseAll()); }