123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Xunit;
- namespace Tests
- {
- public class Amb : AsyncEnumerableExTests
- {
- [Fact]
- public void Amb_Null()
- {
- Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Amb(default, Return42));
- Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Amb(Return42, default));
- }
- [Fact]
- public async Task Amb_First_Wins()
- {
- var source = AsyncEnumerable.Range(1, 5).Amb(AsyncEnumerableEx.Never<int>());
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_First_Wins_Alt()
- {
- var source = AsyncEnumerable.Range(1, 5).Amb(AsyncEnumerable.Range(1, 5).SelectAwait(async v =>
- {
- await Task.Delay(500);
- return v;
- }));
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Second_Wins()
- {
- var source = AsyncEnumerableEx.Never<int>().Amb(AsyncEnumerable.Range(1, 5));
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Second_Wins_Alt()
- {
- var source = AsyncEnumerable.Range(1, 5).SelectAwait(async v =>
- {
- await Task.Delay(500);
- return v;
- }).Amb(AsyncEnumerable.Range(6, 5));
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i + 5, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Many_First_Wins()
- {
- var source = AsyncEnumerableEx.Amb(
- AsyncEnumerable.Range(1, 5),
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerableEx.Never<int>()
- );
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Many_Last_Wins()
- {
- var source = AsyncEnumerableEx.Amb(
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerable.Range(1, 5)
- );
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Many_Enum_First_Wins()
- {
- var source = AsyncEnumerableEx.Amb(new[] {
- AsyncEnumerable.Range(1, 5),
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerableEx.Never<int>()
- }.AsEnumerable()
- );
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Many_Enum_Last_Wins()
- {
- var source = AsyncEnumerableEx.Amb(new[] {
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerable.Range(1, 5)
- }.AsEnumerable()
- );
- var xs = source.GetAsyncEnumerator();
- try
- {
- for (var i = 1; i <= 5; i++)
- {
- Assert.True(await xs.MoveNextAsync());
- Assert.Equal(i, xs.Current);
- }
- Assert.False(await xs.MoveNextAsync());
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_First_GetAsyncEnumerator_Crashes()
- {
- var source = new FailingGetAsyncEnumerator<int>().Amb(AsyncEnumerableEx.Never<int>());
- var xs = source.GetAsyncEnumerator();
- try
- {
- await xs.MoveNextAsync();
- Assert.Fail("Should not have gotten here");
- }
- catch (InvalidOperationException)
- {
- // we expect this
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Second_GetAsyncEnumerator_Crashes()
- {
- var source = AsyncEnumerableEx.Never<int>().Amb(new FailingGetAsyncEnumerator<int>());
- var xs = source.GetAsyncEnumerator();
- try
- {
- await xs.MoveNextAsync();
- Assert.Fail("Should not have gotten here");
- }
- catch (InvalidOperationException)
- {
- // we expect this
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Many_First_GetAsyncEnumerator_Crashes()
- {
- var source = AsyncEnumerableEx.Amb(
- new FailingGetAsyncEnumerator<int>(),
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerableEx.Never<int>()
- );
- var xs = source.GetAsyncEnumerator();
- try
- {
- await xs.MoveNextAsync();
- Assert.Fail("Should not have gotten here");
- }
- catch (InvalidOperationException)
- {
- // we expect this
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- [Fact]
- public async Task Amb_Many_Last_GetAsyncEnumerator_Crashes()
- {
- var source = AsyncEnumerableEx.Amb(
- AsyncEnumerableEx.Never<int>(),
- AsyncEnumerableEx.Never<int>(),
- new FailingGetAsyncEnumerator<int>()
- );
- var xs = source.GetAsyncEnumerator();
- try
- {
- await xs.MoveNextAsync();
- Assert.Fail("Should not have gotten here");
- }
- catch (InvalidOperationException)
- {
- // we expect this
- }
- finally
- {
- await xs.DisposeAsync();
- }
- }
- private class FailingGetAsyncEnumerator<T> : IAsyncEnumerable<T>
- {
- public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- throw new InvalidOperationException();
- }
- }
- }
- }
|