@Test void runShouldAbortAfterRenewDeadlineExpired() throws Exception { final Long renewDeadlineMillis = 1000L; final ExecutorService executor = Executors.newSingleThreadExecutor(); final CountDownLatch signal = new CountDownLatch(1); final LeaderElectionConfig lec = mockLeaderElectionConfiguration(); when(lec.getRenewDeadline()).thenReturn(Duration.ofMillis(renewDeadlineMillis)); final Lock mockedLock = lec.getLock(); doNothing().doAnswer(invocation -> { Thread.sleep(renewDeadlineMillis*2); return null; }).when(mockedLock).update(any(), any()); executor.submit(() -> { new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).run(); signal.countDown(); }); signal.await(10, TimeUnit.SECONDS); assertEquals(0, signal.getCount()); verify(mockedLock, atLeast(2)).get(any()); verify(mockedLock, times(1)).create(any(), any()); verify(mockedLock, atLeast(2)).update(any(), any()); verify(lec.getLeaderCallbacks(), atLeast(1)).onNewLeader(eq("1337")); verify(lec.getLeaderCallbacks(), times(1)).onStartLeading(); verify(lec.getLeaderCallbacks(), times(1)).onStopLeading(); executor.shutdownNow(); }
public void run() { LOGGER.debug("Leader election started"); if (!acquire()) { return; } leaderElectionConfig.getLeaderCallbacks().onStartLeading(); renewWithTimeout(); leaderElectionConfig.getLeaderCallbacks().onStopLeading(); }
LeaderElector { public void run() { LOGGER.debug("Leader election started"); if (!acquire()) { return; } leaderElectionConfig.getLeaderCallbacks().onStartLeading(); renewWithTimeout(); leaderElectionConfig.getLeaderCallbacks().onStopLeading(); } }
LeaderElector { public void run() { LOGGER.debug("Leader election started"); if (!acquire()) { return; } leaderElectionConfig.getLeaderCallbacks().onStartLeading(); renewWithTimeout(); leaderElectionConfig.getLeaderCallbacks().onStopLeading(); } LeaderElector(C kubernetesClient, LeaderElectionConfig leaderElectionConfig); }
LeaderElector { public void run() { LOGGER.debug("Leader election started"); if (!acquire()) { return; } leaderElectionConfig.getLeaderCallbacks().onStartLeading(); renewWithTimeout(); leaderElectionConfig.getLeaderCallbacks().onStopLeading(); } LeaderElector(C kubernetesClient, LeaderElectionConfig leaderElectionConfig); void run(); }
LeaderElector { public void run() { LOGGER.debug("Leader election started"); if (!acquire()) { return; } leaderElectionConfig.getLeaderCallbacks().onStartLeading(); renewWithTimeout(); leaderElectionConfig.getLeaderCallbacks().onStopLeading(); } LeaderElector(C kubernetesClient, LeaderElectionConfig leaderElectionConfig); void run();  }