@Test
public void testGatewayRetrievalFailures() throws Exception {

	final String address = "localhost";
	final UUID leaderId = UUID.randomUUID();
	RpcGateway rpcGateway = mock(RpcGateway.class);
	TestingLeaderGatewayRetriever leaderGatewayRetriever = new TestingLeaderGatewayRetriever(rpcGateway);
	TestingLeaderRetrievalService testingLeaderRetrievalService = new TestingLeaderRetrievalService();
	testingLeaderRetrievalService.start(leaderGatewayRetriever);
	CompletableFuture<RpcGateway> gatewayFuture = leaderGatewayRetriever.getFuture();
	// this triggers the first gateway retrieval attempt
	testingLeaderRetrievalService.notifyListener(address, leaderId);
	// check that the first future has been failed
	try {
		gatewayFuture.get();
		fail("The first future should have been failed.");
	} catch (ExecutionException ignored) {
		// that's what we expect
	}
	// the second attempt should fail as well
	assertFalse((leaderGatewayRetriever.getNow().isPresent()));
	// the third attempt should succeed
	assertEquals(rpcGateway, leaderGatewayRetriever.getNow().get());
}