Beispiel #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testLockCounters() throws InterruptedException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TestLockCounters()
        {
            RagManager   ragManager = new RagManager();
            LockResource resource   = new LockResource(ResourceTypes.NODE, 1L);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final RWLock lock = createRWLock(ragManager, resource);
            RWLock          @lock              = CreateRWLock(ragManager, resource);
            LockTransaction lockTransaction    = new LockTransaction();
            LockTransaction anotherTransaction = new LockTransaction();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LockTransaction writeTransaction = new LockTransaction();
            LockTransaction writeTransaction = new LockTransaction();

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

            @lock.Mark();
            @lock.AcquireReadLock(LockTracer.NONE, lockTransaction);
            @lock.Mark();
            @lock.AcquireReadLock(LockTracer.NONE, anotherTransaction);

            assertEquals(2, @lock.ReadCount);
            assertEquals(0, @lock.WriteCount);
            assertEquals(2, @lock.TxLockElementCount);

            ThreadStart writer = CreateWriter(@lock, writeTransaction, writerCompletedLatch);

            _executor.submit(writer);

            WaitWaitingThreads(@lock, 1);

            // check that all reader, writes, threads counters are correct
            assertEquals(2, @lock.ReadCount);
            assertEquals(0, @lock.WriteCount);
            assertEquals(3, @lock.TxLockElementCount);
            assertEquals(1, @lock.WaitingThreadsCount);

            @lock.ReleaseReadLock(lockTransaction);
            @lock.ReleaseReadLock(anotherTransaction);
            writerCompletedLatch.await();

            // test readers and waiting thread gone
            assertEquals(0, @lock.ReadCount);
            assertEquals(1, @lock.WriteCount);
            assertEquals(1, @lock.TxLockElementCount);
            assertEquals(0, @lock.WaitingThreadsCount);

            @lock.ReleaseWriteLock(writeTransaction);

            // check lock is clean in the end
            assertEquals(0, @lock.TxLockElementCount);
            assertEquals(0, @lock.WaitingThreadsCount);
            assertEquals(0, @lock.ReadCount);
            assertEquals(0, @lock.WriteCount);
        }
Beispiel #2
0
        /*
         * In case if writer thread can't grab write lock now, it should be added to
         * into a waiting list, wait till resource will be free and grab it.
         */
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(timeout = TEST_TIMEOUT_MILLIS) public void testWaitingWriterLock() throws InterruptedException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TestWaitingWriterLock()
        {
            RagManager   ragManager = new RagManager();
            LockResource resource   = new LockResource(ResourceTypes.NODE, 1L);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final RWLock lock = createRWLock(ragManager, resource);
            RWLock @lock = CreateRWLock(ragManager, resource);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LockTransaction lockTransaction = new LockTransaction();
            LockTransaction lockTransaction = new LockTransaction();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LockTransaction anotherTransaction = new LockTransaction();
            LockTransaction anotherTransaction = new LockTransaction();

            @lock.Mark();
            @lock.AcquireReadLock(LockTracer.NONE, lockTransaction);
            @lock.Mark();
            @lock.AcquireReadLock(LockTracer.NONE, anotherTransaction);

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

            ThreadStart writer = CreateWriter(@lock, lockTransaction, writerCompletedLatch);

            // start writer that will be placed in a wait list
            _executor.execute(writer);

            // wait till writer will be added into a list of waiters
            WaitWaitingThreads(@lock, 1);

            assertEquals("No writers for now.", 0, @lock.WriteCount);
            assertEquals(2, @lock.ReadCount);

            // releasing read locks that will allow writer to grab the lock
            @lock.ReleaseReadLock(lockTransaction);
            @lock.ReleaseReadLock(anotherTransaction);

            // wait till writer will have write lock
            writerCompletedLatch.await();

            assertEquals(1, @lock.WriteCount);
            assertEquals(0, @lock.ReadCount);

            // now releasing write lock
            @lock.ReleaseWriteLock(lockTransaction);

            assertEquals("Lock should not have any writers left.", 0, @lock.WriteCount);
            assertEquals("No waiting threads left.", 0, @lock.WaitingThreadsCount);
            assertEquals("No lock elements left.", 0, @lock.TxLockElementCount);
        }
Beispiel #3
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
//ORIGINAL LINE: private Runnable createFailedReader(final RWLock lock, final LockTransaction transaction, final java.util.concurrent.CountDownLatch latch)
        private ThreadStart CreateFailedReader(RWLock @lock, LockTransaction transaction, System.Threading.CountdownEvent latch)
        {
            return(() =>
            {
                @lock.Mark();
                assertFalse(@lock.AcquireReadLock(LockTracer.NONE, transaction));
                latch.Signal();
            });
        }
Beispiel #4
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
//ORIGINAL LINE: private Runnable createReaderForDeadlock(final RWLock node, final LockTransaction transaction, final java.util.concurrent.CountDownLatch latch)
        private ThreadStart CreateReaderForDeadlock(RWLock node, LockTransaction transaction, System.Threading.CountdownEvent latch)
        {
            return(() =>
            {
                try
                {
                    node.Mark();
                    node.AcquireReadLock(LockTracer.NONE, transaction);
                }
                catch (DeadlockDetectedException)
                {
                    latch.Signal();
                }
            });
        }
Beispiel #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void assertReadLockDoesNotLeakMemory()
        public virtual void AssertReadLockDoesNotLeakMemory()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final RagManager ragManager = new RagManager();
            RagManager ragManager = new RagManager();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LockResource resource = new LockResource(org.neo4j.kernel.impl.locking.ResourceTypes.NODE, 0);
            LockResource resource = new LockResource(ResourceTypes.NODE, 0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final RWLock lock = createRWLock(ragManager, resource);
            RWLock @lock = CreateRWLock(ragManager, resource);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.graphdb.Transaction tx1 = mock(org.neo4j.graphdb.Transaction.class);
            Transaction tx1 = mock(typeof(Transaction));

            @lock.Mark();
            @lock.AcquireReadLock(LockTracer.NONE, tx1);
            @lock.Mark();

            assertEquals(1, @lock.TxLockElementCount);
            @lock.ReleaseReadLock(tx1);
            assertEquals(0, @lock.TxLockElementCount);
        }
Beispiel #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(timeout = TEST_TIMEOUT_MILLIS) public void testThreadRemovedFromWaitingListOnDeadlock() throws InterruptedException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TestThreadRemovedFromWaitingListOnDeadlock()
        {
            RagManager   ragManager = Mockito.mock(typeof(RagManager));
            LockResource resource   = new LockResource(ResourceTypes.NODE, 1L);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final RWLock lock = createRWLock(ragManager, resource);
            RWLock @lock = CreateRWLock(ragManager, resource);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LockTransaction lockTransaction = new LockTransaction();
            LockTransaction lockTransaction = new LockTransaction();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LockTransaction anotherTransaction = new LockTransaction();
            LockTransaction anotherTransaction = new LockTransaction();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch exceptionLatch = new java.util.concurrent.CountDownLatch(1);
            System.Threading.CountdownEvent exceptionLatch = new System.Threading.CountdownEvent(1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch completionLatch = new java.util.concurrent.CountDownLatch(1);
            System.Threading.CountdownEvent completionLatch = new System.Threading.CountdownEvent(1);

            Mockito.doNothing().doAnswer(invocation =>
            {
                exceptionLatch.Signal();
                throw new DeadlockDetectedException("Deadlock");
            }).when(ragManager).checkWaitOn(@lock, lockTransaction);

            @lock.Mark();
            @lock.Mark();
            @lock.AcquireReadLock(LockTracer.NONE, lockTransaction);
            @lock.AcquireReadLock(LockTracer.NONE, anotherTransaction);

            // writer will be added to a waiting list
            // then spurious wake up will be simulated
            // and deadlock will be detected
            ThreadStart writer = () =>
            {
                try
                {
                    @lock.Mark();
                    @lock.AcquireWriteLock(LockTracer.NONE, lockTransaction);
                }
                catch (DeadlockDetectedException)
                {
                    // ignored
                }
                completionLatch.Signal();
            };

            _executor.execute(writer);

            WaitWaitingThreads(@lock, 1);

            // sending notify for all threads till our writer will not cause deadlock exception
            do
            {
                //noinspection SynchronizationOnLocalVariableOrMethodParameter
                lock ( @lock )
                {
                    Monitor.PulseAll(@lock);
                }
            } while (exceptionLatch.CurrentCount == 1);

            // waiting for writer to finish
            completionLatch.await();

            assertEquals("In case of deadlock caused by spurious wake up " + "thread should be removed from waiting list", 0, @lock.WaitingThreadsCount);
        }