Exemple #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldAcceptOnlyFirstRequestWithSameId()
        public virtual void ShouldAcceptOnlyFirstRequestWithSameId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));
            int firstCandidateId = LockToken.nextCandidateId(stateMachine.CurrentToken().id());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 1, r =>
            {
            });
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(1), firstCandidateId), 2, r =>
            {
            });

            // then
            assertEquals(0, stateMachine.CurrentToken().id());
            assertEquals(member(0), stateMachine.CurrentToken().owner());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(1), firstCandidateId + 1), 3, r =>
            {
            });
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 1), 4, r =>
            {
            });

            // then
            assertEquals(1, stateMachine.CurrentToken().id());
            assertEquals(member(1), stateMachine.CurrentToken().owner());
        }
Exemple #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBeIdempotent()
        public virtual void ShouldBeIdempotent()
        {
            // given
            EphemeralFileSystemAbstraction fsa = FileSystemRule.get();

            fsa.Mkdir(TestDir.directory());

            StateMarshal <ReplicatedLockTokenState> marshal = new ReplicatedLockTokenState.Marshal(new MemberId.Marshal());

            DurableStateStorage <ReplicatedLockTokenState> storage = new DurableStateStorage <ReplicatedLockTokenState>(fsa, TestDir.directory(), "state", marshal, 100, NullLogProvider.Instance);

            using (Lifespan lifespan = new Lifespan(storage))
            {
                ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(storage);

                MemberId memberA = member(0);
                MemberId memberB = member(1);

                stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(memberA, 0), 3, r =>
                {
                });

                // when
                stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(memberB, 1), 2, r =>
                {
                });

                // then
                assertEquals(memberA, stateMachine.CurrentToken().owner());
            }
        }
Exemple #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotIssueLocksOnNonLeader() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotIssueLocksOnNonLeader()
        {
            // given
            MemberId me     = member(0);
            MemberId leader = member(1);

            ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage(new ReplicatedLockTokenState()));
            DirectReplicator replicator = new DirectReplicator(replicatedLockStateMachine);

            LeaderLocator leaderLocator = mock(typeof(LeaderLocator));

            when(leaderLocator.Leader).thenReturn(leader);
            Locks locks = mock(typeof(Locks));

            Org.Neo4j.Kernel.impl.locking.Locks_Client client = mock(typeof(Org.Neo4j.Kernel.impl.locking.Locks_Client));
            when(locks.NewClient()).thenReturn(client);

            LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager(me, replicator, leaderLocator, locks, replicatedLockStateMachine);

            // when
            Org.Neo4j.Kernel.impl.locking.Locks_Client lockClient = lockManager.NewClient();
            try
            {
                lockClient.AcquireExclusive(LockTracer.NONE, ResourceTypes.NODE, 0L);
                fail("Should have thrown exception");
            }
            catch (AcquireLockTimeoutException)
            {
                // expected
            }
        }
Exemple #4
0
 public LeaderOnlyLockManager(MemberId myself, Replicator replicator, LeaderLocator leaderLocator, Locks localLocks, ReplicatedLockTokenStateMachine lockTokenStateMachine)
 {
     this._myself                = myself;
     this._replicator            = replicator;
     this._leaderLocator         = leaderLocator;
     this._localLocks            = localLocks;
     this._lockTokenStateMachine = lockTokenStateMachine;
 }
Exemple #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldStartWithInvalidTokenId()
        public virtual void ShouldStartWithInvalidTokenId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));

            // when
            int initialTokenId = stateMachine.CurrentToken().id();

            // then
            assertEquals(initialTokenId, LockToken_Fields.INVALID_LOCK_TOKEN_ID);
        }
Exemple #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldIssueNextLockTokenCandidateId()
        public virtual void ShouldIssueNextLockTokenCandidateId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));
            int firstCandidateId = LockToken.nextCandidateId(stateMachine.CurrentToken().id());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 0, r =>
            {
            });

            // then
            assertEquals(firstCandidateId + 1, LockToken.nextCandidateId(stateMachine.CurrentToken().id()));
        }
Exemple #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldOnlyAcceptNextImmediateId()
        public virtual void ShouldOnlyAcceptNextImmediateId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));
            int firstCandidateId = LockToken.nextCandidateId(stateMachine.CurrentToken().id());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 1), 1, r =>
            {
            });               // not accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), LockToken_Fields.INVALID_LOCK_TOKEN_ID);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 2, r =>
            {
            });               // accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 1), 3, r =>
            {
            });               // accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId + 1);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 4, r =>
            {
            });               // not accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId + 1);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 3), 5, r =>
            {
            });               // not accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId + 1);
        }
Exemple #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldPersistAndRecoverState() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldPersistAndRecoverState()
        {
            // given
            EphemeralFileSystemAbstraction fsa = FileSystemRule.get();

            fsa.Mkdir(TestDir.directory());

            StateMarshal <ReplicatedLockTokenState> marshal = new ReplicatedLockTokenState.Marshal(new MemberId.Marshal());

            MemberId memberA = member(0);
            MemberId memberB = member(1);
            int      candidateId;

            DurableStateStorage <ReplicatedLockTokenState> storage = new DurableStateStorage <ReplicatedLockTokenState>(fsa, TestDir.directory(), "state", marshal, 100, NullLogProvider.Instance);

            using (Lifespan lifespan = new Lifespan(storage))
            {
                ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(storage);

                // when
                candidateId = 0;
                stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(memberA, candidateId), 0, r =>
                {
                });
                candidateId = 1;
                stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(memberB, candidateId), 1, r =>
                {
                });

                stateMachine.Flush();
                fsa.Crash();
            }

            // then
            DurableStateStorage <ReplicatedLockTokenState> storage2 = new DurableStateStorage <ReplicatedLockTokenState>(fsa, TestDir.directory(), "state", marshal, 100, NullLogProvider.Instance);

            using (Lifespan lifespan = new Lifespan(storage2))
            {
                ReplicatedLockTokenState initialState = storage2.InitialState;

                assertEquals(memberB, initialState.Get().owner());
                assertEquals(candidateId, initialState.Get().id());
            }
        }
Exemple #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldSetInitialPendingRequestToInitialState()
        public virtual void ShouldSetInitialPendingRequestToInitialState()
        {
            // Given
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") org.neo4j.causalclustering.core.state.storage.StateStorage<ReplicatedLockTokenState> storage = mock(org.neo4j.causalclustering.core.state.storage.StateStorage.class);
            StateStorage <ReplicatedLockTokenState> storage = mock(typeof(StateStorage));
            MemberId initialHoldingCoreMember     = member(0);
            ReplicatedLockTokenState initialState = new ReplicatedLockTokenState(123, new ReplicatedLockTokenRequest(initialHoldingCoreMember, 3));

            when(storage.InitialState).thenReturn(initialState);

            // When
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(storage);

            // Then
            LockToken initialToken = stateMachine.CurrentToken();

            assertEquals(initialState.Get().owner(), initialToken.Owner());
            assertEquals(initialState.Get().id(), initialToken.Id());
        }
Exemple #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldIssueLocksOnLeader() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldIssueLocksOnLeader()
        {
            // given
            MemberId me = member(0);

            ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage(new ReplicatedLockTokenState()));

            DirectReplicator replicator = new DirectReplicator(replicatedLockStateMachine);

            LeaderLocator leaderLocator = mock(typeof(LeaderLocator));

            when(leaderLocator.Leader).thenReturn(me);
            Locks locks = mock(typeof(Locks));

            Org.Neo4j.Kernel.impl.locking.Locks_Client client = mock(typeof(Org.Neo4j.Kernel.impl.locking.Locks_Client));
            when(locks.NewClient()).thenReturn(client);

            LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager(me, replicator, leaderLocator, locks, replicatedLockStateMachine);

            // when
            lockManager.NewClient().acquireExclusive(LockTracer.NONE, ResourceTypes.NODE, 0L);

            // then
        }