Exemplo n.º 1
0
        public async Task ValueAsyncDisposalAsync()
        {
            var people = new ObservableCollection <AsyncDisposableTestPerson>
            {
                AsyncDisposableTestPerson.CreateJohn(),
                AsyncDisposableTestPerson.CreateEmily()
            };
            var disposedTcs = new TaskCompletionSource <object?>();
            AsyncDisposableTestPerson?newPerson;

            using (var expr = ActiveExpression.Create(p => p[0] + p[1], people))
            {
                newPerson = expr.Value;
                Assert.IsFalse(newPerson !.IsDisposed);
                newPerson.Disposed += (sender, e) => disposedTcs.SetResult(null);
                people[0]           = AsyncDisposableTestPerson.CreateJohn();
                await Task.WhenAny(disposedTcs.Task, Task.Delay(TimeSpan.FromSeconds(1)));

                Assert.IsTrue(newPerson.IsDisposed);
                newPerson = expr.Value;
                Assert.IsFalse(newPerson !.IsDisposed);
                disposedTcs         = new TaskCompletionSource <object?>();
                newPerson.Disposed += (sender, e) => disposedTcs.SetResult(null);
            }
            await Task.WhenAny(disposedTcs.Task, Task.Delay(TimeSpan.FromSeconds(1)));

            Assert.IsTrue(newPerson.IsDisposed);
        }
Exemplo n.º 2
0
        public async Task ValueAsyncDisposal()
        {
            var john    = AsyncDisposableTestPerson.CreateJohn();
            var emily   = AsyncDisposableTestPerson.CreateEmily();
            var options = new ActiveExpressionOptions();

            options.AddExpressionValueDisposal(() => CombineAsyncDisposablePeople(null !, null !));
            AsyncDisposableTestPerson?first, second;
            var disposedTcs = new TaskCompletionSource <object?>();

            using (var expr = ActiveExpression.Create(() => CombineAsyncDisposablePeople(john.Name !.Length > 3 ? john : emily, emily), options))
            {
                Assert.IsNull(expr.Fault);
                first = expr.Value;
                Assert.IsFalse(first !.IsDisposed);
                first.Disposed += (sender, e) => disposedTcs.SetResult(null);
                john.Name       = string.Empty;
                await Task.WhenAny(disposedTcs.Task, Task.Delay(TimeSpan.FromSeconds(1)));

                Assert.IsNull(expr.Fault);
                second = expr.Value;
                Assert.IsFalse(second !.IsDisposed);
                Assert.IsTrue(first.IsDisposed);
                disposedTcs      = new TaskCompletionSource <object?>();
                second.Disposed += (sender, e) => disposedTcs.SetResult(null);
            }
            await Task.WhenAny(disposedTcs.Task, Task.Delay(TimeSpan.FromSeconds(1)));

            Assert.IsTrue(second.IsDisposed);
        }