use of org.neo4j.cluster.protocol.cluster.ClusterContext 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.protocol.cluster.ClusterContext in project neo4j by neo4j.
the class ClusterContextImplTest method instanceEnteringTheClusterMustBeRemovedAsElector.
/*
* This test ensures that an instance that enters the cluster has its elector version reset. That means that
* if it was the elector before its version is now reset so results can be applied. This and the previous tests
* actually perform the same things at different events, one covering for the other.
*/
@Test
public void instanceEnteringTheClusterMustBeRemovedAsElector() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
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.joined(elector, URI.create("cluster://elector"));
// Then
assertEquals(context.getLastElector(), InstanceId.NONE);
assertEquals(context.getLastElectorVersion(), -1);
}
use of org.neo4j.cluster.protocol.cluster.ClusterContext 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.ClusterContext in project neo4j by neo4j.
the class ClusterContextImplTest method shouldGracefullyHandleEmptyDiscoveryHeader.
@Test
public void shouldGracefullyHandleEmptyDiscoveryHeader() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId joining = new InstanceId(2);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
Timeouts timeouts = mock(Timeouts.class);
Executor executor = mock(Executor.class);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
ClusterMessage.ConfigurationRequestState request = mock(ClusterMessage.ConfigurationRequestState.class);
when(request.getJoiningId()).thenReturn(joining);
// When
// Instance 2 contacts us with a request but it is empty
context.addContactingInstance(request, "");
// Then
// The discovery header we generate should still contain that instance
assertEquals("2", context.generateDiscoveryHeader());
}
use of org.neo4j.cluster.protocol.cluster.ClusterContext in project neo4j by neo4j.
the class ClusterContextImplTest method nonElectorFailingMustNotCauseElectorVersionToBeReset.
/*
* This test ensures that an instance that is marked as failed has its elector version reset. This means that
* the instance, once it comes back, will still be able to do elections even if it lost state
*/
@Test
public void nonElectorFailingMustNotCauseElectorVersionToBeReset() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
Timeouts timeouts = mock(Timeouts.class);
Executor executor = mock(Executor.class);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
ArgumentCaptor<HeartbeatListener> listenerCaptor = ArgumentCaptor.forClass(HeartbeatListener.class);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
verify(heartbeatContext).addHeartbeatListener(listenerCaptor.capture());
HeartbeatListener theListener = listenerCaptor.getValue();
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
theListener.failed(new InstanceId(3));
// Then
assertEquals(context.getLastElector(), elector);
assertEquals(context.getLastElectorVersion(), 8);
}
Aggregations