Example #1
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);
        }