### Question:
BackpressureManagingHandler extends ChannelDuplexHandler { State getCurrentState() { return currentState; } protected  BackpressureManagingHandler(String thisHandlerName); @SuppressWarnings("fallthrough") @Override final void channelRead(ChannelHandlerContext ctx, Object msg); @Override void handlerAdded(ChannelHandlerContext ctx); @Override void handlerRemoved(ChannelHandlerContext ctx); @Override final void channelReadComplete(ChannelHandlerContext ctx); @Override final void read(ChannelHandlerContext ctx); @Override void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise); @Override void userEventTriggered(ChannelHandlerContext ctx, Object evt);  }### Answer:
@Test(timeout = 60000) public void testExactDemandAndSupply() throws Exception { assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); final String msg1 = "hello1"; final String msg2 = "hello2"; handlerRule.feedMessagesForRead(msg1, msg2); handlerRule.setMaxMessagesPerRead(2); handlerRule.requestMessages(2); assertThat("Unexpected read requested count.", handlerRule.getReadRequestedCount(), is(1)); handlerRule.assertMessagesReceived(msg1, msg2); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); }
@Test(timeout = 60000) public void testExactDemandAndSupplyMultiRequests() throws Exception { assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); final String msg1 = "hello1"; final String msg2 = "hello2"; handlerRule.feedMessagesForRead(msg1, msg2); handlerRule.setMaxMessagesPerRead(2); handlerRule.requestMessages(2); assertThat("Unexpected read requested count.", handlerRule.getReadRequestedCount(), is(1)); handlerRule.assertMessagesReceived(msg1, msg2); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); handlerRule.resetReadCount(); assertThat("Unexpected read requested count post reset.", handlerRule.getReadRequestedCount(), is(0)); handlerRule.handler.reset(); final String msg3 = "hello3"; handlerRule.feedMessagesForRead(msg3); assertThat("Unexpected read requested count post reset.", handlerRule.getReadRequestedCount(), is(0)); handlerRule.assertMessagesReceived(); handlerRule.requestMessages(1); assertThat("Unexpected read requested count.", handlerRule.getReadRequestedCount(), is(1)); handlerRule.assertMessagesReceived(msg3); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Reading)); }
@Test(timeout = 60000) public void testMoreDemand() throws Exception { assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); final String msg1 = "hello1"; final String msg2 = "hello2"; handlerRule.feedMessagesForRead(msg1, msg2); handlerRule.setMaxMessagesPerRead(2); handlerRule.requestMessages(4); assertThat("Unexpected read requested count.", handlerRule.getReadRequestedCount(), is(2)); handlerRule.assertMessagesReceived(msg1, msg2); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.ReadRequested)); }