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));
}
Aggregations