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