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(); }
public async Task CompleteAsync_WhileEnumerating_EnumerationCompleted() { var completed = false; var stream = new MessageStreamEnumerable <int>(); using var enumerator = stream.GetEnumerator(); // The next MoveNext reaches the end of the enumerable // ReSharper disable once AccessToDisposedClosure Task.Run( () => { enumerator.MoveNext(); 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(); }
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>(); }
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(); }