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 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); } }
public void T002_SetTraceEnabledForAll() { var sut = new TracedDisposableControl(); Assert.False(sut.IsTraceEnabled(null)); Assert.False(sut.IsTraceEnabled(typeof(int))); }
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); }
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)); }
public TracedDisposable(TracedDisposableControl tracedDisposableControl) { this._TracedDisposableControl = tracedDisposableControl; tracedDisposableControl ??= TracedDisposableControl.Instance; if (tracedDisposableControl.IsTraceEnabled(this.GetType())) { this._CtorStackTrace = TracedDisposableControl.GetStackTrace(); } }
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); }
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))); }
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); } }
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 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; } }
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 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 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); }
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)"); }
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); } }
public TracedDisposableTest004( bool disposingExpected, TracedDisposableControl tracedDisposableControl) : base(tracedDisposableControl) { this.DisposingExpected = disposingExpected; }
public TestTracedDisposable(Action <bool> onDispose, TracedDisposableControl tracedDisposableControl) : base(tracedDisposableControl) { this._OnDispose = onDispose; }