public async Task Composite_Disposable_Combines_All() { var setupBag = new ConcurrentBag <int>(); var tearDownBag = new ConcurrentBag <int>(); await using (CompositeDisposable compose = Disposable.Compose(Disposable.Create(() => tearDownBag.Add(0))) .Add(Disposable.CreateAsync(() => { tearDownBag.Add(0); return(new ValueTask(Task.CompletedTask)); })) .Add(Disposable.Undoable(() => setupBag.Add(0), () => tearDownBag.Add(0))) .Add(Disposable.UndoableAsync(() => { setupBag.Add(0); return(Task.CompletedTask); }, () => { tearDownBag.Add(0); return(new ValueTask(Task.CompletedTask)); }))) { await compose.SetupAsync(); } Assert.Equal(2 * 2, setupBag.Count); Assert.Equal(2 * 4, tearDownBag.Count); }
public async Task Composite_Disposable_Runs_All_Setups_Even_When_Ones_Faulted() { var setupBag = new ConcurrentBag <int>(); ILifetimeAsyncDisposable composite = Disposable.Compose() .AddSetup(() => setupBag.Add(0)) .AddSetupAsync(() => throw new InvalidOperationException("Test exception")) .AddSetup(() => setupBag.Add(0)); var aggregate = await Assert.ThrowsAsync <AggregateException>(() => composite.SetupAsync()); Assert.Collection( aggregate.InnerExceptions, ex => Assert.IsType <InvalidOperationException>(ex)); Assert.Equal(2, setupBag.Count); }