use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.
the class ProposerStateTest method ifProposingWithClosedInstanceThenRetryWithNextInstance.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void ifProposingWithClosedInstanceThenRetryWithNextInstance() throws Throwable {
ProposerContext context = Mockito.mock(ProposerContext.class);
when(context.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId instanceId = new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(42);
PaxosInstanceStore paxosInstanceStore = new PaxosInstanceStore();
// The instance is closed
// the instance
PaxosInstance paxosInstance = new PaxosInstance(paxosInstanceStore, instanceId);
// is closed for that conversation, not really important
paxosInstance.closed(instanceId, "1/15#");
when(context.unbookInstance(instanceId)).thenReturn(Message.internal(ProposerMessage.accepted, "the closed payload"));
// required for
when(context.getPaxosInstance(instanceId)).thenReturn(paxosInstance);
// But in the meantime it was reused and has now (of course) timed out
String theTimedoutPayload = "the timed out payload";
Message message = Message.internal(ProposerMessage.phase1Timeout, theTimedoutPayload);
message.setHeader(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId.INSTANCE, instanceId.toString());
// Handle it
MessageHolder mockHolder = mock(MessageHolder.class);
ProposerState.proposer.handle(context, message, mockHolder);
// Verify it was resent as a propose with the same value
verify(mockHolder, times(1)).offer(Matchers.<Message<? extends MessageType>>argThat(new MessageArgumentMatcher().onMessageType(ProposerMessage.propose).withPayload(theTimedoutPayload)));
verify(context, times(1)).unbookInstance(instanceId);
}
use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.
the class SnapshotStateTest method baseNoSendTest.
public void baseNoSendTest(Map<InstanceId, URI> extraMembers) throws Throwable {
URI me = URI.create("cluster://me");
Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
final InstanceId myId = new InstanceId(1);
members.put(myId, me);
members.putAll(extraMembers);
ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
when(clusterConfiguration.getMembers()).thenReturn(members);
when(clusterConfiguration.getElected(ClusterConfiguration.COORDINATOR)).thenReturn(myId);
when(clusterConfiguration.getUriForId(myId)).thenReturn(me);
ClusterContext clusterContext = mock(ClusterContext.class);
when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
when(clusterContext.getMyId()).thenReturn(myId);
SnapshotContext context = mock(SnapshotContext.class);
when(context.getClusterContext()).thenReturn(clusterContext);
when(context.getSnapshotProvider()).thenReturn(mock(SnapshotProvider.class));
Message<SnapshotMessage> message = Message.to(SnapshotMessage.refreshSnapshot, me);
MessageHolder outgoing = mock(MessageHolder.class);
SnapshotState newState = (SnapshotState) SnapshotState.ready.handle(context, message, outgoing);
assertThat(newState, equalTo(SnapshotState.ready));
Mockito.verifyZeroInteractions(outgoing);
}
use of org.neo4j.cluster.com.message.MessageHolder 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.MessageHolder in project neo4j by neo4j.
the class ElectionStateTest method testElectionFromDemoteIsRejectedIfNoQuorum.
@Test
public void testElectionFromDemoteIsRejectedIfNoQuorum() throws Throwable {
ElectionContext context = mock(ElectionContext.class);
ClusterContext clusterContextMock = mock(ClusterContext.class);
when(context.electionOk()).thenReturn(false);
when(clusterContextMock.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
when(context.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
MessageHolder holder = mock(MessageHolder.class);
election.handle(context, Message.<ElectionMessage>internal(demote), holder);
verifyZeroInteractions(holder);
}
use of org.neo4j.cluster.com.message.MessageHolder 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);
}
Aggregations