Search in sources :

Example 21 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class ClusterStateTest method discoveredInstancesShouldBeOnlyOnesWeHaveContactedDirectly.

@Test
public void discoveredInstancesShouldBeOnlyOnesWeHaveContactedDirectly() throws Throwable {
    // GIVEN
    ClusterContext context = mock(ClusterContext.class);
    when(context.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
    when(context.getUriForId(id(2))).thenReturn(uri(2));
    List<ConfigurationRequestState> discoveredInstances = new LinkedList<>();
    when(context.getDiscoveredInstances()).thenReturn(discoveredInstances);
    when(context.shouldFilterContactingInstances()).thenReturn(true);
    MessageHolder outgoing = mock(MessageHolder.class);
    ConfigurationRequestState configurationRequestFromTwo = configuration(2);
    Message<ClusterMessage> message = to(configurationRequest, uri(1), configurationRequestFromTwo).setHeader(Message.FROM, uri(2).toString());
    // WHEN
    // We receive a configuration request from an instance which we haven't contacted
    ClusterState.discovery.handle(context, message, outgoing);
    // THEN
    // It shouldn't be added to the discovered instances
    assertTrue(discoveredInstances.isEmpty());
    // WHEN
    // It subsequently contacts us
    when(context.haveWeContactedInstance(configurationRequestFromTwo)).thenReturn(true);
    ClusterState.discovery.handle(context, message, outgoing);
    // Then
    assertTrue(discoveredInstances.contains(configurationRequestFromTwo));
}
Also used : TrackingMessageHolder(org.neo4j.cluster.com.message.TrackingMessageHolder) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) ConfigurationRequestState(org.neo4j.cluster.protocol.cluster.ClusterMessage.ConfigurationRequestState) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 22 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder 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);
}
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)

Example 23 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder 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);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 24 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder 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());
}
Also used : Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 25 with MessageHolder

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