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

            using var enumerator = stream.GetEnumerator();

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

            enumerator.MoveNext();

            await Task.Delay(100);

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

            enumerator.MoveNext();

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

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

            await Task.Delay(100);

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

            enumerator.MoveNext();

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

            pushTask2.IsCompleted.Should().BeTrue();
            pushTask3.IsCompleted.Should().BeFalse();
        }
Beispiel #2
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 #3
0
        public async Task CompleteAsync_TryPushingAfterComplete_ExceptionThrown()
        {
            var stream = new MessageStreamEnumerable <int>();

            await stream.CompleteAsync();

            Func <Task> act = async() => await stream.PushAsync(new PushedMessage(1, 1));

            act.Should().Throw <InvalidOperationException>();
        }
Beispiel #4
0
        public void Dispose_TryPushingAfterDispose_ExceptionThrown()
        {
            var stream = new MessageStreamEnumerable <int>();

            stream.Dispose();

            Func <Task> act = async() => await stream.PushAsync(new PushedMessage(1, 1));

            act.Should().Throw <InvalidOperationException>();
        }
Beispiel #5
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);
        }