Search in sources :

Example 1 with Repository

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());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Repository(com.airbnb.spinaltap.common.util.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

SourceState (com.airbnb.spinaltap.common.source.SourceState)1 Repository (com.airbnb.spinaltap.common.util.Repository)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1