Search in sources :

Example 1 with MessageSender

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

Aggregations

Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Executor (java.util.concurrent.Executor)1 Test (org.junit.Test)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 Message (org.neo4j.cluster.com.message.Message)1 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)1 MessageSender (org.neo4j.cluster.com.message.MessageSender)1 MessageSource (org.neo4j.cluster.com.message.MessageSource)1 MessageType (org.neo4j.cluster.com.message.MessageType)1 StateMachine (org.neo4j.cluster.statemachine.StateMachine)1 Timeouts (org.neo4j.cluster.timeout.Timeouts)1