Пример #1
0
        public void ActuallyDisposesWithOnlyOneReference()
        {
            var disposable = new ReferenceCounted();

            disposable.Dispose();
            Assert.IsTrue(disposable.IsDisposed);
        }
Пример #2
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);
                    }
                }
        }
Пример #3
0
        public void OnlyDisposesOnce()
        {
            var disposable = new ReferenceCounted();

            disposable.Dispose();
            disposable.Dispose();
            Assert.IsTrue(disposable.IsDisposed);
        }
Пример #4
0
        public void DisposeTokenDisposesWhenCountReachesZero()
        {
            var disposable = new ReferenceCounted();
            var token      = disposable.CreateReferenceToken();

            disposable.Dispose();

            Assert.IsFalse(disposable.IsDisposed);
            token.Dispose();
            Assert.IsTrue(disposable.IsDisposed);
        }
Пример #5
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);
                }
        }
Пример #6
0
        public void DisposeTokenDoesntDecrementCountMuiltipleTimes()
        {
            var disposable = new ReferenceCounted();
            var token      = disposable.CreateReferenceToken();

            token.Dispose();
            Assert.IsFalse(disposable.IsDisposed);
            token.Dispose();
            Assert.IsFalse(disposable.IsDisposed);
            token.Dispose();
            Assert.IsFalse(disposable.IsDisposed);
            token.Dispose();
            Assert.IsFalse(disposable.IsDisposed);

            disposable.Dispose();
            Assert.IsTrue(disposable.IsDisposed);
        }
Пример #7
0
        public void DoesNotDisposeUntilCountReachesZero()
        {
            var disposable = new ReferenceCounted();

            for (int cnt = 0; cnt < 100; cnt++)
            {
                disposable.AddReference();
            }

            for (int cnt = 0; cnt < 100; cnt++)
            {
                disposable.Dispose();
                Assert.IsFalse(disposable.IsDisposed);
            }

            disposable.Dispose();
            Assert.IsTrue(disposable.IsDisposed);
        }