123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // 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.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Xunit;
- namespace Tests
- {
- public class Timeout : AsyncEnumerableExTests
- {
- [Fact]
- public async Task Timeout_Never()
- {
- var source = AsyncEnumerableEx.Never<int>().Timeout(TimeSpan.FromMilliseconds(100));
- var en = source.GetAsyncEnumerator();
- try
- {
- await en.MoveNextAsync();
- Assert.Fail("MoveNextAsync should have thrown");
- }
- catch (TimeoutException)
- {
- // expected
- }
- finally
- {
- await en.DisposeAsync();
- }
- }
- [Fact]
- public async Task Timeout_Double_Never()
- {
- var source = AsyncEnumerableEx.Never<int>()
- .Timeout(TimeSpan.FromMilliseconds(300))
- .Timeout(TimeSpan.FromMilliseconds(100));
- var en = source.GetAsyncEnumerator();
- try
- {
- await en.MoveNextAsync();
- Assert.Fail("MoveNextAsync should have thrown");
- }
- catch (TimeoutException)
- {
- // expected
- }
- finally
- {
- await en.DisposeAsync();
- }
- }
- [Fact]
- public async Task Timeout_Delayed_Main()
- {
- var source = AsyncEnumerable.Range(1, 5)
- .SelectAwait(async v =>
- {
- await Task.Delay(300);
- return v;
- })
- .Timeout(TimeSpan.FromMilliseconds(100));
- var en = source.GetAsyncEnumerator();
- try
- {
- await en.MoveNextAsync();
- Assert.Fail("MoveNextAsync should have thrown");
- }
- catch (TimeoutException)
- {
- // expected
- }
- finally
- {
- await en.DisposeAsync();
- }
- }
- [Fact]
- public async Task Timeout_Delayed_Main_Canceled()
- {
- var tcs = new TaskCompletionSource<int>();
- var source = AsyncEnumerable.Range(1, 5)
- .SelectAwaitWithCancellation(async (v, ct) =>
- {
- try
- {
- await Task.Delay(500, ct);
- }
- catch (TaskCanceledException)
- {
- tcs.SetResult(0);
- }
- return v;
- })
- .Timeout(TimeSpan.FromMilliseconds(250));
- var en = source.GetAsyncEnumerator();
- try
- {
- await en.MoveNextAsync();
- Assert.Fail("MoveNextAsync should have thrown");
- }
- catch (TimeoutException)
- {
- // expected
- }
- finally
- {
- await en.DisposeAsync();
- }
- Assert.Equal(0, await tcs.Task);
- }
- }
- }
|