Пример #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void ifProposingWithClosedInstanceThenRetryWithNextInstance() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void IfProposingWithClosedInstanceThenRetryWithNextInstance()
        {
            ProposerContext context = Mockito.mock(typeof(ProposerContext));

            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);

            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
            PaxosInstance paxosInstance = new PaxosInstance(paxosInstanceStore, instanceId); // the instance

            paxosInstance.Closed(instanceId, "1/15#");                                       // is closed for that conversation, not really important
            when(context.UnbookInstance(instanceId)).thenReturn(Message.@internal(ProposerMessage.Accepted, "the closed payload"));

            when(context.GetPaxosInstance(instanceId)).thenReturn(paxosInstance);                     // required for

            // 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(typeof(MessageHolder));

            ProposerState.Proposer.handle(context, message, mockHolder);

            // Verify it was resent as a propose with the same value
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(mockHolder, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher().onMessageType(ProposerMessage.propose).withPayload(theTimedoutPayload)));
            verify(mockHolder, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >((new MessageArgumentMatcher()).onMessageType(ProposerMessage.Propose).withPayload(theTimedoutPayload)));
            verify(context, times(1)).unbookInstance(instanceId);
        }
Пример #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt()
        {
            // Given
            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));
            // The instance will be asked for paxos instance 4...
            InstanceId paxosInstanceIdIDontHave          = new InstanceId(4);
            Message <LearnerMessage> messageRequestingId = Message.to(LearnerMessage.LearnRequest, URI.create("c:/1")).setHeader(Message.HEADER_FROM, "c:/2").setHeader(InstanceId.INSTANCE, "4");

            // ...but it does not have it yet
            when(ctx.GetPaxosInstance(paxosInstanceIdIDontHave)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIDontHave));

            // When
            state.handle(ctx, messageRequestingId, outgoing);

            // Then
            // verify there is no logging of the failure
            verify(ctx, never()).notifyLearnMiss(paxosInstanceIdIDontHave);
            // but the learn failed went out anyway
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnFailed).to(URI.create("c:/2"))));
        }
Пример #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue()
        {
            // Given

            IList <URI> allMembers = new List <URI>(3);
            URI         instance1  = URI.create("c:/1");        // this one is failed
            URI         instance2  = URI.create("c:/2");        // this one is ok and will respond
            URI         instance3  = URI.create("c:/3");        // this one is the requesting instance
            URI         instance4  = URI.create("c:/4");        // and this one is ok and will respond too

            allMembers.Add(instance1);
            allMembers.Add(instance2);
            allMembers.Add(instance3);
            allMembers.Add(instance4);

            ISet <Org.Neo4j.cluster.InstanceId> aliveInstanceIds = new HashSet <Org.Neo4j.cluster.InstanceId>();

            Org.Neo4j.cluster.InstanceId id2 = new Org.Neo4j.cluster.InstanceId(2);
            Org.Neo4j.cluster.InstanceId id4 = new Org.Neo4j.cluster.InstanceId(4);
            aliveInstanceIds.Add(id2);
            aliveInstanceIds.Add(id4);

            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));
            InstanceId     paxosInstanceIdIAskedFor = new InstanceId(4);

            when(ctx.LastDeliveredInstanceId).thenReturn(3L);
            when(ctx.LastKnownLearnedInstanceInCluster).thenReturn(5L);
            when(ctx.MemberURIs).thenReturn(allMembers);
            when(ctx.Alive).thenReturn(aliveInstanceIds);
            when(ctx.GetUriForId(id2)).thenReturn(instance2);
            when(ctx.GetUriForId(id4)).thenReturn(instance4);
            when(ctx.GetPaxosInstance(paxosInstanceIdIAskedFor)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIAskedFor));

            Message <LearnerMessage> theCause = Message.to(LearnerMessage.CatchUp, instance2);                // could be anything, really

            // When
            state.handle(ctx, Message.timeout(LearnerMessage.LearnTimedout, theCause), outgoing);

            // Then
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnRequest).to(instance2)));
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnRequest).to(instance4)));
            verifyNoMoreInteractions(outgoing);
        }
Пример #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ElectionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder()
        {
            /*
             * Ensures that when an instance is alone in the cluster, elections for roles that it holds do not set
             * timeouts or try to reach other instances.
             */

            // Given
            ElectionContext context            = mock(typeof(ElectionContext));
            ClusterContext  clusterContextMock = mock(typeof(ClusterContext));

            when(clusterContextMock.GetLog(ArgumentMatchers.any())).thenReturn(NullLog.Instance);
            MessageHolder holder = mock(typeof(MessageHolder));

            // These mean the election can proceed normally, by us
            when(context.ElectionOk()).thenReturn(true);
            when(context.InCluster).thenReturn(true);
            when(context.Elector).thenReturn(true);

            // Like it says on the box, we are the only instance
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.InstanceId myInstanceId = new org.neo4j.cluster.InstanceId(1);
            InstanceId myInstanceId = new InstanceId(1);
            IDictionary <InstanceId, URI> members = new Dictionary <InstanceId, URI>();

            members[myInstanceId] = URI.create("ha://me");
            when(context.Members).thenReturn(members);

            // Any role would do, just make sure we have it
            const string role = "master";
            ElectionContext_VoteRequest voteRequest = new ElectionContext_VoteRequest(role, 13);

            when(context.PossibleRoles).thenReturn(Collections.singletonList(new ElectionRole(role)));
            when(context.GetElected(role)).thenReturn(myInstanceId);
            when(context.VoteRequestForRole(new ElectionRole(role))).thenReturn(voteRequest);

            // Required for logging
            when(context.GetLog(Mockito.any())).thenReturn(NullLog.Instance);

            // When
            election.handle(context, Message.@internal(performRoleElections), holder);

            // Then
            // Make sure that we asked ourselves to vote for that role and that no timer was set
            verify(holder, times(1)).offer(ArgumentMatchers.argThat(new MessageArgumentMatcher <ElectionMessage>()
                                                                    .onMessageType(ElectionMessage.Vote).withPayload(voteRequest)));
            verify(context, never()).setTimeout(ArgumentMatchers.any(), ArgumentMatchers.any());
        }
Пример #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldAddInstanceIdHeaderInCatchUpMessages() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldAddInstanceIdHeaderInCatchUpMessages()
        {
            // Given
            InstanceId           instanceId    = new InstanceId(1);
            HeartbeatState       heartbeat     = HeartbeatState.Heartbeat;
            ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.Instance, "cluster://1", "cluster://2");

            configuration.Joined(instanceId, URI.create("cluster://1"));
            InstanceId otherInstance = new InstanceId(2);

            configuration.Joined(otherInstance, URI.create("cluster://2"));

            Config config = mock(typeof(Config));

            when(config.Get(ClusterSettings.max_acceptors)).thenReturn(10);

            MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, Mockito.mock(typeof(Executor)), NullLogProvider.Instance, Mockito.mock(typeof(ObjectInputStreamFactory)), Mockito.mock(typeof(ObjectOutputStreamFactory)), Mockito.mock(typeof(AcceptorInstanceStore)), Mockito.mock(typeof(Timeouts)), mock(typeof(ElectionCredentialsProvider)), config);

            int lastDeliveredInstanceId = 100;

            context.LearnerContext.LastDeliveredInstanceId = lastDeliveredInstanceId;
            // This gap will trigger the catchUp message that we'll test against
            lastDeliveredInstanceId += 20;

            HeartbeatContext heartbeatContext = context.HeartbeatContext;
            Message          received         = Message.@internal(HeartbeatMessage.IAmAlive, new HeartbeatMessage.IAmAliveState(otherInstance));

            received.setHeader(Message.HEADER_FROM, "cluster://2").SetHeader(Message.HEADER_INSTANCE_ID, "2").setHeader("last-learned", Convert.ToString(lastDeliveredInstanceId));

            // When
            MessageHolder holder = mock(typeof(MessageHolder));

            heartbeat.handle(heartbeatContext, received, holder);

            // Then
            verify(holder, times(1)).offer(ArgumentMatchers.argThat(new MessageArgumentMatcher <LearnerMessage>()
                                                                    .onMessageType(LearnerMessage.catchUp).withHeader(Message.HEADER_INSTANCE_ID, "2")));
        }
Пример #6
0
 private static T[] NullSafeAny <T>()
 {
     return(ArgumentMatchers.argThat(argument => true));
 }