public synchronized void interruptLeadership() {
        if (hasLeadership) {
            cancelElection();
        }
    }
@Test
    public void testInterruptLeadership() throws Exception {
        LeaderSelector selector = null;
        Timing timing = new Timing();
        CuratorFramework client = CuratorFrameworkFactory.newClient(
                server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        try {
            client.start();

            final CountDownLatch isLeaderLatch = new CountDownLatch(1);
            final CountDownLatch losingLeaderLatch = new CountDownLatch(1);
            LeaderSelectorListener listener = new LeaderSelectorListener() {
                @Override
                public void takeLeadership(CuratorFramework client) throws Exception {
                    isLeaderLatch.countDown();
                    try {
                        Thread.currentThread().join();
                    } finally {
                        losingLeaderLatch.countDown();
                    }
                }

                @Override
                public void stateChanged(CuratorFramework client, ConnectionState newState) {}
            };

            selector = new LeaderSelector(client, "/leader", listener);
            selector.start();

            assertTrue(timing.awaitLatch(isLeaderLatch));
            selector.interruptLeadership();
            assertTrue(timing.awaitLatch(losingLeaderLatch));
        } finally {
            CloseableUtils.closeQuietly(selector);
            CloseableUtils.closeQuietly(client);
        }
    }