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); } }
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); }
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); } }