示例#1
0
        public void TestIdiotUsesSafeLockDispose()
        {
            object instance = new object();

            using (SafeLock safeLock = new SafeLock(instance, 0))
            {
                ((IDisposable)safeLock).Dispose();//since the using statement has the same boxed pointer to r, we are allowed to dispose
            }
        }
示例#2
0
        public void TestYouCanDisposeSafeLock()
        {
            object instance = new object();

            using (IDisposable safeLock = new SafeLock(instance, 0))
            {
                safeLock.Dispose();//since the using statement has the same boxed pointer to r, we are allowed to dispose
            }
        }
示例#3
0
        public void TestSafeLock()
        {
            var timeout = TimeSpan.FromSeconds(15.0);

            SafeLock.Timeout = timeout / 3;

            Assert.Equal((timeout / 3), SafeLock.Timeout);

            var safeLock = new SafeLock();

            Assert.True(safeLock.TryEnter());
            Assert.True(safeLock.TryEnter());

            safeLock.Exit();
            safeLock.Exit();

            Task.Run(() =>
            {
                using (safeLock.Enter())
                {
                    Thread.Sleep(timeout);
                }
            });

            Thread.Sleep(1000);

            Assert.False(safeLock.TryEnter());

            Assert.Throws <TimeoutException>(() => safeLock.Enter());

            Task.Run(() =>
            {
                using (safeLock.Enter())
                {
                    using (safeLock.Enter())
                    {
                        Thread.Sleep(timeout);
                    }
                }
            });

            Thread.Sleep(1000);

            Assert.Throws <SafeLockException>(() => safeLock.Exit());
        }
示例#4
0
        public void TestSafeLock()
        {
            var timeout = TimeSpan.FromSeconds(15.0);

            SafeLock.Timeout = timeout / 3;

            Assert.Equal((timeout / 3), SafeLock.Timeout);

            var safeLock = new SafeLock();

            Assert.True(safeLock.TryEnter());
            Assert.True(safeLock.TryEnter());

            safeLock.Exit();
            safeLock.Exit();

            Task.Run(() =>
            {
                using (safeLock.Enter())
                {
                    Thread.Sleep(timeout);
                }
            });

            Thread.Sleep(1000);

            Assert.False(safeLock.TryEnter());

            Assert.Throws <TimeoutException>(() => safeLock.Enter());

            bool threwException = false;

            Task.Run(() =>
            {
                try
                {
                    using (safeLock.Enter())
                    {
                        using (safeLock.Enter())
                        {
                            Thread.Sleep(timeout);
                        }
                    }
                }
                catch
                {
                    threwException = true;
                }
            });

            Thread.Sleep(timeout);

            Assert.False(threwException);

            Assert.ThrowsAny <Exception>(() => safeLock.Exit());

            bool pulsed = false;

            Task.Run(() =>
            {
                using (IWaitable sync = safeLock.Enter() as IWaitable)
                {
                    sync.Wait((int)timeout.TotalMilliseconds);

                    pulsed = true;
                }
            });

            Thread.Sleep(timeout / 3);

            using (IWaitable sync = safeLock.Enter() as IWaitable)
            {
                sync.PulseAll();
            }

            Thread.Sleep(1000);

            Assert.True(pulsed);
        }