public async Task Dispose() { // Create a mutex, acquire it, and then create another task that will // attempt to acquire it as well (and will fail because the mutex has // already been acquired). Then dispose the mutex and verify that the // waiting task saw the [ObjectDisposedException]. var mutex = new AsyncMutex(); var inTask = false; var acquired = false; var disposed = false; await mutex.AcquireAsync(); var task = Task.Run( async() => { try { var acquireTask = mutex.AcquireAsync(); inTask = true; await acquireTask; acquired = true; } catch (ObjectDisposedException) { disposed = true; } }); // Wait for the task to have called [AcquireAsync()]. NeonHelper.WaitFor(() => inTask, defaultTimeout); // Dispose the mutex, wait for the task to exit and then verify // that it caught the [ObjectDisposedException]. mutex.Dispose(); task.Wait(defaultTimeout); Assert.False(acquired); Assert.True(disposed); }