public void OnDisposeMustBeCalledOnGcWhenEnumerationHasNotBeenStarted() { // ARRANGE var testDisposable = new TestDisposable(); void CreateEnumerator() { var enumerator = new AsyncEnumerator <int>(async yield => { await yield.ReturnAsync(1); }, onDispose: () => testDisposable.Dispose()); } // ACT CreateEnumerator(); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true); Thread.Sleep(16); // ASSERT Assert.IsTrue(testDisposable.HasDisposed); }
public void Remove() { TestDisposable[] o = new TestDisposable[4]; for (int i = 0; i < o.Length; i++) { o[i] = new TestDisposable(); } using (var disposer = new Disposables()) { foreach (IDisposable d in o) { disposer.Add(d); } disposer.RemoveAt(2); } for (int i = 0; i < o.Length; i++) { if (i == 2) { Assert.IsFalse(o[i].IsDisposed); } else { Assert.IsTrue(o[i].IsDisposed); } } }
public void TestBaseDisposable() { var sut = new TestDisposable(); Assert.False(sut.IsDisposed); Assert.False(sut.IsDisposing); Task.Run(() => { sut.Dispose(); }); Thread.Sleep(1000); Assert.False(sut.IsDisposed); Assert.True(sut.IsDisposing); Thread.Sleep(5000); Assert.True(sut.IsDisposed); // Dispose when already disposed sut.Dispose(); // Dispose with exception var sut2 = new TestDisposable(); sut2.ShouldThrow = true; sut2.Dispose(); }
public async Task DisposeWithNextLoad_works_with_1_item() { var list = new List <TestDisposable>(); var dataLoader = GetDataLoader(Load); await dataLoader.Load(CancellationToken.None); Assert.Single(list); Assert.False(list[0].IsDisposed); await dataLoader.Load(CancellationToken.None); Assert.Equal(2, list.Count); Assert.True(list[0].IsDisposed); Assert.False(list[1].IsDisposed); async Task <string> Load(CancellationToken ct, IDataLoaderRequest request) { var disposable = new TestDisposable() .DisposeWithNextLoad(request); list.Add((TestDisposable)disposable); return(string.Empty); } }
public async Task LargePayload_DisposesRequest_AfterResponseIsCompleted() { // Arrange var data = new byte[2048]; var character = Encoding.ASCII.GetBytes("a"); for (var i = 0; i < data.Length; i++) { data[i] = character[0]; } var builder = new WebHostBuilder(); RequestDelegate app = async ctx => { var disposable = new TestDisposable(); ctx.Response.RegisterForDispose(disposable); await ctx.Response.Body.WriteAsync(data, 0, 1024); Assert.False(disposable.IsDisposed); await ctx.Response.Body.WriteAsync(data, 1024, 1024); }; builder.Configure(appBuilder => appBuilder.Run(app)); var server = new TestServer(builder); var client = server.CreateClient(); // Act & Assert var response = await client.GetAsync("http://localhost:12345"); }
public async Task DisposeAfterPartialEnumeration() { // ARRANGE var testDisposable = new TestDisposable(); var enumerator = new AsyncEnumerator <int>(async yield => { using (testDisposable) { await yield.ReturnAsync(1); await yield.ReturnAsync(2); await yield.ReturnAsync(3); } }); // ACT await enumerator.MoveNextAsync(); enumerator.Dispose(); // ASSERT Assert.IsTrue(testDisposable.HasDisposed); }
public static UseDisposable Create(TestDisposable disposable) { return(new UseDisposable() { Disposable = disposable }); }
static void Test1() { using (TestDisposable a = new TestDisposable(), b = new TestDisposable()) { a.i += b.i; } }
public async Task DisposeByGCAfterPartialEnumeration() { // ARRANGE var testDisposable = new TestDisposable(); var enumerator = new AsyncEnumerator <int>(async yield => { using (testDisposable) { await yield.ReturnAsync(1); await yield.ReturnAsync(2); await yield.ReturnAsync(3); } }); // ACT // Do partial enumeration. await enumerator.MoveNextAsync(); // Instead of calling enumerator.Dispose(), do garbage collection. enumerator = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true); // Give some time to other thread that does the disposal of the enumerator. // (see finalizer of the AsyncEnumerator for details) await Task.Delay(16); // ASSERT Assert.IsTrue(testDisposable.HasDisposed); }
public static void TestUsing2() { using (var test = new TestDisposable()) { System.Diagnostics.Debug.WriteLine("hello"); } }
public void TestUsing_NoException_DisposeCalled() { //-- Arrange DeriveClassFrom <AncestorRepository.StatementTester>() .DefaultConstructor() .Method <int, int>(x => x.DoTest).Implement((m, input) => { var output = m.Local(initialValue: Static.Prop(() => OutputList)); output.Add(m.Const("BEFORE-USING")); m.Using(Static.Prop(() => InputDisposable)).Do(() => { output.Add(m.Const("INSIDE-USING")); }); output.Add(m.Const("AFTER-USING")); m.Return(0); }); InputDisposable = new TestDisposable(); OutputList = new List <string>(); //-- Act var tester = CreateClassInstanceAs <AncestorRepository.StatementTester>().UsingDefaultConstructor(); tester.DoTest(111); //-- Assert Assert.That(OutputList, Is.EqualTo(new[] { "BEFORE-USING", "INSIDE-USING", "DISPOSE", "AFTER-USING", })); }
public void NullObject() { var disposable = new TestDisposable(); disposable = Disposer.Null(disposable); Assert.IsNull(disposable); }
public void DisposeDisposable() { var o = new TestDisposable(); o.SafeDispose(); Assert.True(o.IsDisposed); }
static void Test1() { TestDisposable a = new TestDisposable(); using (a) { } }
public static void Main() { using (TestDisposable d = new TestDisposable()) { } // Will trace "disposed" UseDisposable use = UseDisposable.Create(new TestDisposable()); // Will trace "disposed" }
public void When_Using_used_with_idisposable_object_Action_is_called_and_disposable_is_disposed() { bool called = false; var disposable = new TestDisposable(); Values.Using(disposable, _ => called = true); Assert.That(disposable.DisposeCalled, Is.True); Assert.That(called, Is.True); }
public void Null() { TestDisposable disposable1 = new TestDisposable(); TestDisposable disposable2 = disposable1; Disposer.Null(ref disposable1); Assert.IsNull(disposable1); Assert.AreEqual(1, disposable2.DisposeCalled); }
public void Test_ThrowIfDisposed() { // Arrange. var test = new TestDisposable(); test.Dispose(); // Act/Assert. Assert.Throws <ObjectDisposedException>(() => test.Do()); }
public async Task UsingDeclare() { TestDisposable disposed; using (var disposable = new TestDisposable()) { disposed = disposable; } AssertTrue(disposed.Disposed); }
public void ConstructorThrowsExceptionForInvalidAllocator() { using (TestDisposable disposable = TestDisposable.Create()) { Assert.That( () => new SharedDisposable <DisposeCallCounter>( disposable.Counter, Allocator.None), Throws.TypeOf <ArgumentException>()); } }
public void Dispose_MultipleObjectsInReverseOrder() { MultiDisposable subject = new MultiDisposable(); TestDisposable first = subject.Add(new TestDisposable()); TestDisposable middle = subject.Add(new TestDisposable()); TestDisposable last = subject.Add(new TestDisposable()); subject.Dispose(); Assert.IsTrue(first.DisposedOrderPosition > middle.DisposedOrderPosition && middle.DisposedOrderPosition > last.DisposedOrderPosition); }
public void Dispose_MultipleObjectsByConstructorInReverseOrder() { TestDisposable first = new TestDisposable(); TestDisposable middle = new TestDisposable(); TestDisposable last = new TestDisposable(); MultiDisposable subject = new MultiDisposable(first, middle, last); subject.Dispose(); Assert.IsTrue(first.DisposedOrderPosition > middle.DisposedOrderPosition && middle.DisposedOrderPosition > last.DisposedOrderPosition); }
public void ShareExtensionUsesGivenDisposable() { using (TestDisposable disposable = TestDisposable.Create()) { disposable.Counter.AssignUniqueId(); using (var shared = disposable.Counter.Share(Allocator.TempJob)) { Assert.That(shared.Value.Id, Is.EqualTo(disposable.Counter.Id)); } } }
public void RemoveAt_DisposesObject() { TestDisposable[] objects = new TestDisposable[10]; for (var i = 0; i < 10; i++) { objects[i] = new TestDisposable(); list.Add(objects[i]); } Assert.IsFalse(objects[0].IsDisposed, "Expected {0}, got {1}.", false, objects[0].IsDisposed); list.RemoveAt(0); Assert.IsTrue(objects[0].IsDisposed, "Expected {0}, got {1}.", true, objects[0].IsDisposed); }
public void It_Gets_Disposable_If_Added() { var viewModel = new ViewModelBase(); var disposable = new TestDisposable(); viewModel.AddDisposable(DefaultDisposableKey, disposable); var disposableFound = viewModel.TryGetDisposable(DefaultDisposableKey, out var storedDisposable); disposableFound.Should().BeTrue(); storedDisposable.Should().NotBeNull(); }
public void DisposeRequiresReadAccess() { using (TestDisposable disposable = TestDisposable.Create()) { using (var shared = disposable.Counter.Share(Allocator.TempJob)) { AssertRequiresReadOrWriteAccess( shared, shared.Dispose); } } }
public void IsCreatedOnlyReturnsTrueBeforeDispose() { using (TestDisposable disposable = TestDisposable.Create()) { var shared = disposable.Counter.Share(Allocator.TempJob); Assert.That(shared.IsCreated, Is.True); shared.Dispose(); Assert.That(shared.IsCreated, Is.False); } }
public void It_Adds_Disposable() { var viewModel = new ViewModelBase(); var disposable = new TestDisposable(); viewModel.AddDisposable(DefaultDisposableKey, disposable); var storedDisposable = viewModel.Disposables.SingleOrDefault(s => s.Key == DefaultDisposableKey); storedDisposable.Key.Should().NotBeNull(); storedDisposable.Value.Should().NotBeNull(); }
public void TestThatDisposeGetsCalled() { var disposable = new TestDisposable(); using ( disposable ) { Assert.IsFalse(disposable.Disposed); Assert.IsFalse(disposable.DisposeCalled); } Assert.IsTrue(disposable.Disposed); Assert.IsTrue(disposable.DisposeCalled); }
public void TestDisposeItems() { var tests = new List<Test>(); var disposable = new TestDisposable(); var test = new Test(); tests.Add(disposable); tests.Add(test); Assert.AreEqual(0, disposable.DisposeCallCount); CollectionHelper.DisposeItems(tests); Assert.AreEqual(1, disposable.DisposeCallCount); }
public void Test_Dispose() { // Arrange. var test = new TestDisposable(); // Act. test.Dispose(); // Assert. Assert.True(test.IsDisposed); Assert.True(test.OnDisposingCalled); Assert.True(test.OnDisposeCalled); }