public void TestDisposeAsYouGo_AnyAndEnumerate() { lock (typeof(SimpleDisposable)) { // ensure we're starting clean SimpleDisposable.Reset(); var items = SimpleDisposable.GetSome(10).DisposeAsYouGo(); // should call the DAYG 'any' Assert.True(items.Any()); if (items.Any()) { // once you start you have to either enumerate the collection // or dispose of the enumerable. foreach (var i in items) { Console.Write($"each : {i.Number} "); } Console.WriteLine(); } // we can see that a disposable was created, and by disposing of the enumerable // we have cleaned up. Assert.False(SimpleDisposable.UndisposedItems.Any()); } }
public void TestDisposeAsYouGo_Any() { lock (typeof(SimpleDisposable)) { // ensure we're starting clean SimpleDisposable.Reset(); var items = SimpleDisposable.GetSome(10).DisposeAsYouGo(); // should call the DAYG 'any' Assert.True(items.Any()); if (items.Any()) { // once you start you have to either enumerate the collection // or dispose of the enumerable. items.Dispose(); } var more = SimpleDisposable.GetSome(0).DisposeAsYouGo(); // should not find any items. Assert.False(more.Any()); // I've used Any(), but nothing was found. // I should still dispose of more, but nothing was created. // we can see that everything disposable that was created, has been disposed Assert.False(SimpleDisposable.UndisposedItems.Any()); } }
public void TestLeakOfIDisposable() { lock (typeof(SimpleDisposable)) { // ensure we're starting clean SimpleDisposable.Reset(); var items = SimpleDisposable.GetSome(10); // show that this would normally leak Assert.True(items.Any()); // we can see that a disposable was created and nobody cared. Assert.True(SimpleDisposable.UndisposedItems.Any()); } }
public void TestDisposeAsYouGo_AnyWithDisposeAfterLeaksWhenImproperlyUsed() { lock (typeof(SimpleDisposable)) { // ensure we're starting clean SimpleDisposable.Reset(); // but if we add something to dispose after, we should have disposed of the collection var evenmore = SimpleDisposable.GetSome(0).DisposeAsYouGo(new SimpleDisposable()); // still no items. Assert.False(evenmore.Any()); // we can see that a disposable was created and nobody cared. Assert.True(SimpleDisposable.UndisposedItems.Any()); } }
public void TestDisposeAsYouGo_AnyWithDisposeAfterIsOkWhenProperlyUsed() { lock (typeof(SimpleDisposable)) { // ensure we're starting clean SimpleDisposable.Reset(); // but if we add something to dispose after, we should have disposed of the collection using (var evenmore = SimpleDisposable.GetSome(0).DisposeAsYouGo(new SimpleDisposable())) { // still no items. Assert.False(evenmore.Any()); } // we can see that everything disposable that was created, has been disposed Assert.False(SimpleDisposable.UndisposedItems.Any()); } }