public void CheckDisposeThreadSafety()
        {
            var d = new DisposableMock
            {
                FailOnDispose = true
            };

            Assert.False(d.IsDisposed);
            Assert.Equal(0, d.CallCount);
            d.LockEvent.Reset();
            for (var n = 0; n < 10; ++n)
            {
                var t = new Thread(() => { d.Dispose(); })
                {
                    Name         = $@"Dispose thread {n}.",
                    IsBackground = true
                };
                t.Start();
            }

            // Allow one second for all the threads to block.
            Thread.Sleep(1000);
            d.LockEvent.Set();
            Assert.True(d.IsDisposed);
            Assert.Equal(1, d.CallCount);
        }
        public void DisposableIsDisposedOf()
        {
            var d = new DisposableMock();

            Assert.False(d.IsDisposed);
            d.Dispose();
            Assert.True(d.IsDisposed);
            Assert.True(d.Disposing);
        }
        public void CheckNotDisposedThrows()
        {
            var d = new DisposableMock();

            Assert.False(d.IsDisposed);
            d.PerformCheck();
            d.Dispose();
            Assert.True(d.IsDisposed);
            Assert.Throws <ObjectDisposedException>(() => d.PerformCheck());
        }
        public void DisposeNeverThrowsExceptions()
        {
            var d = new DisposableMock
            {
                FailOnDispose = true
            };

            d.Dispose();
            Assert.True(d.IsDisposed);
            Assert.Equal(1, d.CallCount);
        }
        public void DisposableOnlyCalledOnce()
        {
            var d = new DisposableMock();

            Assert.False(d.IsDisposed);
            Assert.Equal(0, d.CallCount);
            d.Dispose();
            Assert.True(d.IsDisposed);
            Assert.Equal(1, d.CallCount);
            d.Dispose();
            Assert.True(d.IsDisposed);
            Assert.Equal(1, d.CallCount);
        }