public static DispatcherRunner create(
            LeaderElection leaderElection,
            FatalErrorHandler fatalErrorHandler,
            DispatcherLeaderProcessFactory dispatcherLeaderProcessFactory)
            throws Exception {
        final DefaultDispatcherRunner dispatcherRunner =
                new DefaultDispatcherRunner(
                        leaderElection, fatalErrorHandler, dispatcherLeaderProcessFactory);
        dispatcherRunner.start();
        return dispatcherRunner;
    }
@Test
    public void grantLeadership_withExistingLeader_waitsForTerminationOfFirstLeader()
            throws Exception {
        final UUID firstLeaderSessionId = UUID.randomUUID();
        final UUID secondLeaderSessionId = UUID.randomUUID();

        final StartStopDispatcherLeaderProcess firstTestingDispatcherLeaderProcess =
                StartStopDispatcherLeaderProcess.create(firstLeaderSessionId);
        final StartStopDispatcherLeaderProcess secondTestingDispatcherLeaderProcess =
                StartStopDispatcherLeaderProcess.create(secondLeaderSessionId);

        testingDispatcherLeaderProcessFactory =
                TestingDispatcherLeaderProcessFactory.from(
                        firstTestingDispatcherLeaderProcess.asTestingDispatcherLeaderProcess(),
                        secondTestingDispatcherLeaderProcess.asTestingDispatcherLeaderProcess());

        try (final DispatcherRunner dispatcherRunner = createDispatcherRunner()) {
            leaderElection.isLeader(firstLeaderSessionId);

            assertThat(firstTestingDispatcherLeaderProcess.isStarted(), is(true));

            leaderElection.isLeader(secondLeaderSessionId);

            assertThat(secondTestingDispatcherLeaderProcess.isStarted(), is(false));
            firstTestingDispatcherLeaderProcess.terminateProcess();
            assertThat(secondTestingDispatcherLeaderProcess.isStarted(), is(true));
            secondTestingDispatcherLeaderProcess
                    .terminateProcess(); // make the dispatcherRunner terminate
        }
    }