test async enumerable
This commit is contained in:
parent
00695a8db1
commit
c502ee90f6
@ -2,4 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(TargetFramework)' != 'netstandard2.0' and !$(TargetFramework.StartsWith('netcoreapp2')) and !$(TargetFramework.StartsWith('net4'))">
|
||||
<DefineConstants>$(DefineConstants);HAS_ASYNC_ENUMERATOR</DefineConstants>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
@ -845,7 +845,7 @@ namespace System.Diagnostics
|
||||
{
|
||||
foundAttribute = true;
|
||||
foundIteratorAttribute |= asma is IteratorStateMachineAttribute
|
||||
#if NETSTANDARD2_1
|
||||
#if HAS_ASYNC_ENUMERATOR
|
||||
|| asma is AsyncIteratorStateMachineAttribute
|
||||
#endif
|
||||
;
|
||||
|
||||
68
test/Ben.Demystifier.Test/AsyncEnumerableTests.cs
Normal file
68
test/Ben.Demystifier.Test/AsyncEnumerableTests.cs
Normal file
@ -0,0 +1,68 @@
|
||||
#if HAS_ASYNC_ENUMERATOR
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Ben.Demystifier.Test
|
||||
{
|
||||
public class AsyncEnumerableTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task DemystifiesAsyncEnumerable()
|
||||
{
|
||||
Exception demystifiedException = null;
|
||||
|
||||
try
|
||||
{
|
||||
await foreach (var val in Start(CancellationToken.None))
|
||||
{
|
||||
_ = val;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
demystifiedException = ex.Demystify();
|
||||
}
|
||||
|
||||
// Assert
|
||||
var stackTrace = demystifiedException.ToString();
|
||||
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
|
||||
var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Skip(1)
|
||||
.ToArray());
|
||||
var expected = string.Join("", new[] {
|
||||
" at async IAsyncEnumerable<int> Ben.Demystifier.Test.AsyncEnumerableTests.Throw(CancellationToken cancellationToken)+MoveNext()",
|
||||
" at async IAsyncEnumerable<int> Ben.Demystifier.Test.AsyncEnumerableTests.Throw(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(short token)",
|
||||
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+MoveNext()",
|
||||
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+MoveNext()",
|
||||
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(short token)",
|
||||
" at async Task Ben.Demystifier.Test.AsyncEnumerableTests.DemystifiesAsyncEnumerable()",
|
||||
" at async Task Ben.Demystifier.Test.AsyncEnumerableTests.DemystifiesAsyncEnumerable()"
|
||||
});
|
||||
Assert.Equal(expected, trace);
|
||||
}
|
||||
|
||||
async IAsyncEnumerable<long> Start([EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
|
||||
yield return 1;
|
||||
await foreach (var @throw in Throw(cancellationToken))
|
||||
{
|
||||
yield return @throw;
|
||||
}
|
||||
}
|
||||
|
||||
async IAsyncEnumerable<int> Throw([EnumeratorCancellation] CancellationToken cancellationToken)
|
||||
{
|
||||
yield return 2;
|
||||
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Loading…
Reference in New Issue
Block a user