Source:
public void testMultipleKubernetesLeaderElectors() throws Exception { final Configuration configuration = kubernetesResource.getConfiguration(); final ExecutorService executorService = kubernetesResource.getExecutorService(); final BlockingQueue<String> leaderStore = new LinkedBlockingQueue<>(); final String leaderConfigMapName = LEADER_CONFIGMAP_NAME_PREFIX + System.currentTimeMillis(); final int leaderNum = 3; final KubernetesLeaderElector[] leaderElectors = new KubernetesLeaderElector[leaderNum]; final FlinkKubeClient[] kubeClients = new FlinkKubeClient[leaderNum]; final TestingLeaderCallbackHandler[] leaderCallbackHandlers = new TestingLeaderCallbackHandler[leaderNum]; try { for (int i = 0; i < leaderNum; i++) { kubeClients[i] = kubeClientFactory.fromConfiguration(configuration, executorService); leaderCallbackHandlers[i] = new TestingLeaderCallbackHandler(leaderStore, UUID.randomUUID().toString()); final KubernetesLeaderElectionConfiguration leaderConfig = new KubernetesLeaderElectionConfiguration( leaderConfigMapName, leaderCallbackHandlers[i].getLockIdentity(), configuration); leaderElectors[i] = kubeClients[i].createLeaderElector(leaderConfig, leaderCallbackHandlers[i]); leaderElectors[i].run(); } final String firstLockIdentity = leaderStore.poll(TIMEOUT, TimeUnit.MILLISECONDS); for (int i = 0; i < leaderNum; i++) { if (leaderCallbackHandlers[i].getLockIdentity().equals(firstLockIdentity)) { assertThat(leaderCallbackHandlers[i].hasLeadership(), is(true)); leaderElectors[i].stop(); } else { assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false)); } } final String anotherLockIdentity = leaderStore.poll(TIMEOUT, TimeUnit.MILLISECONDS); assertThat(anotherLockIdentity, is(not(firstLockIdentity))); } finally { for (int i = 0; i < leaderNum; i++) { if (leaderElectors[i] != null) { leaderElectors[i].stop(); } if (kubeClients[i] != null) { kubeClients[i].close(); } } kubernetesResource.getFlinkKubeClient().deleteConfigMap(leaderConfigMapName).get(); } }

Target:
public void testMultipleKubernetesLeaderElectors() throws Exception { final Configuration configuration = kubernetesResource.getConfiguration(); final String leaderConfigMapName = LEADER_CONFIGMAP_NAME_PREFIX + System.currentTimeMillis(); final int leaderNum = 3; final KubernetesLeaderElector[] leaderElectors = new KubernetesLeaderElector[leaderNum]; final FlinkKubeClient[] kubeClients = new FlinkKubeClient[leaderNum]; final TestingLeaderCallbackHandler[] leaderCallbackHandlers = new TestingLeaderCallbackHandler[leaderNum]; try { for (int i = 0; i < leaderNum; i++) { kubeClients[i] = kubeClientFactory.fromConfiguration(configuration); leaderCallbackHandlers[i] = new TestingLeaderCallbackHandler(UUID.randomUUID().toString()); final KubernetesLeaderElectionConfiguration leaderConfig = new KubernetesLeaderElectionConfiguration( leaderConfigMapName, leaderCallbackHandlers[i].getLockIdentity(), configuration); leaderElectors[i] = kubeClients[i].createLeaderElector(leaderConfig, leaderCallbackHandlers[i]); leaderElectors[i].run(); } final String firstLockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT); for (int i = 0; i < leaderNum; i++) { if (leaderCallbackHandlers[i].getLockIdentity().equals(firstLockIdentity)) { leaderCallbackHandlers[i].waitForNewLeader(TIMEOUT); assertThat(leaderCallbackHandlers[i].hasLeadership(), is(true)); leaderElectors[i].stop(); leaderCallbackHandlers[i].waitForRevokeLeader(TIMEOUT); assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false)); } else { assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false)); } } final String anotherLockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT); assertThat(anotherLockIdentity, is(not(firstLockIdentity))); } finally { for (int i = 0; i < leaderNum; i++) { if (leaderElectors[i] != null) { leaderElectors[i].stop(); } if (kubeClients[i] != null) { kubeClients[i].close(); } } kubernetesResource.getFlinkKubeClient().deleteConfigMap(leaderConfigMapName).get(); } }