Search in sources :

Example 26 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class HeartbeatStateTest method shouldAddInstanceIdHeaderInCatchUpMessages.

@Test
public void shouldAddInstanceIdHeaderInCatchUpMessages() throws Throwable {
    // Given
    InstanceId instanceId = new InstanceId(1);
    HeartbeatState heartbeat = HeartbeatState.heartbeat;
    ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
    configuration.joined(instanceId, URI.create("cluster://1"));
    InstanceId otherInstance = new InstanceId(2);
    configuration.joined(otherInstance, URI.create("cluster://2"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, Mockito.mock(Executor.class), NullLogProvider.getInstance(), Mockito.mock(ObjectInputStreamFactory.class), Mockito.mock(ObjectOutputStreamFactory.class), Mockito.mock(AcceptorInstanceStore.class), Mockito.mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    int lastDeliveredInstanceId = 100;
    context.getLearnerContext().setLastDeliveredInstanceId(lastDeliveredInstanceId);
    // This gap will trigger the catchUp message that we'll test against
    lastDeliveredInstanceId += 20;
    HeartbeatContext heartbeatContext = context.getHeartbeatContext();
    Message received = Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance));
    received.setHeader(Message.FROM, "cluster://2").setHeader(Message.INSTANCE_ID, "2").setHeader("last-learned", Integer.toString(lastDeliveredInstanceId));
    // When
    MessageHolder holder = mock(MessageHolder.class);
    heartbeat.handle(heartbeatContext, received, holder);
    // Then
    verify(holder, times(1)).offer(Matchers.argThat(new MessageArgumentMatcher<LearnerMessage>().onMessageType(LearnerMessage.catchUp).withHeader(Message.INSTANCE_ID, "2")));
}
Also used : LearnerMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) AcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore) Test(org.junit.Test)

Example 27 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class AtomicBroadcastStateTest method shouldBroadcastWhenHavingQuorumAndCoordinator.

@Test
public void shouldBroadcastWhenHavingQuorumAndCoordinator() throws Throwable {
    // GIVEN
    AtomicBroadcastContext context = mock(AtomicBroadcastContext.class);
    when(context.hasQuorum()).thenReturn(true);
    InstanceId coordinator = id(1);
    when(context.getCoordinator()).thenReturn(coordinator);
    when(context.getUriForId(coordinator)).thenReturn(uri(1));
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder outgoing = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    // WHEN
    broadcasting.handle(context, message(1), outgoing);
    // THEN
    assertEquals(1, messages.size());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 28 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class AtomicBroadcastStateTest method shouldBroadcastWhenHavingQuorumNoCoordinator.

@Test
public void shouldBroadcastWhenHavingQuorumNoCoordinator() throws Throwable {
    // GIVEN
    AtomicBroadcastContext context = mock(AtomicBroadcastContext.class);
    when(context.hasQuorum()).thenReturn(true);
    when(context.getCoordinator()).thenReturn(null);
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder outgoing = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    // WHEN
    broadcasting.handle(context, message(1), outgoing);
    // THEN
    assertEquals(1, messages.size());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Message(org.neo4j.cluster.com.message.Message) ArrayList(java.util.ArrayList) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 29 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class AtomicBroadcastStateTest method shouldNotBroadcastWhenHavingNoQuorumButCoordinator.

@Test
public void shouldNotBroadcastWhenHavingNoQuorumButCoordinator() throws Throwable {
    // GIVEN
    AtomicBroadcastContext context = mock(AtomicBroadcastContext.class);
    when(context.hasQuorum()).thenReturn(false);
    when(context.getCoordinator()).thenReturn(null);
    when(context.getLog(AtomicBroadcastState.class)).thenReturn(NullLog.getInstance());
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder outgoing = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    // WHEN
    broadcasting.handle(context, message(1), outgoing);
    // THEN
    assertEquals(0, messages.size());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Message(org.neo4j.cluster.com.message.Message) ArrayList(java.util.ArrayList) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Aggregations

MessageHolder (org.neo4j.cluster.com.message.MessageHolder)29 Test (org.junit.Test)28 Message (org.neo4j.cluster.com.message.Message)19 InstanceId (org.neo4j.cluster.InstanceId)14 URI (java.net.URI)11 MessageType (org.neo4j.cluster.com.message.MessageType)11 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)10 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)8 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)5 MessageArgumentMatcher (org.neo4j.cluster.protocol.MessageArgumentMatcher)5 AtomicBroadcastMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage)5 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)5 TrackingMessageHolder (org.neo4j.cluster.com.message.TrackingMessageHolder)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 HashMap (java.util.HashMap)2 Executor (java.util.concurrent.Executor)2 ConfigurationRequestState (org.neo4j.cluster.protocol.cluster.ClusterMessage.ConfigurationRequestState)2