### Question:
LeaderElection implements Cancellable { public ListenableFuture<?> asyncCancel() { final SettableFuture<String> completion = SettableFuture.create(); if (!cancelFuture.compareAndSet(null, completion)) { return cancelFuture.get(); } executor.execute(new Runnable() { @Override public void run() { if (state != State.CANCELLED) { if (state == State.LEADER) { becomeFollower(); } state = State.CANCELLED; doDeleteNode(completion); } } }); return completion; } LeaderElection(ZKClient zkClient, String prefix, ElectionHandler handler); @Override void cancel(); ListenableFuture<?> asyncCancel();  }### Answer:
@Test (timeout = 10000) public void testDisconnect() throws IOException, InterruptedException { File zkDataDir = tmpFolder.newFolder(); InMemoryZKServer ownZKServer = InMemoryZKServer.builder().setDataDir(zkDataDir).build(); ownZKServer.startAndWait(); try { ZKClientService zkClient = ZKClientService.Builder.of(ownZKServer.getConnectionStr()).build(); zkClient.startAndWait(); try { final Semaphore leaderSem = new Semaphore(0); final Semaphore followerSem = new Semaphore(0); LeaderElection leaderElection = new LeaderElection(zkClient, "/testDisconnect", new ElectionHandler() { @Override public void leader() { leaderSem.release(); } @Override public void follower() { followerSem.release(); } }); leaderSem.acquire(); int zkPort = ownZKServer.getLocalAddress().getPort(); ownZKServer.stopAndWait(); followerSem.acquire(); ownZKServer = InMemoryZKServer.builder().setDataDir(zkDataDir).setPort(zkPort).build(); ownZKServer.startAndWait(); leaderSem.acquire(); ownZKServer.stopAndWait(); followerSem.acquire(); ListenableFuture<?> cancelFuture = leaderElection.asyncCancel(); ownZKServer = InMemoryZKServer.builder().setDataDir(zkDataDir).setPort(zkPort).build(); ownZKServer.startAndWait(); Futures.getUnchecked(cancelFuture); Assert.assertFalse(leaderSem.tryAcquire(2, TimeUnit.SECONDS)); } finally { zkClient.stopAndWait(); } } finally { ownZKServer.stopAndWait(); } }