@Override public Optional<LeadershipToken> getCurrentTokenIfLeading() { return delegate.getCurrentTokenIfLeading().map(leaseTokens::getIfPresent); }
private void tryToGainLeadership() { Optional<LeadershipToken> currentToken = leaderElectionService.getCurrentTokenIfLeading(); if (currentToken.isPresent()) { onGainedLeadership(currentToken.get()); } else { tryToGainLeadershipAsync(); } }
@Test public void shouldGainLeadershipImmediatelyIfAlreadyLeading() throws Exception { when(leaderElectionService.getCurrentTokenIfLeading()).thenReturn(Optional.of(leadershipToken)); Callable proxy = proxyFor(() -> null); proxy.call(); verify(leaderElectionService, never()).blockOnBecomingLeader(); }
@Before public void before() throws InterruptedException { when(leaderElectionService.blockOnBecomingLeader()).thenReturn(leadershipToken); when(leaderElectionService.getCurrentTokenIfLeading()).thenReturn(Optional.empty()); when(leaderElectionService.getSuspectedLeaderInMemory()).thenReturn(Optional.empty()); when(leaderElectionService.isStillLeading(leadershipToken)).thenReturn( LeaderElectionService.StillLeadingStatus.LEADING); }
@Test @SuppressWarnings("SelfEquals") // We're asserting that calling .equals on a proxy does not redirect // the .equals call to the instance its being proxied. public void shouldAllowObjectMethodsWhenLeading() { when(mockLeader.getSuspectedLeaderInMemory()).thenReturn(Optional.empty()); when(mockLeader.getCurrentTokenIfLeading()).thenReturn(Optional.empty()); when(mockLeader.isStillLeading(any(LeaderElectionService.LeadershipToken.class))) .thenReturn(LeaderElectionService.StillLeadingStatus.LEADING); Runnable proxy = AwaitingLeadershipProxy.newProxyInstance(Runnable.class, delegateSupplier, mockLeader); assertThat(proxy.hashCode()).isNotNull(); assertThat(proxy.equals(proxy)).isTrue(); assertThat(proxy.equals(null)).isFalse(); assertThat(proxy.toString()).startsWith("com.palantir.leader.proxy.AwaitingLeadershipProxy@"); }
@Test @SuppressWarnings("SelfEquals") // We're asserting that calling .equals on a proxy does not redirect // the .equals call to the instance its being proxied. public void shouldAllowObjectMethodsWhenNotLeading() { when(mockLeader.getSuspectedLeaderInMemory()).thenReturn(Optional.empty()); when(mockLeader.getCurrentTokenIfLeading()).thenReturn(Optional.empty()); when(mockLeader.isStillLeading(any(LeaderElectionService.LeadershipToken.class))) .thenReturn(LeaderElectionService.StillLeadingStatus.NOT_LEADING); Runnable proxy = AwaitingLeadershipProxy.newProxyInstance(Runnable.class, delegateSupplier, mockLeader); assertThat(proxy.hashCode()).isNotNull(); assertThat(proxy.equals(proxy)).isTrue(); assertThat(proxy.equals(null)).isFalse(); assertThat(proxy.toString()).startsWith("com.palantir.leader.proxy.AwaitingLeadershipProxy@"); }
@Test public void testIsNotLeadingIfWaiting() { Thread thread = new Thread(() -> { try { leased.blockOnBecomingLeader(); } catch (InterruptedException e) { throw new RuntimeException(e); } }); thread.start(); while (thread.getState() != Thread.State.TIMED_WAITING) { Thread.yield(); } assertThat(leased.getCurrentTokenIfLeading()).isEmpty(); }
@Before public void before() throws InterruptedException { // cache forever leased = new LeasingLeaderElectionService(() -> true, delegate, Duration.ofDays(1), TIMEOUT); when(delegate.blockOnBecomingLeader()).thenReturn(token); when(delegate.getCurrentTokenIfLeading()).thenReturn(Optional.of(token)); }
@Test public void shouldBlockOnGainingLeadershipIfNotCurrentlyLeading() throws Exception { Callable proxy = proxyFor(() -> null); waitForLeadershipToBeGained(); proxy.call(); verify(leaderElectionService).getCurrentTokenIfLeading(); verify(leaderElectionService).blockOnBecomingLeader(); }
@Override public Optional<LeadershipToken> getCurrentTokenIfLeading() { return delegate.getCurrentTokenIfLeading().map(leaseTokens::getIfPresent); }
private void tryToGainLeadership() { Optional<LeadershipToken> currentToken = leaderElectionService.getCurrentTokenIfLeading(); if (currentToken.isPresent()) { onGainedLeadership(currentToken.get()); } else { tryToGainLeadershipAsync(); } }