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

            var enumerationTask = Task.Run(
                async() =>
            {
                var enumerator = stream.GetAsyncEnumerator();
                (await enumerator.MoveNextAsync()).Should().BeTrue();
                enumerator.Current.Should().Be(1);
                (await enumerator.MoveNextAsync()).Should().BeTrue();
                enumerator.Current.Should().Be(2);
                (await enumerator.MoveNextAsync()).Should().BeTrue();
                enumerator.Current.Should().Be(3);
                (await enumerator.MoveNextAsync()).Should().BeFalse();
                success = true;
            });

            await stream.PushAsync(new PushedMessage(1, 1));

            await stream.PushAsync(new PushedMessage(2, 2));

            await stream.PushAsync(new PushedMessage(3, 3));

            await stream.CompleteAsync();

            await enumerationTask;

            success.Should().BeTrue();
        }
Beispiel #2
0
        public async Task PushAsync_WhileAsyncEnumerating_BackpressureIsHandled()
        {
            var stream     = new MessageStreamEnumerable <int>();
            var enumerator = stream.GetAsyncEnumerator();

            var pushTask1 = stream.PushAsync(new PushedMessage(1, 1));
            var pushTask2 = stream.PushAsync(new PushedMessage(2, 2));
            var pushTask3 = stream.PushAsync(new PushedMessage(3, 3));

            await enumerator.MoveNextAsync();

            await Task.Delay(100);

            pushTask1.IsCompleted.Should().BeFalse();

            await enumerator.MoveNextAsync();

            await AsyncTestingUtil.WaitAsync(() => pushTask1.IsCompleted);

            pushTask1.IsCompleted.Should().BeTrue();

            await Task.Delay(100);

            pushTask2.IsCompleted.Should().BeFalse();
            pushTask3.IsCompleted.Should().BeFalse();

            await enumerator.MoveNextAsync();

            await AsyncTestingUtil.WaitAsync(() => pushTask2.IsCompleted);

            pushTask2.IsCompleted.Should().BeTrue();
            pushTask3.IsCompleted.Should().BeFalse();
        }
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);
        }
Beispiel #4
0
        public async Task CompleteAsync_WhileAsyncEnumerating_EnumerationCompleted()
        {
            var completed  = false;
            var stream     = new MessageStreamEnumerable <int>();
            var enumerator = stream.GetAsyncEnumerator();

            // The next MoveNext reaches the end of the enumerable
            Task.Run(
                async() =>
            {
                await enumerator.MoveNextAsync();
                completed = true;
            }).FireAndForget();

            completed.Should().BeFalse();

            await stream.CompleteAsync();

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

            completed.Should().BeTrue();
        }