Beispiel #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void discoveredInstancesShouldBeOnlyOnesWeHaveContactedDirectly() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void DiscoveredInstancesShouldBeOnlyOnesWeHaveContactedDirectly()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));

            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            when(context.GetUriForId(Id(2))).thenReturn(Uri(2));

            IList <ConfigurationRequestState> discoveredInstances = new LinkedList <ConfigurationRequestState>();

            when(context.DiscoveredInstances).thenReturn(discoveredInstances);
            when(context.ShouldFilterContactingInstances()).thenReturn(true);

            MessageHolder             outgoing = mock(typeof(MessageHolder));
            ConfigurationRequestState configurationRequestFromTwo = Configuration(2);
            Message <ClusterMessage>  message = to(configurationRequest, Uri(1), configurationRequestFromTwo).setHeader(Message.HEADER_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.Count == 0);

            // WHEN
            // It subsequently contacts us
            when(context.HaveWeContactedInstance(configurationRequestFromTwo)).thenReturn(true);
            ClusterState.Discovery.handle(context, message, outgoing);

            // Then
            assertTrue(discoveredInstances.Contains(configurationRequestFromTwo));
        }
Beispiel #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldSetDiscoveryHeaderProperly() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldSetDiscoveryHeaderProperly()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));

            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            when(context.GetUriForId(Id(2))).thenReturn(Uri(2));
            when(context.JoiningInstances).thenReturn(singletonList(Uri(2)));

            IList <ConfigurationRequestState> discoveredInstances = new LinkedList <ConfigurationRequestState>();

            when(context.DiscoveredInstances).thenReturn(discoveredInstances);

            TrackingMessageHolder outgoing = new TrackingMessageHolder();

            ClusterMessage.ConfigurationTimeoutState timeoutState = new ClusterMessage.ConfigurationTimeoutState(3);
            Message <ClusterMessage> message = @internal(configurationTimeout, timeoutState);
            string discoveryHeader           = "1,2,3";

            when(context.GenerateDiscoveryHeader()).thenReturn(discoveryHeader);

            // 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
            assertEquals(discoveryHeader, outgoing.First().getHeader(DISCOVERED));
        }
Beispiel #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void joinDeniedHandlingShouldKeepResponseConfiguration() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void JoinDeniedHandlingShouldKeepResponseConfiguration()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));

            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            TrackingMessageHolder         outgoing = new TrackingMessageHolder();
            IDictionary <InstanceId, URI> members  = members(1, 2);

            // WHEN a joining instance receives a denial to join
            ClusterState.Discovery.handle(context, to(joinDenied, Uri(2), ConfigurationResponseState(members)), outgoing);

            // THEN assert that the response contains the configuration
            verify(context).joinDenied(argThat((new ConfigurationResponseStateMatcher()).WithMembers(members)));
        }
Beispiel #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testElectionFromDemoteIsRejectedIfNoQuorum() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TestElectionFromDemoteIsRejectedIfNoQuorum()
        {
            ElectionContext context            = mock(typeof(ElectionContext));
            ClusterContext  clusterContextMock = mock(typeof(ClusterContext));

            when(context.ElectionOk()).thenReturn(false);
            when(clusterContextMock.GetLog(ArgumentMatchers.any())).thenReturn(NullLog.Instance);
            when(context.GetLog(ArgumentMatchers.any())).thenReturn(NullLog.Instance);

            MessageHolder holder = mock(typeof(MessageHolder));

            election.handle(context, Message.@internal(demote), holder);

            verifyZeroInteractions(holder);
        }
Beispiel #5
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());
        }
Beispiel #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldLogElectionProcess()
        public virtual void ShouldLogElectionProcess()
        {
            // given
            ClusterContext clusterContext = mock(typeof(ClusterContext));

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.logging.Log log = mock(org.neo4j.logging.Log.class);
            Log         log         = mock(typeof(Log));
            LogProvider logProvider = new LogProviderAnonymousInnerClass(this, log);

            when(clusterContext.GetLog(typeof(DefaultWinnerStrategy))).thenReturn(logProvider.GetLog(typeof(DefaultWinnerStrategy)));

            // when
            ICollection <Vote> votes = Collections.emptyList();

            DefaultWinnerStrategy strategy = new DefaultWinnerStrategy(clusterContext);

            strategy.PickWinner(votes);

            // then
            verify(log).debug("Election: received votes [], eligible votes []");
        }
Beispiel #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void joinDeniedTimeoutShouldBeHandledWithExceptionIncludingConfiguration() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void JoinDeniedTimeoutShouldBeHandledWithExceptionIncludingConfiguration()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));
            IDictionary <InstanceId, URI> existingMembers = Members(1, 2);

            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            when(context.JoiningInstances).thenReturn(Collections.emptyList());
            when(context.HasJoinBeenDenied()).thenReturn(true);
            when(context.JoinDeniedConfigurationResponseState).thenReturn(ConfigurationResponseState(existingMembers));
            TrackingMessageHolder outgoing = new TrackingMessageHolder();

            // WHEN the join denial actually takes effect (signaled by a join timeout locally)
            ClusterState.Joining.handle(context, to(ClusterMessage.JoiningTimeout, Uri(2)).setHeader(Message.HEADER_CONVERSATION_ID, "bla"), outgoing);

            // THEN assert that the failure contains the received configuration
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType> response = outgoing.single();
            Message <MessageType>       response        = outgoing.Single();
            ClusterEntryDeniedException deniedException = response.Payload;

            assertEquals(existingMembers, deniedException.ConfigurationResponseState.Members);
        }
Beispiel #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void joinDeniedResponseShouldContainRespondersConfiguration() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void JoinDeniedResponseShouldContainRespondersConfiguration()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));
            IDictionary <InstanceId, URI> existingMembers = Members(1, 2);

            when(context.IsCurrentlyAlive(any(typeof(InstanceId)))).thenReturn(true);
            when(context.Members).thenReturn(existingMembers);
            when(context.Configuration).thenReturn(ClusterConfiguration(existingMembers));
            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            TrackingMessageHolder    outgoing = new TrackingMessageHolder();
            Message <ClusterMessage> message  = to(configurationRequest, Uri(1), Configuration(2)).setHeader(Message.HEADER_FROM, Uri(2).ToString());

            // WHEN an instance responds to a join request, responding that the joining instance cannot join
            ClusterState.Entered.handle(context, message, outgoing);

            // THEN assert that the responding instance sends its configuration along with the response
            Message <ClusterMessage> response = outgoing.Single();

            assertTrue(response.Payload is ConfigurationResponseState);
            ConfigurationResponseState responseState = response.Payload;

            assertEquals(existingMembers, responseState.Members);
        }
Beispiel #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void discoveredInstancesShouldNotFilterByDefault() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void DiscoveredInstancesShouldNotFilterByDefault()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));

            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            when(context.GetUriForId(Id(2))).thenReturn(Uri(2));
            when(context.GetUriForId(Id(3))).thenReturn(Uri(3));

            IList <ConfigurationRequestState> discoveredInstances = new LinkedList <ConfigurationRequestState>();

            when(context.DiscoveredInstances).thenReturn(discoveredInstances);

            MessageHolder             outgoing = mock(typeof(MessageHolder));
            ConfigurationRequestState configurationRequestFromTwo = Configuration(2);
            Message <ClusterMessage>  messageFromTwo = to(configurationRequest, Uri(1), configurationRequestFromTwo).setHeader(Message.HEADER_FROM, Uri(2).ToString());
            ConfigurationRequestState configurationRequestFromThree = Configuration(3);
            Message <ClusterMessage>  messageFromThree = to(configurationRequest, Uri(1), configurationRequestFromThree).setHeader(Message.HEADER_FROM, Uri(3).ToString());

            // WHEN
            // We receive a configuration request from an instance which we haven't contacted
            ClusterState.Discovery.handle(context, messageFromTwo, outgoing);

            // THEN
            // Since the setting is on, it should be added to the list anyway
            assertTrue(discoveredInstances.Contains(configurationRequestFromTwo));

            // WHEN
            // Another contacts us as well
            ClusterState.Discovery.handle(context, messageFromThree, outgoing);

            // Then
            // That should be in as well
            assertTrue(discoveredInstances.Contains(configurationRequestFromTwo));
            assertTrue(discoveredInstances.Contains(configurationRequestFromThree));
        }
Beispiel #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotDenyJoinToInstanceThatRejoinsBeforeTimingOut() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotDenyJoinToInstanceThatRejoinsBeforeTimingOut()
        {
            // GIVEN
            ClusterContext context = mock(typeof(ClusterContext));
            IDictionary <InstanceId, URI> existingMembers = Members(1, 2);

            when(context.IsCurrentlyAlive(Id(2))).thenReturn(true);
            when(context.Members).thenReturn(existingMembers);
            when(context.Configuration).thenReturn(ClusterConfiguration(existingMembers));
            when(context.GetLog(any(typeof(Type)))).thenReturn(NullLog.Instance);
            when(context.GetUriForId(Id(2))).thenReturn(Uri(2));
            TrackingMessageHolder    outgoing = new TrackingMessageHolder();
            Message <ClusterMessage> message  = to(configurationRequest, Uri(1), Configuration(2)).setHeader(Message.HEADER_FROM, Uri(2).ToString());

            // WHEN the join denial actually takes effect (signaled by a join timeout locally)
            ClusterState.Entered.handle(context, message, outgoing);

            // THEN assert that the failure contains the received configuration
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType> response = outgoing.single();
            Message <MessageType> response = outgoing.Single();

            assertEquals(ClusterMessage.ConfigurationResponse, response.MessageType);
        }
Beispiel #11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotPickAWinnerIfAllVotesAreForIneligibleCandidates()
        public virtual void ShouldNotPickAWinnerIfAllVotesAreForIneligibleCandidates()
        {
            // given
            InstanceId instanceOne = new InstanceId(1);
            InstanceId instanceTwo = new InstanceId(2);

            ClusterContext clusterContext = mock(typeof(ClusterContext));

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.logging.Log log = mock(org.neo4j.logging.Log.class);
            Log         log         = mock(typeof(Log));
            LogProvider logProvider = new LogProviderAnonymousInnerClass2(this, log);

            when(clusterContext.GetLog(typeof(DefaultWinnerStrategy))).thenReturn(logProvider.GetLog(typeof(DefaultWinnerStrategy)));

            // when
            ICollection <Vote> votes = Arrays.asList(new Vote(instanceOne, new NotElectableElectionCredentials()), new Vote(instanceTwo, new NotElectableElectionCredentials()));

            DefaultWinnerStrategy strategy = new DefaultWinnerStrategy(clusterContext);
            InstanceId            winner   = strategy.PickWinner(votes);

            // then
            assertNull(winner);
        }