@Test
    public void testNoRaceConditionForPendingReplyTasks() {
        MockComponents mc = createDefaultMock();
        mc.visitorSession.start();
        mc.controlHandler.resetMock(); // clear messages
        mc.executor.expectAndProcessTasks(1);
        assertEquals(2, mc.sender.getMessageCount());
        replyToCreateVisitor(mc.sender, (reply) -> {
            reply.addError(new Error(
                    DocumentProtocol.ERROR_INTERNAL_FAILURE,
                    "node fell down a well"));
        });
        replyToCreateVisitor(mc.sender, (reply) -> {
            reply.addError(new Error(
                    DocumentProtocol.ERROR_INTERNAL_FAILURE,
                    "node got hit by a falling brick"));
        });

        // Now 2 pending reply tasks, but 0 pending messages. Ergo, using
        // the sender as a ground truth to determine whether or not we have
        // completed will cause a race condition.
        mc.executor.expectAndProcessTasks(2);
        mc.executor.expectNoTasks();
        assertEquals(0, mc.sender.getMessageCount()); // no resending
        assertTrue(mc.visitorSession.isDone());

        // should get first received failure message as completion failure message
        assertEquals("onVisitorError : INTERNAL_FAILURE: node fell down a well\n" +
                "onVisitorError : INTERNAL_FAILURE: node got hit by a falling brick\n" +
                "onDone : FAILURE - 'INTERNAL_FAILURE: node fell down a well'\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