### Question:
MessageGate extends ChannelDuplexHandler { @Override public void write( ChannelHandlerContext ctx, Object msg, ChannelPromise promise ) { if ( !gated.test( msg ) ) { ctx.write( msg, promise ); } else if ( pending != null ) { pending.add( new GatedWrite( msg, promise ) ); } else { promise.setFailure( new RuntimeException( "Gate failed and has been permanently closed." ) ); } } MessageGate( Predicate<Object> gated ); @Override void userEventTriggered( ChannelHandlerContext ctx, Object evt ); @Override void write( ChannelHandlerContext ctx, Object msg, ChannelPromise promise );  }### Answer:
@Test public void shouldLetAllowedMessagesPass() throws Exception { ChannelPromise promise = mock( ChannelPromise.class ); gate.write( ctx, ALLOWED_MSG, promise ); gate.write( ctx, ALLOWED_MSG, promise ); gate.write( ctx, ALLOWED_MSG, promise ); verify( ctx, times( 3 ) ).write( ALLOWED_MSG, promise ); }
@Test public void shouldGateMessages() throws Exception { ChannelPromise promise = mock( ChannelPromise.class ); gate.write( ctx, "A", promise ); gate.write( ctx, "B", promise ); gate.write( ctx, "C", promise ); verify( ctx, never() ).write( any(), any() ); }