@Test public void shouldResumePullingUpdatesWhenThisInstanceSwitchesFromMasterToSlave() throws Throwable { // GIVEN final UpdatePullerClient puller = new UpdatePullerClient(1, scheduler, logging, updatePuller, availabilityGuard); // WHEN puller.init(); puller.start(); updatePuller.unpause(); scheduler.runJob(); // THEN verify(lastUpdateTime, times(1)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(1)).isAvailable(anyLong()); verify(master, times(1)).pullUpdates(Matchers.<RequestContext>any()); stateMachine.masterIsElected(); // pauses the update puller // This job should be ignored, since I'm now master scheduler.runJob(); updatePuller.unpause(); scheduler.runJob(); verify(lastUpdateTime, times(2)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(2)).isAvailable(anyLong()); verify(master, times(2)).pullUpdates(Matchers.<RequestContext>any()); }
@Test public void shouldResumePullingUpdatesWhenThisInstanceSwitchesFromSlaveToMaster() throws Throwable { final UpdatePullerClient puller = new UpdatePullerClient(1, scheduler, logging, updatePuller, availabilityGuard); puller.init(); puller.start(); updatePuller.unpause(); scheduler.runJob(); verify(lastUpdateTime, times(1)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(1)).isAvailable(anyLong()); verify(master, times(1)).pullUpdates(Matchers.<RequestContext>any()); scheduler.runJob(); verify(lastUpdateTime, times(2)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(2)).isAvailable(anyLong()); verify(master, times(2)).pullUpdates(Matchers.<RequestContext>any()); stateMachine.masterIsElected(); // pauses the update puller verifyNoMoreInteractions(lastUpdateTime, availabilityGuard); }
@Test public void shouldStartAndStopPullingUpdatesWhenStartAndStopIsCalled() throws Throwable { // GIVEN final UpdatePullerClient puller = new UpdatePullerClient(1, scheduler, logging, updatePuller, availabilityGuard); // WHEN puller.init(); // THEN // Asserts the puller set the job assertNotNull(scheduler.getJob()); puller.start(); updatePuller.unpause(); scheduler.runJob(); verify(lastUpdateTime, times(1)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(1)).isAvailable(anyLong()); verify(master, times(1)).pullUpdates(Matchers.<RequestContext>any()); updatePuller.stop(); scheduler.runJob(); verifyNoMoreInteractions(lastUpdateTime, availabilityGuard); }
@Test public void shouldReturnFalseIfPullerBecomesInactiveWhileWaitingNonStrict() throws Exception { // GIVEN Condition condition = mock(Condition.class); updatePuller.unpause(); when(condition.evaluate(anyInt(), anyInt())) .thenAnswer( new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { updatePuller.pause(); return false; } }); // WHEN boolean result = updatePuller.await(condition, false); // THEN assertFalse(result); verify(condition, times(1)).evaluate(anyInt(), anyInt()); }
@Test public void shouldThrowIfPullerBecomesInactiveWhileWaitingStrict() throws Exception { // GIVEN Condition condition = mock(Condition.class); updatePuller.unpause(); when(condition.evaluate(anyInt(), anyInt())) .thenAnswer( new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { updatePuller.pause(); return false; } }); // WHEN try { updatePuller.await(condition, true); fail("Should have thrown"); } catch (IllegalStateException e) { // THEN Good verify(condition, times(1)).evaluate(anyInt(), anyInt()); } }
@Test public void shouldKeepPullingUpdatesWhenThisInstanceBecomesASlave() throws Throwable { // GIVEN final UpdatePullerClient puller = new UpdatePullerClient(1, scheduler, logging, updatePuller, availabilityGuard); // WHEN puller.init(); puller.start(); updatePuller.unpause(); scheduler.runJob(); // THEN verify(lastUpdateTime, times(1)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(1)).isAvailable(anyLong()); verify(master, times(1)).pullUpdates(Matchers.<RequestContext>any()); scheduler.runJob(); verify(lastUpdateTime, times(2)).setLastUpdateTime(anyLong()); verify(availabilityGuard, times(2)).isAvailable(anyLong()); verify(master, times(2)).pullUpdates(Matchers.<RequestContext>any()); }