Search in sources :

Example 1 with MessageType

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

the class ElectionStateTest method shouldSendAtomicBroadcastOnJoiningAClusterWithAnEstablishedCoordinator.

@Test
public void shouldSendAtomicBroadcastOnJoiningAClusterWithAnEstablishedCoordinator() throws Throwable {
    // Given
    String winnerURI = "some://winner";
    InstanceId winner = new InstanceId(2);
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder holder = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    ElectionCredentials voteCredentialComparable = mock(ElectionCredentials.class);
    ElectionContext electionContext = mock(ElectionContext.class);
    when(electionContext.voted(eq(COORDINATOR), eq(new InstanceId(1)), eq(voteCredentialComparable), anyLong())).thenReturn(true);
    when(electionContext.getVoteCount(COORDINATOR)).thenReturn(3);
    when(electionContext.getNeededVoteCount()).thenReturn(3);
    when(electionContext.getElectionWinner(COORDINATOR)).thenReturn(winner);
    when(electionContext.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
    when(electionContext.newConfigurationStateChange()).thenReturn(mock(ClusterMessage.VersionedConfigurationStateChange.class));
    when(electionContext.getUriForId(winner)).thenReturn(URI.create(winnerURI));
    // When
    Message<ElectionMessage> votedMessage = Message.to(ElectionMessage.voted, URI.create("some://instance"), new ElectionMessage.VotedData(COORDINATOR, new InstanceId(1), voteCredentialComparable));
    votedMessage.setHeader(Message.FROM, "some://other");
    election.handle(electionContext, votedMessage, holder);
    // Then
    assertEquals(1, messages.size());
    Message<?> message = messages.get(0);
    assertEquals(AtomicBroadcastMessage.broadcast, message.getMessageType());
}
Also used : AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 2 with MessageType

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

the class HeartbeatIAmAliveProcessorTest method shouldRevertToInverseUriLookupIfNoInstanceIdHeader.

/*
     * This test is required to ensure compatibility with the previous version. If we fail on non existing INSTANCE_ID
     * header then heartbeats may pause during rolling upgrades and cause timeouts, which we don't want.
     */
@Test
public void shouldRevertToInverseUriLookupIfNoInstanceIdHeader() throws Exception {
    final List<Message> sentOut = new LinkedList<Message>();
    String instance2UriString = "ha://2";
    // 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.getIdForUri(URI.create(instance2UriString))).thenReturn(new InstanceId(2));
    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.FROM, instance2UriString);
    // 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 3 with MessageType

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

the class StateMachinesTest method shouldAlwaysAddItsInstanceIdToOutgoingMessages.

@Test
public void shouldAlwaysAddItsInstanceIdToOutgoingMessages() throws Exception {
    InstanceId me = new InstanceId(42);
    final List<Message> sentOut = new LinkedList<Message>();
    /*
         * Lots of setup required. Must have a sender that keeps messages so we can see what the machine sent out.
         * We must have the StateMachines actually delegate the incoming message and retrieve the generated outgoing.
         * That means we need an actual StateMachine with a registered MessageType. And most of those are void
         * methods, which means lots of Answer objects.
         */
    // Given
    MessageSender sender = mock(MessageSender.class);
    // The sender, which adds messages outgoing to the list above.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sentOut.addAll((Collection<? extends Message>) invocation.getArguments()[0]);
            return null;
        }
    }).when(sender).process(Matchers.<List<Message<? extends MessageType>>>any());
    StateMachines stateMachines = new StateMachines(NullLogProvider.getInstance(), mock(StateMachines.Monitor.class), mock(MessageSource.class), sender, mock(Timeouts.class), mock(DelayedDirectExecutor.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, me);
    // The state machine, which has a TestMessage message type and simply adds a TO header to the messages it
    // is handed to handle.
    StateMachine machine = mock(StateMachine.class);
    when(machine.getMessageType()).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return TestMessage.class;
        }
    });
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Message message = (Message) invocation.getArguments()[0];
            MessageHolder holder = (MessageHolder) invocation.getArguments()[1];
            message.setHeader(Message.TO, "to://neverland");
            holder.offer(message);
            return null;
        }
    }).when(machine).handle(any(Message.class), any(MessageHolder.class));
    stateMachines.addStateMachine(machine);
    // When
    stateMachines.process(Message.internal(TestMessage.message1));
    // Then
    assertEquals("StateMachines should not make up messages from thin air", 1, sentOut.size());
    Message sent = sentOut.get(0);
    assertTrue("StateMachines should add the instance-id header", sent.hasHeader(Message.INSTANCE_ID));
    assertEquals("StateMachines should add instance-id header that has the correct value", me.toString(), sent.getHeader(Message.INSTANCE_ID));
}
Also used : Message(org.neo4j.cluster.com.message.Message) MessageSender(org.neo4j.cluster.com.message.MessageSender) Timeouts(org.neo4j.cluster.timeout.Timeouts) StateMachine(org.neo4j.cluster.statemachine.StateMachine) MessageSource(org.neo4j.cluster.com.message.MessageSource) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 4 with MessageType

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

the class NetworkReceiverTest method testMessageReceivedOriginFix.

@Test
public void testMessageReceivedOriginFix() throws Exception {
    LogProvider logProvider = mock(LogProvider.class);
    when(logProvider.getLog(NetworkReceiver.class)).thenReturn(mock(Log.class));
    NetworkReceiver networkReceiver = new NetworkReceiver(mock(NetworkReceiver.Monitor.class), mock(NetworkReceiver.Configuration.class), logProvider);
    // This defines where message is coming from
    final InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", PORT);
    final Channel channel = mock(Channel.class);
    when(channel.getLocalAddress()).thenReturn(inetSocketAddress);
    when(channel.getRemoteAddress()).thenReturn(inetSocketAddress);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.getChannel()).thenReturn(channel);
    final Message message = Message.to(new MessageType() {

        @Override
        public String name() {
            return "test";
        }
    }, new URI("cluster://anywhere"));
    MessageEvent messageEvent = mock(MessageEvent.class);
    when(messageEvent.getRemoteAddress()).thenReturn(inetSocketAddress);
    when(messageEvent.getMessage()).thenReturn(message);
    when(messageEvent.getChannel()).thenReturn(channel);
    // the original FROM header should be ignored
    message.setHeader(Message.FROM, "cluster://someplace:1234");
    networkReceiver.new MessageReceiver().messageReceived(ctx, messageEvent);
    assertEquals("FROM header should have been changed to visible ip address: " + message.getHeader(Message.FROM), "cluster://127.0.0.1:1234", message.getHeader(Message.FROM));
}
Also used : Message(org.neo4j.cluster.com.message.Message) Log(org.neo4j.logging.Log) InetSocketAddress(java.net.InetSocketAddress) MessageEvent(org.jboss.netty.channel.MessageEvent) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) URI(java.net.URI) LogProvider(org.neo4j.logging.LogProvider) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 5 with MessageType

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

the class AtomicBroadcastStateTest method shouldNotBroadcastWhenHavingNoQuorumNoCoordinator.

@Test
public void shouldNotBroadcastWhenHavingNoQuorumNoCoordinator() throws Throwable {
    // GIVEN
    AtomicBroadcastContext context = mock(AtomicBroadcastContext.class);
    when(context.hasQuorum()).thenReturn(false);
    InstanceId coordinator = id(1);
    when(context.getCoordinator()).thenReturn(coordinator);
    when(context.getUriForId(coordinator)).thenReturn(uri(1));
    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) InstanceId(org.neo4j.cluster.InstanceId) 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