static void Main(string[] args) { // Using using (DisposableTest obj = new DisposableTest()) { Console.WriteLine("Using case"); } // Dispose DisposableTest obj2 = new DisposableTest(); Console.WriteLine("Dispose case"); obj2.Dispose(); Console.Read(); }
public void CanDisposeThreadedSafely() { var d = new DisposableTest(); d.EnteredDisposeManagedResources.ShouldBeEquivalentTo(false); d.EnteredDisposeUnmanagedResorces.ShouldBeEquivalentTo(false); Thread disposingThread = new Thread(() => { d.Dispose(); d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(true); d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(true); d.IsResourceAllocated.ShouldBeEquivalentTo(false); }); disposingThread.Start(); for (int i = 0; i < 1000; i++) { d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(false); d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(false); Thread tryToDispose = new Thread(() => d.Dispose()); tryToDispose.Start(); } d.IsResourceAllocated.ShouldBeEquivalentTo(false); d.EnteredDisposeManagedResources.ShouldBeEquivalentTo(true); d.EnteredDisposeUnmanagedResorces.ShouldBeEquivalentTo(false); d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(false); d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(false); d.WaitDisposeManagedResources.Set(); d.WaitDisposeUnmanagedResources.Set(); d.HasDisposedManagedResources.WaitOne(TimeSpan.FromSeconds(5)); d.HasDisposedUnmanagedResources.WaitOne(TimeSpan.FromSeconds(5)); d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(true); d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(true); d.TimesDisposeManagedRan.ShouldBeEquivalentTo(1); d.TimesDisposeUnmanagedRan.ShouldBeEquivalentTo(1); }
public void DisposableTestFunc() { DisposableTest disposableTest = new DisposableTest(); //do something disposableTest.Dispose(); using (DisposableTest disposableTest1 = new DisposableTest()) { //do something } //FileStream : Stream : IDisposable }
public void CanDispose() { var d = new DisposableTest(); d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(false); d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(false); d.IsResourceAllocated.ShouldBeEquivalentTo(true); d.WaitDisposeManagedResources.Set(); d.WaitDisposeUnmanagedResources.Set(); d.Dispose(); d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(true); d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(true); d.IsResourceAllocated.ShouldBeEquivalentTo(false); d.HasDisposedManagedResources.WaitOne(1).ShouldBeEquivalentTo(true); d.HasDisposedUnmanagedResources.WaitOne(1).ShouldBeEquivalentTo(true); }
public void TryDisposeTest() { var t1 = new DisposableTest(); t1.TryDispose(); t1.TryDispose(); Should.Throw <ObjectDisposedException>(() => t1.Dispose()); var t2 = new CommonDisposableTest2(); t2.TryDispose(); t2.TryDispose(); Should.Throw <ObjectDisposedException>(() => t2.Dispose()); // In order to get 100% test coverage of all paths we force an exception to get generated // from Dispose and eaten by TryDispose because we never allow the set disposed via doing DI // of ISetOnceValue. // var neverSet = new NeverSetBool(); var t2Test = new CommonDisposableTest2(neverSet); Should.Throw <ObjectDisposedException>(() => t2Test.Dispose()); // Should report an exception t2Test.TryDispose(); // Test that an exception didn't get thrown! neverSet.Value.ShouldBe(default);