use of com.airbnb.spinaltap.common.util.Repository in project SpinalTap by airbnb.
the class StateRepositoryTest method testSave.
@Test
public void testSave() throws Exception {
SourceState state = mock(SourceState.class);
SourceState nextState = mock(SourceState.class);
AtomicReference<SourceState> updatedState = new AtomicReference<>();
when(state.getCurrentLeaderEpoch()).thenReturn(5l);
doAnswer(new Answer<SourceState>() {
@Override
public SourceState answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
SourceState newState = (SourceState) args[0];
Repository.DataUpdater<SourceState> updater = (Repository.DataUpdater<SourceState>) args[1];
updatedState.set(updater.apply(state, newState));
return null;
}
}).when(repository).update(any(SourceState.class), any(Repository.DataUpdater.class));
// Test new leader epoch leader less than current
when(nextState.getCurrentLeaderEpoch()).thenReturn(4l);
stateRepository.save(nextState);
assertEquals(state, updatedState.get());
// Test new leader epoch leader same as current
when(nextState.getCurrentLeaderEpoch()).thenReturn(5l);
stateRepository.save(nextState);
assertEquals(nextState, updatedState.get());
// Test new leader epoch leader greather current
when(nextState.getCurrentLeaderEpoch()).thenReturn(6l);
stateRepository.save(nextState);
assertEquals(nextState, updatedState.get());
}
Aggregations