use of org.neo4j.cluster.com.message.Message in project neo4j by neo4j.
the class ElectionStateTest method electionCompletingMakesItBeForgotten.
@Test
public void electionCompletingMakesItBeForgotten() throws Throwable {
// Given
String coordinatorRole = "coordinator";
InstanceId votingInstance = new InstanceId(2);
ElectionCredentials voteCredentialComparable = mock(ElectionCredentials.class);
ElectionContext context = mock(ElectionContext.class);
when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
when(context.getNeededVoteCount()).thenReturn(3);
when(context.getVoteCount(coordinatorRole)).thenReturn(3);
when(context.voted(coordinatorRole, votingInstance, voteCredentialComparable, 4)).thenReturn(true);
MessageHolder holder = mock(MessageHolder.class);
Message vote = Message.to(ElectionMessage.voted, URI.create("cluster://elector"), new ElectionMessage.VersionedVotedData(coordinatorRole, votingInstance, voteCredentialComparable, 4));
// When
election.handle(context, vote, holder);
// Then
verify(context, times(1)).forgetElection(coordinatorRole);
}
use of org.neo4j.cluster.com.message.Message in project neo4j by neo4j.
the class ElectionStateTest method delayedVoteFromPreviousElectionMustNotCauseCurrentElectionToComplete.
@Test
public void delayedVoteFromPreviousElectionMustNotCauseCurrentElectionToComplete() throws Throwable {
// Given
ElectionContext context = mock(ElectionContext.class);
MessageHolder holder = mock(MessageHolder.class);
when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
final String role = "master";
final InstanceId voter = new InstanceId(2);
ElectionCredentials voteCredentialComparable = mock(ElectionCredentials.class);
Message vote = Message.internal(voted, new ElectionMessage.VersionedVotedData(role, voter, voteCredentialComparable, 4));
when(context.voted(role, voter, voteCredentialComparable, 4)).thenReturn(false);
// When
election.handle(context, vote, holder);
verify(context).getLog(Matchers.<Class>any());
verify(context).voted(role, voter, voteCredentialComparable, 4);
// Then
verifyNoMoreInteractions(context, holder);
}
use of org.neo4j.cluster.com.message.Message in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldNotProcessMessagesWithEqualFromAndToHeaders.
@Test
public void shouldNotProcessMessagesWithEqualFromAndToHeaders() throws Exception {
URI to = URI.create("ha://someAwesomeInstanceInJapan");
// GIVEN
MessageHolder outgoing = mock(MessageHolder.class);
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(outgoing, mockContext);
Message incoming = Message.to(mock(MessageType.class), to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
// WHEN
processor.process(incoming);
// THEN
verifyZeroInteractions(outgoing);
}
use of org.neo4j.cluster.com.message.Message 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.Message in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldNotCreateHeartbeatsForNonExistingInstances.
@Test
public void shouldNotCreateHeartbeatsForNonExistingInstances() throws Exception {
// GIVEN
MessageHolder outgoing = mock(MessageHolder.class);
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(outgoing, mockContext);
Message incoming = Message.to(mock(MessageType.class), URI.create("ha://someAwesomeInstanceInJapan")).setHeader(Message.FROM, "some://value").setHeader(Message.INSTANCE_ID, "5");
// WHEN
processor.process(incoming);
// THEN
verifyZeroInteractions(outgoing);
}
Aggregations