use of org.neo4j.cluster.protocol.cluster.ClusterContext 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.protocol.cluster.ClusterContext 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.protocol.cluster.ClusterContext 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);
}
use of org.neo4j.cluster.protocol.cluster.ClusterContext in project neo4j by neo4j.
the class DefaultWinnerStrategyTest method theWinnerShouldHaveTheBestVoteCredentials.
@Test
public void theWinnerShouldHaveTheBestVoteCredentials() throws Exception {
// given
InstanceId instanceOne = new InstanceId(1);
InstanceId instanceTwo = new InstanceId(2);
ClusterContext clusterContext = mock(ClusterContext.class);
final Log log = mock(Log.class);
LogProvider logProvider = new LogProvider() {
@Override
public Log getLog(Class loggingClass) {
return log;
}
@Override
public Log getLog(String name) {
return log;
}
};
when(clusterContext.getLog(DefaultWinnerStrategy.class)).thenReturn(logProvider.getLog(DefaultWinnerStrategy.class));
// when
Collection<Vote> votes = Arrays.asList(new Vote(instanceOne, new IntegerElectionCredentials(1)), new Vote(instanceTwo, new IntegerElectionCredentials(2)));
DefaultWinnerStrategy strategy = new DefaultWinnerStrategy(clusterContext);
org.neo4j.cluster.InstanceId winner = strategy.pickWinner(votes);
// then
assertEquals(instanceTwo, winner);
}
use of org.neo4j.cluster.protocol.cluster.ClusterContext in project neo4j by neo4j.
the class DefaultWinnerStrategyTest method shouldLogElectionProcess.
@Test
public void shouldLogElectionProcess() {
// given
ClusterContext clusterContext = mock(ClusterContext.class);
final Log log = mock(Log.class);
LogProvider logProvider = new LogProvider() {
@Override
public Log getLog(Class loggingClass) {
return log;
}
@Override
public Log getLog(String name) {
return log;
}
};
when(clusterContext.getLog(DefaultWinnerStrategy.class)).thenReturn(logProvider.getLog(DefaultWinnerStrategy.class));
// when
Collection<Vote> votes = Collections.emptyList();
DefaultWinnerStrategy strategy = new DefaultWinnerStrategy(clusterContext);
strategy.pickWinner(votes);
// then
verify(log).debug("Election: received votes [], eligible votes []");
}
Aggregations