use of org.neo4j.cluster.com.message.MessageHolder 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.MessageHolder in project neo4j by neo4j.
the class ElectionStateTest method electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder.
@Test
public void electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder() throws Throwable {
/*
* Ensures that when an instance is alone in the cluster, elections for roles that it holds do not set
* timeouts or try to reach other instances.
*/
// Given
ElectionContext context = mock(ElectionContext.class);
ClusterContext clusterContextMock = mock(ClusterContext.class);
when(clusterContextMock.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
MessageHolder holder = mock(MessageHolder.class);
// These mean the election can proceed normally, by us
when(context.electionOk()).thenReturn(true);
when(context.isInCluster()).thenReturn(true);
when(context.isElector()).thenReturn(true);
// Like it says on the box, we are the only instance
final InstanceId myInstanceId = new InstanceId(1);
Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
members.put(myInstanceId, URI.create("ha://me"));
when(context.getMembers()).thenReturn(members);
// Any role would do, just make sure we have it
final String role = "master";
ElectionContext.VoteRequest voteRequest = new ElectionContext.VoteRequest(role, 13);
when(context.getPossibleRoles()).thenReturn(Collections.<ElectionRole>singletonList(new ElectionRole(role)));
when(context.getElected(role)).thenReturn(myInstanceId);
when(context.voteRequestForRole(new ElectionRole(role))).thenReturn(voteRequest);
// Required for logging
when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
// When
election.handle(context, Message.<ElectionMessage>internal(performRoleElections), holder);
// Then
// Make sure that we asked ourselves to vote for that role and that no timer was set
verify(holder, times(1)).offer(Matchers.argThat(new MessageArgumentMatcher<ElectionMessage>().onMessageType(ElectionMessage.vote).withPayload(voteRequest)));
verify(context, times(0)).setTimeout(Matchers.<String>any(), Matchers.<Message>any());
}
use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.
the class ElectionStateTest method timeoutMakesElectionBeForgotten.
@Test
public void timeoutMakesElectionBeForgotten() throws Throwable {
// Given
String coordinatorRole = "coordinator";
ElectionContext context = mock(ElectionContext.class);
when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
MessageHolder holder = mock(MessageHolder.class);
Message timeout = Message.timeout(ElectionMessage.electionTimeout, Message.internal(performRoleElections), new ElectionState.ElectionTimeoutData(coordinatorRole, null));
// When
election.handle(context, timeout, holder);
// Then
verify(context, times(1)).forgetElection(coordinatorRole);
}
use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldNotGenerateHeartbeatsForSuspicions.
@Test
public void shouldNotGenerateHeartbeatsForSuspicions() throws Exception {
URI to = URI.create("ha://1");
// 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(HeartbeatMessage.suspicions, to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
assertEquals(HeartbeatMessage.suspicions, incoming.getMessageType());
// WHEN
processor.process(incoming);
// THEN
verifyZeroInteractions(outgoing);
}
use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldNotGenerateHeartbeatsForHeartbeats.
@Test
public void shouldNotGenerateHeartbeatsForHeartbeats() throws Exception {
URI to = URI.create("ha://1");
// 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(HeartbeatMessage.i_am_alive, to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
assertEquals(HeartbeatMessage.i_am_alive, incoming.getMessageType());
// WHEN
processor.process(incoming);
// THEN
verifyZeroInteractions(outgoing);
}
Aggregations