Search in sources :

Example 1 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateHistoryTest method testRemoveMultipleElementsFromHistory.

@Test
public void testRemoveMultipleElementsFromHistory() throws Exception {
    SourceState firstState = mock(SourceState.class);
    SourceState secondState = mock(SourceState.class);
    SourceState thirdState = mock(SourceState.class);
    TestRepository repository = new TestRepository(firstState, secondState, thirdState);
    StateHistory history = new StateHistory(SOURCE_NAME, 3, repository, metrics);
    assertEquals(secondState, history.removeLast(2));
    assertEquals(Collections.singletonList(firstState), repository.get());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 2 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateHistoryTest method testRemoveAllElementsFromHistory.

@Test
public void testRemoveAllElementsFromHistory() throws Exception {
    SourceState firstState = mock(SourceState.class);
    SourceState secondState = mock(SourceState.class);
    TestRepository repository = new TestRepository(firstState, secondState);
    StateHistory history = new StateHistory(SOURCE_NAME, 2, repository, metrics);
    assertEquals(firstState, history.removeLast(2));
    assertTrue(history.isEmpty());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 3 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState 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)

Example 4 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class JsonSerializationTest method testSerializeSourceState.

@Test
public void testSerializeSourceState() throws Exception {
    SourceState state = JsonUtil.OBJECT_MAPPER.readValue(JsonUtil.OBJECT_MAPPER.writeValueAsString(SOURCE_STATE), new TypeReference<SourceState>() {
    });
    assertEquals(BINLOG_FILE_POS, state.getLastPosition());
    assertEquals(SOURCE_STATE.getLastTimestamp(), state.getLastTimestamp());
    assertEquals(SOURCE_STATE.getLastOffset(), state.getLastOffset());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 5 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class JsonSerializationTest method testSerializeStateHistory.

@Test
public void testSerializeStateHistory() throws Exception {
    SourceState firstState = new SourceState(15l, 20l, -1l, BINLOG_FILE_POS);
    SourceState secondState = new SourceState(16l, 21l, -1l, BINLOG_FILE_POS);
    SourceState thirdState = new SourceState(17l, 22l, -1l, BINLOG_FILE_POS);
    Deque<SourceState> stateHistory = Queues.newArrayDeque();
    stateHistory.addLast(firstState);
    stateHistory.addLast(secondState);
    stateHistory.addLast(thirdState);
    Collection<SourceState> states = JsonUtil.OBJECT_MAPPER.readValue(JsonUtil.OBJECT_MAPPER.writeValueAsString(stateHistory), new TypeReference<Collection<SourceState>>() {
    });
    stateHistory = Queues.newArrayDeque(states);
    assertEquals(3, states.size());
    assertEquals(thirdState, stateHistory.removeLast());
    assertEquals(secondState, stateHistory.removeLast());
    assertEquals(firstState, stateHistory.removeLast());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

SourceState (com.airbnb.spinaltap.common.source.SourceState)21 Test (org.junit.Test)16 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)3 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)2 MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Repository (com.airbnb.spinaltap.common.util.Repository)1 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 TableCache (com.airbnb.spinaltap.mysql.TableCache)1 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)1 DeleteEvent (com.airbnb.spinaltap.mysql.event.DeleteEvent)1 QueryEvent (com.airbnb.spinaltap.mysql.event.QueryEvent)1 StartEvent (com.airbnb.spinaltap.mysql.event.StartEvent)1 TableMapEvent (com.airbnb.spinaltap.mysql.event.TableMapEvent)1 UpdateEvent (com.airbnb.spinaltap.mysql.event.UpdateEvent)1 WriteEvent (com.airbnb.spinaltap.mysql.event.WriteEvent)1 XidEvent (com.airbnb.spinaltap.mysql.event.XidEvent)1 Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)1 Collection (java.util.Collection)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1