Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #916 from akarnokd/TimeoutCancelLoser
IxAsync.Timeout: propagate timeout cancellation to main source
- Loading branch information
Showing
2 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/Timeout.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 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.False(true, "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.False(true, "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.False(true, "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.False(true, "MoveNextAsync should have thrown"); | ||
} | ||
catch (TimeoutException) | ||
{ | ||
// expected | ||
} | ||
finally | ||
{ | ||
await en.DisposeAsync(); | ||
} | ||
|
||
Assert.Equal(0, await tcs.Task); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters