コード例 #1
0
        public void Dispose_IgnoresMultipleCalls()
        {
            var t = new TestDisposable();

            t.Dispose();
            Assert.AreEqual(1, t.DisposeCount);
            t.Dispose();
            Assert.AreEqual(1, t.DisposeCount);
        }
コード例 #2
0
        public void Dispose_ThrowIfDisposedThrowsObjectDisposedExceptionIfDisposed()
        {
            var t = new TestDisposable();

            t.Dispose();
            t.DoWork(1000);
        }
コード例 #3
0
        public void Dispose_CallsDisposeUnmanagedResources()
        {
            var t = new TestDisposable();

            t.Dispose();
            Assert.AreEqual(true, t.UnmanagedDisposeCalled);
        }
コード例 #4
0
        public void Dispose_EnterBusyThrowsObjectDisposedExceptionIfDisposed()
        {
            var t = new TestDisposable();

            t.Dispose();
            t.DoWorkWithBusyToken(1000);
        }
コード例 #5
0
        public void Dispose_BlockedWhileObjectBusyCalls()
        {
            int doWorkDelayTimeInMs = 1000;
            var t  = new TestDisposable();
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            using (var signal = new System.Threading.ManualResetEvent(false))
            {
                System.Threading.ThreadPool.QueueUserWorkItem(
                    (reserved) =>
                {
                    signal.Set();
                    t.DoWorkWithBusyCalls(doWorkDelayTimeInMs);
                }
                    );
                signal.WaitOne();
                System.Threading.Thread.Sleep(16);

                t.Dispose();
            }
            sw.Stop();

            //If the test takes less time than the amout of time DoWork delayed for,
            //then dispose didn't stop/wait for the busy flag and this represents a bug.
            Assert.IsTrue(sw.Elapsed.TotalMilliseconds >= doWorkDelayTimeInMs);
        }
コード例 #6
0
        public void Dispose_SetsIsDisposed()
        {
            var t = new TestDisposable();

            Assert.AreEqual(false, t.IsDisposed);
            t.Dispose();
            Assert.AreEqual(true, t.IsDisposed);
        }
コード例 #7
0
        public void Dispose_SimultaneousDisposeWaitsForOriginalDisposeToComplete()
        {
            int disposeCallCount = 0;

            var t = new TestDisposable();

            t.SleepMilliseconds = 1000;
            using (var signal = new System.Threading.ManualResetEvent(false))
            {
                System.Threading.ThreadPool.QueueUserWorkItem(
                    (reserved) =>
                {
                    signal.Set();
                    t.Dispose();
                }
                    );
                signal.WaitOne();
                System.Threading.Thread.Sleep(100);

                for (int cnt = 0; cnt < 1000; cnt++)
                {
                    disposeCallCount++;
                    t.Dispose();
                    if (t.IsDisposed)
                    {
                        break;
                    }
                }
            }

            //disposeCallCount should be 1. We are calling t.Dispose on a single thread
            //and the first call inside the loop should block until the call first made
            //outside the loop has finished. The following IsDisposed check should
            //then break out of the loop. If disposeCallCount is ever greater than 1
            //then we failed to wait for the original dispose to complete, and this is a bug.
            Assert.AreEqual(1, disposeCallCount, "Called dispose multiple times in loop");
            Assert.AreEqual(1, t.DisposeCount, "Object was disposed multiple times");
        }
コード例 #8
0
        public void OutputsDoesNotOutputUnregisteredObject()
        {
            DisposableTracker.Enabled = false;
            DisposableTracker.Enabled = true;
            var sb = new StringBuilder();
            //DisposableTracker.CaptureStackTraceAtCreation = true;

            var disposable = new TestDisposable();

            disposable.Dispose();

            DisposableTracker.EnumerateTrackedInstances((str) => sb.Append(str));
            System.Diagnostics.Trace.WriteLine(sb.ToString());
            Assert.IsTrue(sb.Length == 0);
        }
コード例 #9
0
        public void Dispose_IsDisposedReturnsTrueWhileDisposeInProgress()
        {
            var t = new TestDisposable();

            t.SleepMilliseconds = 1000;
            using (var signal = new System.Threading.ManualResetEvent(false))
            {
                System.Threading.ThreadPool.QueueUserWorkItem(
                    (reserved) =>
                {
                    signal.Set();
                    t.Dispose();
                }
                    );
                signal.WaitOne();
                System.Threading.Thread.Sleep(100);

                Assert.AreEqual(0, t.DisposeCount);
                Assert.IsTrue(t.IsDisposed);
            }
        }
コード例 #10
0
        public void DisposableDeregistrationIsLogged()
        {
            DisposableTracker.Enabled = false;
            DisposableTracker.Enabled = true;
            var sb = new StringBuilder();

            //DisposableTracker.CaptureStackTraceAtCreation = true;

            DisposableTracker.DisposableUnregisteredLogger = (a) =>
            {
                sb.AppendLine("Disposed " + a.Instance.GetType().FullName + " at " + a.CreationStackTrace + " at " + a.RegisteredAt + Environment.NewLine + a.State);
            };

            var disposable = new TestDisposable();

            disposable.Dispose();

            System.Diagnostics.Trace.WriteLine(sb.ToString());
            Assert.IsTrue(sb.Length > 0);
            Assert.IsTrue(sb.ToString().StartsWith("Disposed " + typeof(TestDisposable).FullName));
        }
コード例 #11
0
        public void DisposableRegistrationIsLogged()
        {
            DisposableTracker.Enabled = false;
            DisposableTracker.Enabled = true;
            var sb = new StringBuilder();

            //DisposableTracker.CaptureStackTraceAtCreation = true;
            DisposableTracker.DisposableRegisteredLogger = (a) => sb.AppendLine("Created " + a.InstanceType.FullName + " at " + DateTime.Now);

            try
            {
                var disposable = new TestDisposable();
                disposable.Dispose();

                System.Diagnostics.Trace.WriteLine(sb.ToString());
                Assert.IsTrue(sb.Length > 0);
                Assert.IsTrue(sb.ToString().StartsWith("Created " + typeof(TestDisposable).FullName));
            }
            catch (InvalidOperationException ioe)
            {
                var x = ioe;
            }
        }