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