Пример #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldHandleBranchedStoreWhenHandshakeFailsWithBranchedDataException() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHandleBranchedStoreWhenHandshakeFailsWithBranchedDataException()
        {
            // Given
            SwitchToSlaveCopyThenBranch switchToSlave = NewSwitchToSlaveSpy();
            URI masterUri = new URI("cluster://localhost?serverId=1");
            URI me        = new URI("cluster://localhost?serverId=2");

            MasterClient masterClient = mock(typeof(MasterClient));

            when(masterClient.Handshake(anyLong(), any(typeof(StoreId)))).thenThrow(new BranchedDataException(""));

            TransactionIdStore transactionIdStore = mock(typeof(TransactionIdStore));

            when(transactionIdStore.LastCommittedTransaction).thenReturn(new TransactionId(42, 42, 42));
            when(transactionIdStore.LastCommittedTransactionId).thenReturn(Org.Neo4j.Kernel.impl.transaction.log.TransactionIdStore_Fields.BASE_TX_ID);

            // When
            try
            {
                switchToSlave.CheckDataConsistency(masterClient, transactionIdStore, _storeId, masterUri, me, Org.Neo4j.Helpers.CancellationRequest_Fields.NeverCancelled);
                fail("Should have thrown " + typeof(BranchedDataException).Name + " exception");
            }
            catch (BranchedDataException)
            {
                // good we got the expected exception
            }

            // Then
            verify(switchToSlave).stopServicesAndHandleBranchedStore(any(typeof(BranchedDataPolicy)), any(typeof(URI)), any(typeof(URI)), any(typeof(CancellationRequest)));
        }
Пример #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void updatesPulledAndPullingScheduledOnSwitchToSlave() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void UpdatesPulledAndPullingScheduledOnSwitchToSlave()
        {
            SwitchToSlaveCopyThenBranch switchToSlave = NewSwitchToSlaveSpy();

            when(_fs.fileExists(any(typeof(File)))).thenReturn(true);
            JobScheduler jobScheduler      = mock(typeof(JobScheduler));
            LifeSupport  communicationLife = mock(typeof(LifeSupport));
            URI          localhost         = LocalhostUri;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.UpdatePullerScheduler pullerScheduler = new org.neo4j.kernel.ha.UpdatePullerScheduler(jobScheduler, org.neo4j.logging.NullLogProvider.getInstance(), updatePuller, 10L);
            UpdatePullerScheduler pullerScheduler = new UpdatePullerScheduler(jobScheduler, NullLogProvider.Instance, _updatePuller, 10L);

            when(_pullerFactory.createUpdatePullerScheduler(_updatePuller)).thenReturn(pullerScheduler);
            // emulate lifecycle start call on scheduler
            doAnswer(invocationOnMock =>
            {
                pullerScheduler.Init();
                return(null);
            }).when(communicationLife).start();

            switchToSlave.SwitchToSlaveConflict(communicationLife, localhost, localhost, mock(typeof(CancellationRequest)));

            verify(_updatePuller).tryPullUpdates();
            verify(communicationLife).add(pullerScheduler);
            verify(jobScheduler).scheduleRecurring(eq(Group.PULL_UPDATES), any(typeof(ThreadStart)), eq(10L), eq(10L), eq(TimeUnit.MILLISECONDS));
        }
Пример #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test @SuppressWarnings("unchecked") public void shouldHandleBranchedStoreWhenMyStoreIdDiffersFromMasterStoreId() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHandleBranchedStoreWhenMyStoreIdDiffersFromMasterStoreId()
        {
            // Given
            SwitchToSlaveCopyThenBranch switchToSlave = NewSwitchToSlaveSpy();
            URI me = new URI("cluster://localhost?serverId=2");

            MasterClient masterClient           = mock(typeof(MasterClient));
            Response <HandshakeResult> response = mock(typeof(Response));

            when(response.ResponseConflict()).thenReturn(new HandshakeResult(1, 2));
            when(masterClient.Handshake(anyLong(), any(typeof(StoreId)))).thenReturn(response);

            StoreId storeId = newStoreIdForCurrentVersion(1, 2, 3, 4);

            TransactionIdStore transactionIdStore = mock(typeof(TransactionIdStore));

            when(transactionIdStore.LastCommittedTransaction).thenReturn(new TransactionId(42, 42, 42));
            when(transactionIdStore.LastCommittedTransactionId).thenReturn(Org.Neo4j.Kernel.impl.transaction.log.TransactionIdStore_Fields.BASE_TX_ID);

            // When
            try
            {
                switchToSlave.CheckDataConsistency(masterClient, transactionIdStore, storeId, new URI("cluster://localhost?serverId=1"), me, Org.Neo4j.Helpers.CancellationRequest_Fields.NeverCancelled);
                fail("Should have thrown " + typeof(MismatchingStoreIdException).Name + " exception");
            }
            catch (MismatchingStoreIdException)
            {
                // good we got the expected exception
            }

            // Then
            verify(switchToSlave).stopServicesAndHandleBranchedStore(any(typeof(BranchedDataPolicy)), any(typeof(URI)), any(typeof(URI)), any(typeof(CancellationRequest)));
        }
Пример #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRestartServicesIfCopyStoreFails() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRestartServicesIfCopyStoreFails()
        {
            when(_updatePuller.tryPullUpdates()).thenReturn(true);

            PageCache pageCacheMock = MockPageCache();

            StoreCopyClient storeCopyClient = mock(typeof(StoreCopyClient));

            doThrow(new Exception()).doNothing().when(storeCopyClient).copyStore(any(typeof(StoreCopyClient.StoreCopyRequester)), any(typeof(CancellationRequest)), any(typeof(MoveAfterCopy)));

            SwitchToSlaveCopyThenBranch switchToSlave = NewSwitchToSlaveSpy(pageCacheMock, storeCopyClient);

            URI localhost = LocalhostUri;

            try
            {
                switchToSlave.SwitchToSlaveConflict(mock(typeof(LifeSupport)), localhost, localhost, mock(typeof(CancellationRequest)));
                fail("Should have thrown an Exception");
            }
            catch (Exception)
            {
                verify(_requestContextFactory, never()).start();
                // Store should have been deleted due to failure in copy
                verify(switchToSlave).cleanStoreDir();

                // Try again, should succeed
                switchToSlave.SwitchToSlaveConflict(mock(typeof(LifeSupport)), localhost, localhost, mock(typeof(CancellationRequest)));
                verify(_requestContextFactory).start();
            }
        }
Пример #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReturnNullIfWhenFailingToPullingUpdatesFromMaster() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReturnNullIfWhenFailingToPullingUpdatesFromMaster()
        {
            // Given
            SwitchToSlaveCopyThenBranch switchToSlave = NewSwitchToSlaveSpy();

            when(_fs.fileExists(any(typeof(File)))).thenReturn(true);
            when(_updatePuller.tryPullUpdates()).thenReturn(false);

            // when
            URI localhost = LocalhostUri;
            URI uri       = switchToSlave.SwitchToSlaveConflict(mock(typeof(LifeSupport)), localhost, localhost, mock(typeof(CancellationRequest)));

            // then
            assertNull(uri);
        }
Пример #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotBranchStoreUnlessWeHaveCopiedDownAReplacement() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotBranchStoreUnlessWeHaveCopiedDownAReplacement()
        {
            // Given
            StoreCopyClient storeCopyClient = mock(typeof(StoreCopyClient));

            doAnswer(invocation =>
            {
                MoveAfterCopy moveAfterCopy = invocation.getArgument(2);
                moveAfterCopy.move(Stream.empty(), new File(""), Function.identity());
                return(null);
            }).when(storeCopyClient).copyStore(any(typeof(StoreCopyClient.StoreCopyRequester)), any(typeof(CancellationRequest)), any(typeof(MoveAfterCopy)));

            PageCache pageCacheMock = mock(typeof(PageCache));
            PagedFile pagedFileMock = mock(typeof(PagedFile));

            when(pagedFileMock.LastPageId).thenReturn(1L);
            when(pageCacheMock.Map(any(typeof(File)), anyInt())).thenReturn(pagedFileMock);

            SwitchToSlaveCopyThenBranch switchToSlave = NewSwitchToSlaveSpy(pageCacheMock, storeCopyClient);

            URI masterUri = new URI("cluster://localhost?serverId=1");
            URI me        = new URI("cluster://localhost?serverId=2");
            CancellationRequest cancellationRequest = Org.Neo4j.Helpers.CancellationRequest_Fields.NeverCancelled;

            MasterClient masterClient = mock(typeof(MasterClient));

            when(masterClient.Handshake(anyLong(), any(typeof(StoreId)))).thenThrow(new BranchedDataException(""));

            TransactionIdStore transactionIdStore = mock(typeof(TransactionIdStore));

            when(transactionIdStore.LastCommittedTransaction).thenReturn(new TransactionId(42, 42, 42));
            when(transactionIdStore.LastCommittedTransactionId).thenReturn(Org.Neo4j.Kernel.impl.transaction.log.TransactionIdStore_Fields.BASE_TX_ID);

            // When
            BranchedDataPolicy branchPolicy = mock(typeof(BranchedDataPolicy));

            switchToSlave.StopServicesAndHandleBranchedStore(branchPolicy, masterUri, me, cancellationRequest);

            // Then
            InOrder inOrder = Mockito.inOrder(storeCopyClient, branchPolicy);

            inOrder.verify(storeCopyClient).copyStore(any(typeof(StoreCopyClient.StoreCopyRequester)), any(typeof(CancellationRequest)), any(typeof(MoveAfterCopy)));
            inOrder.verify(branchPolicy).handle(TestDirectory.databaseDir(), pageCacheMock, NullLogService.Instance);
        }
Пример #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void WhenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient()
        {
            InstanceId me      = new InstanceId(1);
            StoreId    storeId = newStoreIdForCurrentVersion();
            HighAvailabilityMemberContext context = mock(typeof(HighAvailabilityMemberContext));

            when(context.MyId).thenReturn(me);
            AvailabilityGuard      guard        = mock(typeof(DatabaseAvailabilityGuard));
            ObservedClusterMembers members      = mock(typeof(ObservedClusterMembers));
            ClusterMember          masterMember = mock(typeof(ClusterMember));

            when(masterMember.HARole).thenReturn("master");
            when(masterMember.HasRole("master")).thenReturn(true);
            when(masterMember.InstanceId).thenReturn(new InstanceId(2));
            when(masterMember.StoreId).thenReturn(storeId);
            ClusterMember self = new ClusterMember(me);

            when(members.Members).thenReturn(Arrays.asList(self, masterMember));
            when(members.CurrentMember).thenReturn(self);
            DependencyResolver    dependencyResolver = mock(typeof(DependencyResolver));
            FileSystemAbstraction fs = mock(typeof(FileSystemAbstraction));

            when(fs.FileExists(any(typeof(File)))).thenReturn(true);
            when(dependencyResolver.ResolveDependency(typeof(FileSystemAbstraction))).thenReturn(fs);
            when(dependencyResolver.ResolveDependency(typeof(Monitors))).thenReturn(new Monitors());
            NeoStoreDataSource dataSource = mock(typeof(NeoStoreDataSource));

            when(dataSource.DependencyResolver).thenReturn(dependencyResolver);
            when(dataSource.StoreId).thenReturn(storeId);
            when(dependencyResolver.ResolveDependency(typeof(NeoStoreDataSource))).thenReturn(dataSource);
            when(dependencyResolver.ResolveDependency(typeof(TransactionIdStore))).thenReturn(new SimpleTransactionIdStore());
            when(dependencyResolver.ResolveDependency(typeof(ObservedClusterMembers))).thenReturn(members);
            UpdatePuller updatePuller = mock(typeof(UpdatePuller));

            when(updatePuller.TryPullUpdates()).thenReturn(true);
            when(dependencyResolver.ResolveDependency(typeof(UpdatePuller))).thenReturn(updatePuller);

            ClusterMemberAvailability clusterMemberAvailability = mock(typeof(ClusterMemberAvailability));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();
            TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();

            Election election = mock(typeof(Election));
            HighAvailabilityMemberStateMachine stateMachine = new HighAvailabilityMemberStateMachine(context, guard, members, events, election, NullLogProvider.Instance);

            ClusterMembers clusterMembers = new ClusterMembers(members, stateMachine);

            when(dependencyResolver.ResolveDependency(typeof(ClusterMembers))).thenReturn(clusterMembers);

            stateMachine.Init();
            stateMachine.Start();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.DelegateInvocationHandler<org.neo4j.kernel.ha.com.master.Master> handler = new org.neo4j.kernel.ha.DelegateInvocationHandler<>(org.neo4j.kernel.ha.com.master.Master.class);
            DelegateInvocationHandler <Master> handler = new DelegateInvocationHandler <Master>(typeof(Master));

            MasterClientResolver masterClientResolver = mock(typeof(MasterClientResolver));
            MasterClient         masterClient         = mock(typeof(MasterClient));

            when(masterClient.ProtocolVersion).thenReturn(MasterClient214.PROTOCOL_VERSION);
            when(masterClient.Handshake(anyLong(), any(typeof(StoreId)))).thenReturn(new ResponseAnonymousInnerClass(this, storeId, mock(typeof(ResourceReleaser)), handler));
            when(masterClient.ToString()).thenReturn("TheExpectedMasterClient!");
            when(masterClientResolver.Instantiate(anyString(), anyInt(), anyString(), any(typeof(Monitors)), any(typeof(StoreId)), any(typeof(LifeSupport)))).thenReturn(masterClient);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(2);
            System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(2);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicBoolean switchedSuccessfully = new java.util.concurrent.atomic.AtomicBoolean();
            AtomicBoolean switchedSuccessfully = new AtomicBoolean();

            SwitchToSlave.Monitor monitor = new MonitorAnonymousInnerClass(this, latch, switchedSuccessfully);

            Config config = Config.defaults(ClusterSettings.server_id, me.ToString());

            DatabaseTransactionStats transactionCounters = mock(typeof(DatabaseTransactionStats));

            when(transactionCounters.NumberOfActiveTransactions).thenReturn(0L);

            PageCache pageCacheMock = mock(typeof(PageCache));
            PagedFile pagedFileMock = mock(typeof(PagedFile));

            when(pagedFileMock.LastPageId).thenReturn(1L);
            when(pageCacheMock.Map(any(typeof(File)), anyInt())).thenReturn(pagedFileMock);

            TransactionIdStore transactionIdStoreMock = mock(typeof(TransactionIdStore));

            when(transactionIdStoreMock.LastCommittedTransaction).thenReturn(new TransactionId(0, 0, 0));
            SwitchToSlaveCopyThenBranch switchToSlave = new SwitchToSlaveCopyThenBranch(DatabaseLayout.of(new File("")), NullLogService.Instance, mock(typeof(FileSystemAbstraction)), config, mock(typeof(HaIdGeneratorFactory)), handler, mock(typeof(ClusterMemberAvailability)), mock(typeof(RequestContextFactory)), mock(typeof(PullerFactory), RETURNS_MOCKS), Iterables.empty(), masterClientResolver, monitor, new Org.Neo4j.com.storecopy.StoreCopyClientMonitor_Adapter(), Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave =>
            {
                SlaveServer mock = mock(typeof(SlaveServer));
                when(mock.SocketAddress).thenReturn(new InetSocketAddress("localhost", 123));
                return(mock);
            }, updatePuller, pageCacheMock, mock(typeof(Monitors)), () => transactionCounters);

            ComponentSwitcherContainer   switcherContainer = new ComponentSwitcherContainer();
            HighAvailabilityModeSwitcher haModeSwitcher    = new HighAvailabilityModeSwitcher(switchToSlave, mock(typeof(SwitchToMaster)), election, clusterMemberAvailability, mock(typeof(ClusterClient)), storeSupplierMock(), me, switcherContainer, NeoStoreDataSourceSupplierMock(), NullLogService.Instance);

            haModeSwitcher.Init();
            haModeSwitcher.Start();
            haModeSwitcher.ListeningAt(URI.create("http://localhost:12345"));

            stateMachine.AddHighAvailabilityMemberListener(haModeSwitcher);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReference<org.neo4j.kernel.ha.com.master.Master> ref = new java.util.concurrent.atomic.AtomicReference<>(null);
            AtomicReference <Master> @ref = new AtomicReference <Master>(null);

            //noinspection unchecked
            AbstractComponentSwitcher <object> otherModeSwitcher = new AbstractComponentSwitcherAnonymousInnerClass(this, mock(typeof(DelegateInvocationHandler)), handler, latch, @ref);

            switcherContainer.Add(otherModeSwitcher);
            // When
            events.SwitchToSlave(me);

            // Then
            latch.await();
            assertTrue("mode switch failed", switchedSuccessfully.get());
            Master actual = @ref.get();

            // let's test the toString()s since there are too many wrappers of proxies
            assertEquals(masterClient.ToString(), actual.ToString());

            stateMachine.Stop();
            stateMachine.Shutdown();
            haModeSwitcher.Stop();
            haModeSwitcher.Shutdown();
        }