Esempio n. 1
0
        public void T004_SetTraceEnabledForType_Null()
        {
            var sut = new TracedDisposableControl();

            sut.SetTraceEnabledForAll(false);
            Assert.False(sut.IsTraceEnabled(null));
            sut.SetTraceEnabledForAll(true);
            Assert.True(sut.IsTraceEnabled(null));
        }
Esempio n. 2
0
        public void ContainingDisposableActionIsCalledOnDispose()
        {
            int cntFinalized = 0;
            var tdc          = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };
            var d         = false;
            var f         = false;
            var watchDog1 = new DummyDisposable(() => { d = true; }, () => { f = true; });

            Assert.False(watchDog1.IsDisposed);
            for (int i = 0; i < 100; i++)
            {
                var sut = new ContainingDisposable <DummyDisposable>(watchDog1);
                sut.Dispose();
                sut = null;
            }
            Assert.True(watchDog1.IsDisposed);
            //
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            //
            Assert.True(d);
            Assert.False(f);
        }
Esempio n. 3
0
        public void TracedDisposable003()
        {
            {
                var tdc = new TracedDisposableControl();
                tdc.SetTraceEnabledForAll(true);
                bool?            disposeQ = null;
                var              sut      = new TestTracedDisposable((d) => { disposeQ = d; }, tdc);
                IDisposableState sutDS    = sut;
                Assert.False(sutDS.IsDisposed());
                Assert.False(sutDS.IsFinalizeSuppressed());
                Assert.NotNull(sutDS.GetReportFinalizedInfo().CtorStackTrace);
                sut.Dispose();
                Assert.True(sutDS.IsDisposed());
                Assert.True(sutDS.IsFinalizeSuppressed());
                Assert.Equal(true, disposeQ);
            }

            {
                var tdc = new TracedDisposableControl();
                // NOT tdc.SetTraceEnabledForAll(true);
                bool?            disposeQ = null;
                var              sut      = new TestTracedDisposable((d) => { disposeQ = d; }, tdc);
                IDisposableState sutDS    = sut;
                Assert.False(sutDS.IsDisposed());
                Assert.False(sutDS.IsFinalizeSuppressed());
                Assert.Null(sutDS.GetReportFinalizedInfo().CtorStackTrace);
                sut.Dispose();
                Assert.True(sutDS.IsDisposed());
                Assert.True(sutDS.IsFinalizeSuppressed());
                Assert.Equal(true, disposeQ);
            }
        }
Esempio n. 4
0
        public void ResourceDisposableTest02()
        {
            int cntFinalized = 0;
            var tdc          = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };
            var d1        = false;
            var f1        = false;
            var watchDog1 = new DummyDisposable(() => { d1 = true; }, () => { f1 = true; });

            var d2        = false;
            var f2        = false;
            var watchDog2 = new DummyDisposable(() => { d2 = true; }, () => { f2 = true; });

            var sut = ResourceDisposable.Create(watchDog1);

            Assert.Same(watchDog1, sut.Resource);
            Assert.False(watchDog2.IsDisposed);
            Assert.False(d1);
            Assert.False(f1);

            sut.Resource = watchDog2;
            Assert.Same(watchDog2, sut.Resource);

            Assert.Same(watchDog2, sut.ReadResourceAndForget());
            Assert.Null(sut.ReadResourceAndForget());

            Assert.False(watchDog2.IsDisposed);
            Assert.False(d2);
            Assert.False(f2);

            Assert.False(((IDisposableState)sut).IsDisposed());
            Assert.True(((IDisposableState)sut).IsFinalizeSuppressed());


            sut.Resource = watchDog2;
            Assert.Same(watchDog2, sut.Resource);

            Assert.False(((IDisposableState)sut).IsDisposed());
            Assert.False(((IDisposableState)sut).IsFinalizeSuppressed());

            sut.Dispose();
            Assert.Null(sut.Resource);
            Assert.True(((IDisposableState)sut).IsDisposed());
            Assert.True(((IDisposableState)sut).IsFinalizeSuppressed());

            Assert.True(d1);
            Assert.False(f1);
        }
        public void ActionDisposeT1NoAction()
        {
            int cntDisposed  = 0;
            int cntFinalized = 0;

            var tdc = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };

            var sut = new ActionDispose <int>(null, 0, tdc);

            sut.Dispose(); // no error
            Assert.Equal(0, cntDisposed);
            Assert.Equal(0, cntFinalized);
        }
        public void ActionDisposeActionIsCalledOnDispose()
        {
            int cntDisposed  = 0;
            int cntFinalized = 0;

            var tdc = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };

            var sut = new ActionDispose(() => { cntDisposed++; }, tdc);

            sut.Dispose();
            Assert.Equal(1, cntDisposed);
            Assert.Equal(0, cntFinalized);
        }
        public void ActionDisposeActionWithException()
        {
            int cntDisposed  = 0;
            int cntFinalized = 0;

            var tdc = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);

            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };
            var sut = new ActionDispose(() => { cntDisposed++; throw new ArgumentException("HUGO"); }, tdc);

            try {
                Assert.Throws <ArgumentException>(() => sut.Dispose());
            } catch {
            }
            sut.Dispose(); // no error again
            Assert.Equal(1, cntDisposed);
        }
Esempio n. 8
0
        public void ResourceDisposableTest01()
        {
            int cntFinalized = 0;
            var tdc          = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };

            var d1        = false;
            var f1        = false;
            var watchDog1 = new DummyDisposable(() => { d1 = true; }, () => { f1 = true; });

            var sut = ResourceDisposable.Create(watchDog1);

            Assert.Same(watchDog1, sut.Resource);
            sut.Dispose();

            Assert.Null(sut.Resource);
            Assert.True(d1);
            Assert.False(f1);
        }
        public void ActionDisposeActionIsCalledOnFinalizer()
        {
            int cntDisposed  = 0;
            int cntFinalized = 0;

            var tdc = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cntFinalized++; };

            for (int idx = 0; idx < 100; idx++)
            {
                var sut = new ActionDispose(() => { cntDisposed++; }, tdc);
                // NO sut.Dispose();
                sut = null;
            }
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            System.GC.Collect();
            Assert.True(cntDisposed == cntFinalized, $"{cntDisposed} == {cntFinalized}");
            Assert.True(cntDisposed > 90, $"!({cntDisposed}>90)");
            Assert.True(cntFinalized > 90, $"!({cntFinalized}>90)");
        }
Esempio n. 10
0
        public void TracedDisposable002()
        {
            int cnt = 0;
            var tdc = new TracedDisposableControl();

            tdc.SetTraceEnabledForAll(true);
            tdc.CurrentReportFinalized = (rfi) => { cnt++; };
            {
                bool?disposeQ = null;
                for (int idx = 0; idx < 100; idx++)
                {
                    var sut = new TestTracedDisposable((d) => { disposeQ = d; }, tdc);
                    // NOT sut.Dispose();
                }
                System.GC.Collect(2, GCCollectionMode.Forced);
                System.GC.WaitForPendingFinalizers();

                Assert.True(cnt > 90, $"!({cnt} > 90)");
                Assert.Equal(false, disposeQ);
            }
            cnt = 0;
            {
                bool?disposeQ = null;
                for (int idx = 0; idx < 100; idx++)
                {
                    var sut = new TestTracedDisposable((d) => { disposeQ = d; }, tdc);
                    sut.Dispose();
                }

                System.GC.Collect(2, GCCollectionMode.Forced);
                System.GC.WaitForPendingFinalizers();

                Assert.Equal(0, cnt);
                Assert.Equal(true, disposeQ);
            }
        }