Search in sources :

Example 16 with MessageHolder

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);
}
Also used : Message(org.neo4j.cluster.com.message.Message) TrackingMessageHolder(org.neo4j.cluster.com.message.TrackingMessageHolder) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) Test(org.junit.Test)

Example 17 with MessageHolder

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);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI)

Example 18 with MessageHolder

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());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) ArrayList(java.util.ArrayList) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 19 with MessageHolder

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);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) Test(org.junit.Test)

Example 20 with MessageHolder

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);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) InstanceId(org.neo4j.cluster.InstanceId) Test(org.junit.Test)

Aggregations

MessageHolder (org.neo4j.cluster.com.message.MessageHolder)29 Test (org.junit.Test)28 Message (org.neo4j.cluster.com.message.Message)19 InstanceId (org.neo4j.cluster.InstanceId)14 URI (java.net.URI)11 MessageType (org.neo4j.cluster.com.message.MessageType)11 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)10 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)8 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)5 MessageArgumentMatcher (org.neo4j.cluster.protocol.MessageArgumentMatcher)5 AtomicBroadcastMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage)5 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)5 TrackingMessageHolder (org.neo4j.cluster.com.message.TrackingMessageHolder)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 HashMap (java.util.HashMap)2 Executor (java.util.concurrent.Executor)2 ConfigurationRequestState (org.neo4j.cluster.protocol.cluster.ClusterMessage.ConfigurationRequestState)2