Search in sources :

Example 6 with MessageType

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

the class ElectionStateTest method voteResponseShouldHaveSameVersionAsVoteRequest.

@Test
public void voteResponseShouldHaveSameVersionAsVoteRequest() throws Throwable {
    final List<Message> messages = new ArrayList<Message>(1);
    MessageHolder holder = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    ElectionContext context = mock(ElectionContext.class);
    final int version = 14;
    Message voteRequest = Message.to(ElectionMessage.vote, URI.create("some://instance"), new ElectionContext.VoteRequest("coordinator", version));
    voteRequest.setHeader(Message.FROM, "some://other");
    election.handle(context, voteRequest, holder);
    assertEquals(1, messages.size());
    Message response = messages.get(0);
    assertEquals(ElectionMessage.voted, response.getMessageType());
    ElectionMessage.VersionedVotedData payload = (ElectionMessage.VersionedVotedData) response.getPayload();
    assertEquals(version, payload.getVersion());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) ArrayList(java.util.ArrayList) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 7 with MessageType

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

the class HeartbeatIAmAliveProcessorTest method shouldCorrectlySetTheInstanceIdHeaderInTheGeneratedHeartbeat.

@Test
public void shouldCorrectlySetTheInstanceIdHeaderInTheGeneratedHeartbeat() throws Exception {
    final List<Message> sentOut = new LinkedList<Message>();
    // Given
    MessageHolder holder = mock(MessageHolder.class);
    // The sender, which adds messages outgoing to the list above.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sentOut.add((Message) invocation.getArguments()[0]);
            return null;
        }
    }).when(holder).offer(Matchers.<Message<MessageType>>any());
    ClusterContext mockContext = mock(ClusterContext.class);
    ClusterConfiguration mockConfiguration = mock(ClusterConfiguration.class);
    when(mockConfiguration.getMembers()).thenReturn(new HashMap<InstanceId, URI>() {

        {
            put(new InstanceId(1), URI.create("ha://1"));
            put(new InstanceId(2), URI.create("ha://2"));
        }
    });
    when(mockContext.getConfiguration()).thenReturn(mockConfiguration);
    HeartbeatIAmAliveProcessor processor = new HeartbeatIAmAliveProcessor(holder, mockContext);
    Message incoming = Message.to(mock(MessageType.class), URI.create("ha://someAwesomeInstanceInJapan")).setHeader(Message.INSTANCE_ID, "2").setHeader(Message.FROM, "ha://2");
    // WHEN
    processor.process(incoming);
    // THEN
    assertEquals(1, sentOut.size());
    assertEquals(HeartbeatMessage.i_am_alive, sentOut.get(0).getMessageType());
    assertEquals(new InstanceId(2), ((HeartbeatMessage.IAmAliveState) sentOut.get(0).getPayload()).getServer());
}
Also used : Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 8 with MessageType

use of org.neo4j.cluster.com.message.MessageType 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 9 with MessageType

use of org.neo4j.cluster.com.message.MessageType 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 10 with MessageType

use of org.neo4j.cluster.com.message.MessageType 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

MessageType (org.neo4j.cluster.com.message.MessageType)12 Test (org.junit.Test)10 Message (org.neo4j.cluster.com.message.Message)10 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)9 ArrayList (java.util.ArrayList)6 InstanceId (org.neo4j.cluster.InstanceId)5 URI (java.net.URI)4 LinkedList (java.util.LinkedList)3 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 AtomicBroadcastMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage)2 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)2 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)2 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)2 InetSocketAddress (java.net.InetSocketAddress)1 Collection (java.util.Collection)1 Executor (java.util.concurrent.Executor)1 Channel (org.jboss.netty.channel.Channel)1 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)1