public async Task MultipleDispose_OnlyDisposesChildOnce() { var counter = 0; var disposable = new CollectionAsyncDisposable(new AnonymousAsyncDisposable(async() => { ++counter; })); await disposable.DisposeAsync(); await disposable.DisposeAsync(); Assert.Equal(1, counter); }
public async Task Add_AfterDisposeStarts_ExecutingConcurrently_InvokesActionImmediately() { bool action1Invoked = false; bool action2Invoked = false; var ready = new TaskCompletionSource <object>(); var signal = new TaskCompletionSource <object>(); var disposable = new CollectionAsyncDisposable(new[] { new AnonymousAsyncDisposable(async() => { ready.TrySetResult(null); await signal.Task; action1Invoked = true; }), }, AsyncDisposeFlags.ExecuteConcurrently); var task = Task.Run(async() => await disposable.DisposeAsync()); await ready.Task; await disposable.AddAsync(new AnonymousAsyncDisposable(async() => { action2Invoked = true; })); Assert.False(action1Invoked); Assert.True(action2Invoked); signal.TrySetResult(null); await task; Assert.True(action1Invoked); }
public async Task Children_ExecutingInSerial_ExecuteInSerial() { bool running = false; var disposable = new CollectionAsyncDisposable(); for (int i = 0; i != 10; ++i) { await disposable.AddAsync(new AnonymousAsyncDisposable(async() => { Assert.False(running); running = true; await Task.Delay(10); running = false; })); } await disposable.DisposeAsync(); }