Beispiel #1
0
        public async Task Abort_WhileEnumerating_EnumerationAborted()
        {
            var completed = false;
            var stream    = new MessageStreamEnumerable <int>();

            using var enumerator = stream.GetEnumerator();

            // ReSharper disable once AccessToDisposedClosure
            var enumerationTask = Task.Run(
                () =>
            {
                enumerator.MoveNext();
                completed = true;
            });

            completed.Should().BeFalse();

            stream.Abort();

            // Give the other thread a chance to exit the MoveNext
            await AsyncTestingUtil.WaitAsync(() => enumerationTask.IsCompleted);

            completed.Should().BeFalse();
            enumerationTask.Status.Should().Be(TaskStatus.Faulted);
            enumerationTask.Exception !.InnerExceptions.First().Should().BeAssignableTo <OperationCanceledException>();
        }
Beispiel #2
0
        public async Task Abort_WhilePushing_PushAborted()
        {
            var pushed = false;
            var stream = new MessageStreamEnumerable <int>();

            var pushTask = Task.Run(
                async() =>
            {
                await stream.PushAsync(new PushedMessage(1, 1));
                pushed = true;
            });

            pushed.Should().BeFalse();

            stream.Abort();

            // Give the other thread a chance to exit the MoveNext
            await AsyncTestingUtil.WaitAsync(() => pushTask.IsCompleted);

            pushed.Should().BeFalse();
            pushTask.Status.Should().Be(TaskStatus.Canceled);
        }
Beispiel #3
0
        public async Task Abort_WhileAsyncEnumerating_EnumerationAborted()
        {
            var completed  = false;
            var stream     = new MessageStreamEnumerable <int>();
            var enumerator = stream.GetAsyncEnumerator();

            var enumerationTask = Task.Run(
                async() =>
            {
                await enumerator.MoveNextAsync();
                completed = true;
            });

            completed.Should().BeFalse();

            stream.Abort();

            // Give the other thread a chance to exit the MoveNext
            await AsyncTestingUtil.WaitAsync(() => enumerationTask.IsCompleted);

            completed.Should().BeFalse();
            enumerationTask.Status.Should().Be(TaskStatus.Canceled);
        }