Exemplo n.º 1
0
        public void DisposeAll_ThrowsAggregateExceptionWhenNotSuppressed()
        {
            var t  = new TestDisposable();
            var t1 = new TestDisposable();

            t1.ErrorOnDispose = true;
            var t2 = new TestDisposable();

            t2.ErrorOnDispose = true;
            var t3 = new TestDisposable();

            try
            {
                DisposeAssistant.DisposeAll(t, t1, t2, t3);
                Assert.Fail("No aggregate exception thronw");
            }
            catch (AggregateException ae)
            {
                Assert.AreEqual(2, ae.InnerExceptions.Count);

                Assert.IsTrue(t.IsDisposed);
                Assert.IsTrue(t1.IsDisposed);
                Assert.IsTrue(t2.IsDisposed);
                Assert.IsTrue(t3.IsDisposed);
            }
        }
Exemplo n.º 2
0
        public void Dispose_EnterBusyThrowsObjectDisposedExceptionIfDisposed()
        {
            var t = new TestDisposable();

            t.Dispose();
            t.DoWorkWithBusyToken(1000);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public void TryDispose_DisposesDisposable()
        {
            //This should do nothing/not error
            var t = new TestDisposable();

            t.TryDispose();
        }
Exemplo n.º 5
0
        public void Dispose_ThrowIfDisposedThrowsObjectDisposedExceptionIfDisposed()
        {
            var t = new TestDisposable();

            t.Dispose();
            t.DoWork(1000);
        }
Exemplo n.º 6
0
        public void Dispose_CallsDisposeUnmanagedResources()
        {
            var t = new TestDisposable();

            t.Dispose();
            Assert.AreEqual(true, t.UnmanagedDisposeCalled);
        }
Exemplo n.º 7
0
        public void UnregisterTrackedTypeStopsTracking()
        {
            DisposableTracker.RegisterTrackedType(typeof(TestDisposable));
            DisposableTracker.RegisterTrackedType(typeof(ReferenceCounted));
            using (var t = new TestDisposable())
                using (var t2 = new ReferenceCounted())
                {
                    //Confirm both items tracked
                    int trackedItems = 0;
                    DisposableTracker.EnumerateTrackedInstances((td) => trackedItems++);
                    Assert.AreEqual(2, trackedItems);
                    trackedItems = 0;

                    //Remove one type, and check only one instance being tracked
                    DisposableTracker.UnregisterTrackedType(typeof(TestDisposable));
                    DisposableTracker.EnumerateTrackedInstances((td) => trackedItems++);
                    Assert.AreEqual(1, trackedItems);
                    trackedItems = 0;

                    //Create another instance of the type NOT being tracked
                    //and ensure still only one tracked instance.
                    using (var t3 = new TestDisposable())
                    {
                        DisposableTracker.EnumerateTrackedInstances((td) => trackedItems++);
                        Assert.AreEqual(1, trackedItems);
                    }
                }
        }
Exemplo n.º 8
0
        public void TryDispose_RethrowsErrorWithNoneOption()
        {
            //This should do nothing/not error
            var t = new TestDisposable();

            t.ErrorOnDispose = true;
            t.TryDispose(DisposeOptions.None);
        }
Exemplo n.º 9
0
        public void Dispose_SetsIsDisposed()
        {
            var t = new TestDisposable();

            Assert.AreEqual(false, t.IsDisposed);
            t.Dispose();
            Assert.AreEqual(true, t.IsDisposed);
        }
Exemplo n.º 10
0
        public void TryDispose_SupressesExceptionWithSuppressOption()
        {
            //This should do nothing/not error
            var t = new TestDisposable();

            t.ErrorOnDispose = true;
            t.TryDispose(DisposeOptions.SuppressExceptions);
        }
Exemplo n.º 11
0
        public void Dispose_IgnoresMultipleCalls()
        {
            var t = new TestDisposable();

            t.Dispose();
            Assert.AreEqual(1, t.DisposeCount);
            t.Dispose();
            Assert.AreEqual(1, t.DisposeCount);
        }
Exemplo n.º 12
0
        public void TryDispose_Rethrows_OutOfMemory()
        {
            //This should do nothing/not error
            TestDisposable o = new TestDisposable()
            {
                ErrorOnDispose          = true,
                UseOutOfMemoryException = true
            };

            o.TryDispose();
        }
Exemplo n.º 13
0
        public void TracksOnlyRegisteredTypes()
        {
            DisposableTracker.RegisterTrackedType(typeof(TestDisposable));
            using (var t = new TestDisposable())
                using (var t2 = new ReferenceCounted())
                {
                    int trackedItems = 0;
                    DisposableTracker.EnumerateTrackedInstances((td) => trackedItems++);

                    Assert.AreEqual(1, trackedItems);
                }
        }
Exemplo n.º 14
0
        public void OutputsUndisposedObject()
        {
            DisposableTracker.Enabled = false;
            DisposableTracker.Enabled = true;
            DisposableTracker.CaptureStackTraceAtCreation = false;

            var disposable = new TestDisposable();
            var sb         = new StringBuilder();

            DisposableTracker.EnumerateTrackedInstances((td) => sb.Append(td.InstanceType.FullName + " created at " + td.CreationStackTrace + " Alive = " + td.IsAlive));
            Assert.IsTrue(sb.Length > 0);
            System.Diagnostics.Trace.WriteLine(sb.ToString());
        }
Exemplo n.º 15
0
        public void OutputsStackTraceWhenCaptured()
        {
            DisposableTracker.Enabled = false;
            DisposableTracker.Enabled = true;
            DisposableTracker.CaptureStackTraceAtCreation = true;

            var disposable = new TestDisposable();
            var sb         = new StringBuilder();

            DisposableTracker.EnumerateTrackedInstances((td) => sb.Append(td.CreationStackTrace));
            Assert.IsTrue(sb.Length > 0);
            System.Diagnostics.Trace.WriteLine(sb.ToString());
        }
Exemplo n.º 16
0
        public void TryDispose_DisposesItemsInNonGenericEnumerable()
        {
            var list = new System.Collections.ArrayList();
            var t    = new TestDisposable();
            var t1   = new TestDisposable();

            list.Add(t);
            list.Add(t1);

            list.TryDispose();

            Assert.IsTrue(t.IsDisposed);
            Assert.IsTrue(t1.IsDisposed);
        }
Exemplo n.º 17
0
        public void TryDispose_DisposesItemsInGenericEnumerable()
        {
            var list = new List <TestDisposable>();
            var t    = new TestDisposable();
            var t1   = new TestDisposable();

            list.Add(t);
            list.Add(t1);

            list.TryDispose();

            Assert.IsTrue(t.IsDisposed);
            Assert.IsTrue(t1.IsDisposed);
        }
Exemplo n.º 18
0
        public void DisposeAll_IgnoresNulls()
        {
            var t  = new TestDisposable();
            var t1 = new TestDisposable();
            var t2 = new TestDisposable();
            var t3 = new TestDisposable();

            DisposeAssistant.DisposeAll(null, t, null, t1, t2, t3, null);

            Assert.IsTrue(t.IsDisposed);
            Assert.IsTrue(t1.IsDisposed);
            Assert.IsTrue(t2.IsDisposed);
            Assert.IsTrue(t3.IsDisposed);
        }
Exemplo n.º 19
0
        public void DisposeAll_DisposesEachitem()
        {
            var t  = new TestDisposable();
            var t1 = new TestDisposable();
            var t2 = new TestDisposable();
            var t3 = new TestDisposable();

            DisposeAssistant.DisposeAll(t, t1, t2, t3);

            Assert.IsTrue(t.IsDisposed);
            Assert.IsTrue(t1.IsDisposed);
            Assert.IsTrue(t2.IsDisposed);
            Assert.IsTrue(t3.IsDisposed);
        }
Exemplo n.º 20
0
        public void IEnumerable_TryDispose_DisposesChildrenAndEnumerableIfDisposable()
        {
            var list = new DisposableEnumerable();
            var t1   = new TestDisposable();
            var t2   = new TestDisposable();

            list.Add(t1);
            list.Add(t2);

            ((IEnumerable <IDisposable>)list).TryDispose(DisposeOptions.None);

            Assert.IsTrue(list.IsDisposed);
            Assert.IsTrue(t1.IsDisposed);
            Assert.IsTrue(t2.IsDisposed);
        }
Exemplo n.º 21
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);
        }
Exemplo n.º 22
0
        public void DisposeAll_IgnoresExceptionsWithSuppressOptionSet()
        {
            var t  = new TestDisposable();
            var t1 = new TestDisposable();

            t1.ErrorOnDispose = true;
            var t2 = new TestDisposable();
            var t3 = new TestDisposable();

            DisposeAssistant.DisposeAll(DisposeOptions.SuppressExceptions, t, t1, t2, t3);

            Assert.IsTrue(t.IsDisposed);
            Assert.IsTrue(t1.IsDisposed);
            Assert.IsTrue(t2.IsDisposed);
            Assert.IsTrue(t3.IsDisposed);
        }
Exemplo n.º 23
0
        public void OutputsFinalizedUndisposedObject()
        {
            DisposableTracker.Enabled = false;
            DisposableTracker.Enabled = true;
            var sb = new StringBuilder();
            //DisposableTracker.CaptureStackTraceAtCreation = true;

            var disposable = new TestDisposable();

            disposable = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();

            DisposableTracker.EnumerateTrackedInstances((td) => sb.Append(td.InstanceType.FullName + " " + (td.IsAlive ? "Alive" : "Dead")));
            System.Diagnostics.Trace.WriteLine(sb.ToString());
            Assert.IsTrue(sb.Length > 0);
            Assert.IsTrue(sb.ToString().Contains("Dead"));
        }
Exemplo n.º 24
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);
            }
        }
Exemplo n.º 25
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));
        }
Exemplo n.º 26
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");
        }
Exemplo n.º 27
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;
            }
        }