@Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        if (readInProgress) {
            // If there is still a read in progress we are sure we will see a channelReadComplete(...) call. Thus
            // we only need to flush if we reach the explicitFlushAfterFlushes limit.
            if (++flushPendingCount == explicitFlushAfterFlushes) {
                flushNow(ctx);
            }
        } else if (consolidateWhenNoReadInProgress) {
            // Flush immediately if we reach the threshold, otherwise schedule
            if (++flushPendingCount == explicitFlushAfterFlushes) {
                flushNow(ctx);
            } else {
                scheduleFlush(ctx);
            }
        } else {
            // Always flush directly
            flushNow(ctx);
        }
    }
@Test
    public void testImmediateFlushOutsideOfReadLoop() {
        final AtomicInteger flushCount = new AtomicInteger();
        EmbeddedChannel channel = newChannel(flushCount, false);
        channel.flush();
        assertEquals(1, flushCount.get());
        assertFalse(channel.finish());
    }