コード例 #1
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);
            }
        }
コード例 #2
0
        public void TracedDisposable004()
        {
            var tdc = new TracedDisposableControl();
            {
                for (int idx = 0; idx < 100; idx++)
                {
                    var sut = new TracedDisposableTest004(true, tdc);
                    Assert.Equal(true, sut.DisposingExpected);
                    sut.Dispose();
                    sut = null;
                }
                System.GC.Collect(2, GCCollectionMode.Forced);
                System.GC.WaitForPendingFinalizers();
                Assert.Equal(true, TracedDisposableTest004.DisposingActual);
            }
            {
                for (int idx = 0; idx < 100; idx++)
                {
                    var sut = new TracedDisposableTest004(false, tdc);
                    Assert.Equal(false, sut.DisposingExpected);
                    sut = null;
                }
                System.GC.Collect(2, GCCollectionMode.Forced);
                System.GC.WaitForPendingFinalizers();

                Assert.Equal(false, TracedDisposableTest004.DisposingActual);
            }
        }
コード例 #3
0
        public void T002_SetTraceEnabledForAll()
        {
            var sut = new TracedDisposableControl();

            Assert.False(sut.IsTraceEnabled(null));
            Assert.False(sut.IsTraceEnabled(typeof(int)));
        }
コード例 #4
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);
        }
コード例 #5
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));
        }
コード例 #6
0
 public TracedDisposable(TracedDisposableControl tracedDisposableControl)
 {
     this._TracedDisposableControl = tracedDisposableControl;
     tracedDisposableControl ??= TracedDisposableControl.Instance;
     if (tracedDisposableControl.IsTraceEnabled(this.GetType()))
     {
         this._CtorStackTrace = TracedDisposableControl.GetStackTrace();
     }
 }
コード例 #7
0
        public void T001_GetStackTrace()
        {
            /*
             * TracedDisposableControlTest.T001_GetStackTrace fileName:G:\github\grimmborium\Brimborium.Extensions\test\Brimborium.Extensions.Abstractions.Test\TracedDisposableControlTest.cs@12:13
             */
            var st = TracedDisposableControl.GetStackTrace();

            Assert.StartsWith("TracedDisposableControlTest.T001_GetStackTrace fileName:", st);
            Assert.Contains(@"Brimborium.Extensions.Disposable.Test\TracedDisposableControlTest.cs@13:13", st);
        }
コード例 #8
0
        public void T005_SetTraceEnabledForType()
        {
            var sut = new TracedDisposableControl();

            sut.SetTraceEnabledForType(typeof(IDisposable), true);
            Assert.True(sut.IsTraceEnabled(typeof(IDisposable)));
            Assert.False(sut.IsTraceEnabled(typeof(int)));

            sut.SetTraceEnabledForType(typeof(string), true);
            Assert.True(sut.IsTraceEnabled(typeof(IDisposable)));
            Assert.True(sut.IsTraceEnabled(typeof(string)));
            Assert.False(sut.IsTraceEnabled(typeof(int)));
        }
コード例 #9
0
 public ActionDispose(Action onDispose, TracedDisposableControl tracedDisposableControl) : base(tracedDisposableControl)
 {
     if (onDispose is object)
     {
         this._OnDispose = onDispose;
     }
     else
     {
         this._OnDispose    = null;
         this._DisposeState = (int)(DisposeState.FinalizeSuppressed);
         System.GC.SuppressFinalize(this);
     }
 }
コード例 #10
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);
        }
コード例 #11
0
 public ContainingDisposable(T resource, TracedDisposableControl tracedDisposableControl) : base(tracedDisposableControl)
 {
     if (resource is null)
     {
         // no need for finalize
         this._DisposeState = (int)(DisposeState.FinalizeSuppressed);
         System.GC.SuppressFinalize(this);
     }
     else
     {
         // System.GC.ReRegisterForFinalize(this);
         this._Resource = resource;
     }
 }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        public void TracedDisposable001()
        {
            int cnt = 0;
            var tdc = new TracedDisposableControl();

            tdc.CurrentReportFinalized = (rfi) => { cnt++; };
            bool?disposeQ = null;
            var  sut      = new TestTracedDisposable((d) => { disposeQ = d; }, tdc);

            Assert.False(((IDisposableState)sut).IsDisposed());
            Assert.False(((IDisposableState)sut).IsFinalizeSuppressed());
            Assert.False(disposeQ.HasValue);
            sut.Dispose();
            Assert.True(((IDisposableState)sut).IsDisposed());
            Assert.True(((IDisposableState)sut).IsFinalizeSuppressed());
            Assert.Equal(true, disposeQ);
            Assert.Equal(0, cnt);
        }
コード例 #15
0
        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);
        }
コード例 #16
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);
        }
コード例 #17
0
        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)");
        }
コード例 #18
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);
            }
        }
コード例 #19
0
 public TracedDisposableTest004(
     bool disposingExpected,
     TracedDisposableControl tracedDisposableControl) : base(tracedDisposableControl)
 {
     this.DisposingExpected = disposingExpected;
 }
コード例 #20
0
 public TestTracedDisposable(Action <bool> onDispose, TracedDisposableControl tracedDisposableControl) : base(tracedDisposableControl)
 {
     this._OnDispose = onDispose;
 }