public void TestBarrierMethods()
        {
            // Define variables and constants
            StateMutationBarrier testBarrier = new StateMutationBarrier();
            object lockObj = new object();

            // Set up context


            // Execute
            testBarrier.AcquirePermit().Dispose();
            var permit = testBarrier.AcquirePermit(withLock: lockObj);

            Assert.IsTrue(Monitor.IsEntered(lockObj));
            Assert.IsFalse(testBarrier.IsFrozen);
            permit.Dispose();
#if !DEVELOPMENT && !RELEASE
            try {
                permit.Dispose();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            testBarrier.FreezeMutations();
            Assert.IsTrue(testBarrier.IsFrozen);
            testBarrier.UnfreezeMutations();
            Assert.IsFalse(testBarrier.IsFrozen);
            Thread t = new Thread(testBarrier.FreezeMutations);
            t.Start();
            t.Join();
            Assert.IsTrue(testBarrier.IsFrozen);
            t = new Thread(() => {
                try {
                    testBarrier.AcquirePermit();
                    Assert.Fail();
                }
                catch (ThreadAbortException) {
                    Thread.ResetAbort();
                }
            });
            t.Start();
            Thread.Sleep(500);
            t.Abort();

            // Assert outcome
        }
Example #2
0
            internal MutationPermit(StateMutationBarrier barrier, object additionalLockObject)
            {
                Assure.NotNull(barrier);
                if (Monitor.IsEntered(barrier.freezeLock))
                {
                    throw new ApplicationException("Can not acquire permit: Mutations are locked from current thread.");
                }
#if DEBUG
                isDisposed = false;
#endif
                this.barrier = barrier;
                this.additionalLockObject = additionalLockObject;
                lock (barrier.freezeLock) {
                    ++barrier.activeMutatorsCounter;
                }
                if (additionalLockObject != null)
                {
                    Monitor.Enter(additionalLockObject);
                }
            }