@Test
    public void timeout_with_pending_outbound_messages_does_not_close_session_until_all_replies_received() {
        MockComponents mc = createTimeoutMocksAtInitialTime(1_000, 5_000, 20_000, 2);

        assertEquals(2, mc.sender.getMessageCount());

        replyToCreateVisitor(mc.sender, ProgressToken.FINISHED_BUCKET); // Super bucket 1 of 2
        mc.clock.setMonotonicTime(25_000, TimeUnit.MILLISECONDS);

        mc.executor.expectAndProcessTasks(1);
        mc.executor.expectNoTasks(); // No further send tasks enqueued
        assertFalse(mc.controlHandler.isDone()); // Still pending messages, session _not_ yet done.

        replyToCreateVisitor(mc.sender, ProgressToken.FINISHED_BUCKET); // Super bucket 2 of 2
        mc.controlHandler.resetMock();
        mc.executor.expectAndProcessTasks(1);
        mc.executor.expectNoTasks(); // No further send tasks enqueued
        assertTrue(mc.controlHandler.isDone()); // Now it's done.

        assertEquals("onProgress : 0 active, 0 pending, 2 finished, 2 total\n" +
                        "onVisitorStatistics : 0 buckets visited, 0 docs returned\n" +
                        "onDone : TIMEOUT - 'Session timeout of 5000 ms expired'\n",
                mc.controlHandler.toString());
    }
Test that there is no race condition between a reply is handed off
to the executor service via a task (thus decrementing the pending count
for the sender) and the session checking for completion early, e.g.
because of an error transitioning it into a failure state.
documentapi/src/test/java/com/yahoo/documentapi/messagebus/MessageBusVisitorSessionTestCase.java