Example #1
0
        public void Monitor_Enter_SetsLockTaken()
        {
            bool lockTaken = false;
            var  obj       = new object();

            Monitor.Enter(obj, ref lockTaken);
            Assert.IsTrue(lockTaken);
            Monitor.Exit(obj);
            // The documentation does not specifies that lockTaken variable is
            // set to false after Exit is called
            // Assert.IsFalse(lockTaken);
        }
Example #2
0
        public void Monitor_BasicRecursion()
        {
            var obj = new object();

            Assert.IsTrue(Monitor.TryEnter(obj));
            Assert.IsTrue(Monitor.TryEnter(obj));
            Monitor.Exit(obj);
            //Assert.IsTrue(Monitor.IsEntered(obj));
            Monitor.Enter(obj);
            //Assert.IsTrue(Monitor.IsEntered(obj));
            Monitor.Exit(obj);
            //Assert.IsTrue(Monitor.IsEntered(obj));
            Monitor.Exit(obj);
            //Assert.IsFalse(Monitor.IsEntered(obj));
        }
Example #3
0
        public void Monitor_Exit_Invalid()
        {
            var obj       = new object();
            int valueType = 1;

            AssertExtensions.Throws <ArgumentNullException>("obj", () => Monitor.Exit(null));

#if !WindowsCE
            AssertExtensions.Throws <SynchronizationLockException>(() => Monitor.Exit(obj));
            AssertExtensions.Throws <SynchronizationLockException>(() => Monitor.Exit(new object()));
            AssertExtensions.Throws <SynchronizationLockException>(() => Monitor.Exit(valueType));
#else
            AssertExtensions.Throws <ArgumentException>(() => Monitor.Exit(obj));
            AssertExtensions.Throws <ArgumentException>(() => Monitor.Exit(new object()));
            AssertExtensions.Throws <ArgumentException>(() => Monitor.Exit(valueType));
#endif
        }
Example #4
0
        public void Monitor_DeepRecursion()
        {
            var obj = new object();
            var hc  = obj.GetHashCode();
            // reduced from "(long)int.MaxValue + 2;" to something that will return in a more meaningful time
            const int limit = 10000;

            for (var i = 0L; i < limit; i++)
            {
                Assert.IsTrue(Monitor.TryEnter(obj));
            }

            for (var j = 0L; j < (limit - 1); j++)
            {
                Monitor.Exit(obj);
                //Assert.IsTrue(Monitor.IsEntered(obj));
            }

            Monitor.Exit(obj);
            //Assert.IsTrue(Monitor.IsEntered(obj));
        }
Example #5
0
        public void Monitor_WaitTest()
        {
            var obj       = new object();
            var waitTests =
                new Func <bool>[]
            {
                () => Monitor.Wait(obj, FailTimeoutMilliseconds),
                () => Monitor.Wait(obj, FailTimeoutMilliseconds),
                () => Monitor.Wait(obj, TimeSpan.FromMilliseconds(FailTimeoutMilliseconds)),
                () => Monitor.Wait(obj, TimeSpan.FromMilliseconds(FailTimeoutMilliseconds)),
            };

            var t =
                new Thread(() =>
            {
                Monitor.Enter(obj);
                for (int i = 0; i < waitTests.Length; ++i)
                {
                    Monitor.Pulse(obj);
                    Monitor.Wait(obj, FailTimeoutMilliseconds);
                }
                Monitor.Exit(obj);
            });

            t.IsBackground = true;

            Monitor.Enter(obj);
            t.Start();
            int counter = 0;

            foreach (var waitTest in waitTests)
            {
                Assert.IsTrue(waitTest(), "#" + counter.ToString());
                Monitor.Pulse(obj);
                counter++;
            }
            Monitor.Exit(obj);
        }