Exemple #1
0
        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);
        }
 private ValueTask <IDisposable> WithLockAsync() =>
 Disposable.CreateAsync(
     () => _asyncLock.WaitAsync(this.Token),
     () => _asyncLock.Release());