use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.
the class AtomicBroadcastContextImplTest method shouldHasQuorumWhenOneMachineAliveInAClusterWithOneMachine.
@Test
public void shouldHasQuorumWhenOneMachineAliveInAClusterWithOneMachine() {
//Given
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(heartbeatContext.getAlive()).thenReturn(ids(1));
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(1));
AtomicBroadcastContextImpl context = new AtomicBroadcastContextImpl(null, commonState, null, null, null, // we do not care about other args
heartbeatContext);
//When
boolean hasQuorum = context.hasQuorum();
//Then
assertTrue(hasQuorum);
}
use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.
the class ClusterContextImplTest method nonElectorLeavingTheClusterMustNotAffectElectorInformation.
/*
* This test ensures that an instance that cleanly leaves the cluster but is not the elector has no effect on
* elector id and last version
*/
@Test
public void nonElectorLeavingTheClusterMustNotAffectElectorInformation() throws Throwable {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
InstanceId other = new InstanceId(3);
ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
when(clusterConfiguration.getUriForId(other)).thenReturn(URI.create("cluster://instance2"));
CommonContextState commonContextState = mock(CommonContextState.class);
when(commonContextState.configuration()).thenReturn(clusterConfiguration);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), mock(Timeouts.class), mock(Executor.class), mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), mock(HeartbeatContext.class), mock(Config.class));
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
context.left(other);
// Then
assertEquals(context.getLastElector(), elector);
assertEquals(context.getLastElectorVersion(), 8);
}
use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.
the class HeartbeatContextImplTest method shouldFailAndAliveBothNotifyHeartbeatListenerInDelayedDirectExecutor.
@Test
public void shouldFailAndAliveBothNotifyHeartbeatListenerInDelayedDirectExecutor() throws Throwable {
// Given
InstanceId me = new InstanceId(1);
InstanceId failedMachine = new InstanceId(2);
InstanceId goodMachine = new InstanceId(3);
Timeouts timeouts = mock(Timeouts.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(3));
when(configuration.getMemberIds()).thenReturn(ids(3));
final List<Runnable> runnables = new ArrayList<>();
HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, new DelayedDirectExecutor(NullLogProvider.getInstance()) {
@Override
public synchronized void execute(Runnable command) {
runnables.add(command);
}
});
context.addHeartbeatListener(mock(HeartbeatListener.class));
context.suspicions(goodMachine, new HashSet<>(singletonList(failedMachine)));
// fail
context.suspect(failedMachine);
// alive
context.alive(failedMachine);
// Then
// fail + alive
assertEquals(2, runnables.size());
}
use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration 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.ClusterConfiguration 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());
}
Aggregations