diff --git a/Ix.NET/Source/Ix.NET.sln.DotSettings b/Ix.NET/Source/Ix.NET.sln.DotSettings index 953521b5f6..36f7776843 100644 --- a/Ix.NET/Source/Ix.NET.sln.DotSettings +++ b/Ix.NET/Source/Ix.NET.sln.DotSettings @@ -1,5 +1,8 @@  True + SUGGESTION + SUGGESTION + SUGGESTION <?xml version="1.0" encoding="utf-16"?><Profile name="NoReordering"><RemoveCodeRedundancies>True</RemoveCodeRedundancies><CSUseAutoProperty>True</CSUseAutoProperty><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSMakeAutoPropertyGetOnly>True</CSMakeAutoPropertyGetOnly><CSArrangeQualifiers>True</CSArrangeQualifiers><CSFixBuiltinTypeReferences>True</CSFixBuiltinTypeReferences><CSEnforceVarKeywordUsageSettings>True</CSEnforceVarKeywordUsageSettings><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><CSharpFormatDocComments>True</CSharpFormatDocComments><AspOptimizeRegisterDirectives>True</AspOptimizeRegisterDirectives><HtmlReformatCode>True</HtmlReformatCode><FormatAttributeQuoteDescriptor>True</FormatAttributeQuoteDescriptor><JsInsertSemicolon>True</JsInsertSemicolon><CorrectVariableKindsDescriptor>True</CorrectVariableKindsDescriptor><VariablesToInnerScopesDescriptor>True</VariablesToInnerScopesDescriptor><StringToTemplatesDescriptor>True</StringToTemplatesDescriptor><JsReformatCode>True</JsReformatCode><JsFormatDocComments>True</JsFormatDocComments><RemoveRedundantQualifiersTs>True</RemoveRedundantQualifiersTs><OptimizeImportsTs>True</OptimizeImportsTs><OptimizeReferenceCommentsTs>True</OptimizeReferenceCommentsTs><PublicModifierStyleTs>True</PublicModifierStyleTs><TypeAnnotationStyleTs>True</TypeAnnotationStyleTs><ExplicitAnyTs>True</ExplicitAnyTs><RelativePathStyleTs>True</RelativePathStyleTs><XMLReformatCode>True</XMLReformatCode><CppReformatCode>True</CppReformatCode><CssAlphabetizeProperties>True</CssAlphabetizeProperties><CssReformatCode>True</CssReformatCode><VBOptimizeImports>True</VBOptimizeImports><VBShortenReferences>True</VBShortenReferences><VBReformatCode>True</VBReformatCode><VBFormatDocComments>True</VBFormatDocComments><CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="False" AddMissingParentheses="False" ArrangeBraces="False" ArrangeAttributes="False" ArrangeArgumentsStyle="False" /></Profile> <?xml version="1.0" encoding="utf-16"?><Profile name="Normal"><CSArrangeThisQualifier>True</CSArrangeThisQualifier><CSUpdateFileHeader>False</CSUpdateFileHeader><CSRemoveCodeRedundancies>True</CSRemoveCodeRedundancies><CSUseAutoProperty>True</CSUseAutoProperty><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSUseVar><BehavourStyle>CAN_CHANGE_TO_IMPLICIT</BehavourStyle><LocalVariableStyle>ALWAYS_IMPLICIT</LocalVariableStyle><ForeachVariableStyle>ALWAYS_IMPLICIT</ForeachVariableStyle></CSUseVar><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><CSharpFormatDocComments>True</CSharpFormatDocComments><CSReorderTypeMembers>True</CSReorderTypeMembers><CssAlphabetizeProperties>True</CssAlphabetizeProperties><CssReformatCode>True</CssReformatCode><XMLReformatCode>True</XMLReformatCode><JsReformatCode>True</JsReformatCode><JsInsertSemicolon>True</JsInsertSemicolon><VBReformatCode>True</VBReformatCode><VBShortenReferences>True</VBShortenReferences><VBOptimizeImports>True</VBOptimizeImports><HtmlReformatCode>True</HtmlReformatCode><AspOptimizeRegisterDirectives>True</AspOptimizeRegisterDirectives><RemoveCodeRedundancies>True</RemoveCodeRedundancies><CSMakeAutoPropertyGetOnly>True</CSMakeAutoPropertyGetOnly><CSArrangeQualifiers>True</CSArrangeQualifiers><CSEnforceVarKeywordUsageSettings>True</CSEnforceVarKeywordUsageSettings><CSFixBuiltinTypeReferences>True</CSFixBuiltinTypeReferences><FormatAttributeQuoteDescriptor>True</FormatAttributeQuoteDescriptor><CorrectVariableKindsDescriptor>True</CorrectVariableKindsDescriptor><VariablesToInnerScopesDescriptor>True</VariablesToInnerScopesDescriptor><StringToTemplatesDescriptor>True</StringToTemplatesDescriptor><JsFormatDocComments>True</JsFormatDocComments><RemoveRedundantQualifiersTs>True</RemoveRedundantQualifiersTs><OptimizeImportsTs>True</OptimizeImportsTs><OptimizeReferenceCommentsTs>True</OptimizeReferenceCommentsTs><PublicModifierStyleTs>True</PublicModifierStyleTs><ExplicitAnyTs>True</ExplicitAnyTs><TypeAnnotationStyleTs>True</TypeAnnotationStyleTs><RelativePathStyleTs>True</RelativePathStyleTs><CppReformatCode>True</CppReformatCode><VBFormatDocComments>True</VBFormatDocComments><CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="False" AddMissingParentheses="False" ArrangeBraces="False" ArrangeAttributes="False" ArrangeArgumentsStyle="False" /></Profile> Default: Full Cleanup diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Aggregates.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Aggregates.cs index c2d4670137..cd523ac638 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Aggregates.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Aggregates.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using System.Threading; @@ -14,64 +13,37 @@ public static partial class AsyncEnumerable public static Task Aggregate(this IAsyncEnumerable source, TAccumulate seed, Func accumulator, Func resultSelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (accumulator == null) - throw new ArgumentNullException("accumulator"); + throw new ArgumentNullException(nameof(accumulator)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); - var tcs = new TaskCompletionSource(); + return Aggregate_(source, seed, accumulator, resultSelector, cancellationToken); + } + private static async Task Aggregate_(IAsyncEnumerable source, TAccumulate seed, + Func accumulator, Func resultSelector, CancellationToken cancellationToken) + { var acc = seed; - var e = source.GetEnumerator(); - - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - try - { - acc = accumulator(acc, e.Current); - f(ct); - } - catch (Exception exception) - { - tcs.TrySetException(exception); - } - } - else - { - var result = default(TResult); - try - { - result = resultSelector(acc); - } - catch (Exception exception) - { - tcs.TrySetException(exception); - return; - } - - tcs.TrySetResult(result); - } - }); - }); - - f(cancellationToken); + acc = accumulator(acc, e.Current); + } + } - return tcs.Task.Finally(e.Dispose); + return resultSelector(acc); } public static Task Aggregate(this IAsyncEnumerable source, TAccumulate seed, Func accumulator, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (accumulator == null) - throw new ArgumentNullException("accumulator"); + throw new ArgumentNullException(nameof(accumulator)); return source.Aggregate(seed, accumulator, x => x, cancellationToken); } @@ -79,58 +51,35 @@ public static Task Aggregate(this IAsyncEnume public static Task Aggregate(this IAsyncEnumerable source, Func accumulator, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (accumulator == null) - throw new ArgumentNullException("accumulator"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(accumulator)); + return Aggregate_(source, accumulator, cancellationToken); + } + + private static async Task Aggregate_(IAsyncEnumerable source, Func accumulator, CancellationToken cancellationToken) + { var first = true; var acc = default(TSource); - var e = source.GetEnumerator(); - - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - try - { - if (first) - acc = e.Current; - else - acc = accumulator(acc, e.Current); - f(ct); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - } - - first = false; - } - else - { - if (first) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(acc); - } - }); - }); - - f(cancellationToken); - - return tcs.Task.Finally(e.Dispose); + acc = first ? e.Current : accumulator(acc, e.Current); + first = false; + } + } + if (first) + throw new InvalidOperationException(Strings.NO_ELEMENTS); + return acc; } public static Task Count(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0, (c, _) => checked(c + 1), cancellationToken); } @@ -138,9 +87,9 @@ public static Task Count(this IAsyncEnumerable source, Ca public static Task Count(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).Aggregate(0, (c, _) => checked(c + 1), cancellationToken); } @@ -148,7 +97,7 @@ public static Task Count(this IAsyncEnumerable source, Fu public static Task LongCount(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0L, (c, _) => checked(c + 1), cancellationToken); } @@ -156,9 +105,9 @@ public static Task LongCount(this IAsyncEnumerable sourc public static Task LongCount(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).Aggregate(0L, (c, _) => checked(c + 1), cancellationToken); } @@ -166,91 +115,53 @@ public static Task LongCount(this IAsyncEnumerable sourc public static Task All(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(predicate)); - var e = source.GetEnumerator(); + return All_(source, predicate, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task All_(IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - try - { - if (!predicate(e.Current)) - tcs.TrySetResult(false); - else - f(ct); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - } - } - else - { - tcs.TrySetResult(true); - } - }); - }); - - f(cancellationToken); - - return tcs.Task.Finally(e.Dispose); + if (!predicate(e.Current)) + return false; + } + } + return true; } public static Task Any(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); - var tcs = new TaskCompletionSource(); - - var e = source.GetEnumerator(); + return Any_(source, predicate, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Any_(IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - try - { - if (predicate(e.Current)) - tcs.TrySetResult(true); - else - f(ct); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - } - } - else - { - tcs.TrySetResult(false); - } - }); - }); - - f(cancellationToken); - - return tcs.Task.Finally(e.Dispose); + if (predicate(e.Current)) + return true; + } + } + return false; } public static Task Any(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); var e = source.GetEnumerator(); return e.MoveNext(cancellationToken); @@ -259,9 +170,9 @@ public static Task Any(this IAsyncEnumerable source, Can public static Task Contains(this IAsyncEnumerable source, TSource value, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.Any(x => comparer.Equals(x, value), cancellationToken); } @@ -269,7 +180,7 @@ public static Task Contains(this IAsyncEnumerable source public static Task Contains(this IAsyncEnumerable source, TSource value, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Contains(value, EqualityComparer.Default, cancellationToken); } @@ -277,32 +188,29 @@ public static Task Contains(this IAsyncEnumerable source public static Task First(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); - var tcs = new TaskCompletionSource(); - - var e = source.GetEnumerator(); + return First_(source, cancellationToken); + } - e.MoveNext(cancellationToken).Then(t => + private static async Task First_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - tcs.TrySetResult(e.Current); - else - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - }); - }); - - return tcs.Task.Finally(e.Dispose); + return e.Current; + } + } + throw new InvalidOperationException(Strings.NO_ELEMENTS); } public static Task First(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).First(cancellationToken); } @@ -310,32 +218,29 @@ public static Task First(this IAsyncEnumerable source public static Task FirstOrDefault(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return FirstOrDefault_(source, cancellationToken); + } - e.MoveNext(cancellationToken).Then(t => + private static async Task FirstOrDefault_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - tcs.TrySetResult(e.Current); - else - tcs.TrySetResult(default(TSource)); - }); - }); - - return tcs.Task.Finally(e.Dispose); + return e.Current; + } + } + return default(TSource); } public static Task FirstOrDefault(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).FirstOrDefault(cancellationToken); } @@ -343,46 +248,35 @@ public static Task FirstOrDefault(this IAsyncEnumerable Last(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); - var tcs = new TaskCompletionSource(); + return Last_(source, cancellationToken); + } - var e = source.GetEnumerator(); + private static async Task Last_(IAsyncEnumerable source, CancellationToken cancellationToken) + { var last = default(TSource); var hasLast = false; - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - hasLast = true; - last = e.Current; - f(ct); - } - else - { - if (!hasLast) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(last); - } - }); - }); - - f(cancellationToken); - - return tcs.Task.Finally(e.Dispose); + hasLast = true; + last = e.Current; + } + } + if (!hasLast) + throw new InvalidOperationException(Strings.NO_ELEMENTS); + return last; } public static Task Last(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).Last(cancellationToken); } @@ -390,46 +284,33 @@ public static Task Last(this IAsyncEnumerable source, public static Task LastOrDefault(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); - var tcs = new TaskCompletionSource(); + return LastOrDefault_(source, cancellationToken); + } - var e = source.GetEnumerator(); + private static async Task LastOrDefault_(IAsyncEnumerable source, CancellationToken cancellationToken) + { var last = default(TSource); var hasLast = false; - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - hasLast = true; - last = e.Current; - f(ct); - } - else - { - if (!hasLast) - tcs.TrySetResult(default(TSource)); - else - tcs.TrySetResult(last); - } - }); - }); - - f(cancellationToken); - - return tcs.Task.Finally(e.Dispose); + hasLast = true; + last = e.Current; + } + } + return !hasLast ? default(TSource) : last; } public static Task LastOrDefault(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).LastOrDefault(cancellationToken); } @@ -437,44 +318,34 @@ public static Task LastOrDefault(this IAsyncEnumerable Single(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return Single_(source, cancellationToken); + } - e.MoveNext(cancellationToken).Then(t => + private static async Task Single_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - var result = e.Current; - e.MoveNext(cancellationToken).Then(t1 => - { - t1.Handle(tcs, res1 => - { - if (res1) - tcs.TrySetException(new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT)); - else - tcs.TrySetResult(result); - }); - }); - } - else - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - }); - }); - - return tcs.Task.Finally(e.Dispose); + throw new InvalidOperationException(Strings.NO_ELEMENTS); + } + var result = e.Current; + if (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT); + } + return result; + } } public static Task Single(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).Single(cancellationToken); } @@ -482,44 +353,35 @@ public static Task Single(this IAsyncEnumerable sourc public static Task SingleOrDefault(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return SingleOrDefault_(source, cancellationToken); + } - e.MoveNext(cancellationToken).Then(t => + private static async Task SingleOrDefault_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - var result = e.Current; - e.MoveNext(cancellationToken).Then(t1 => - { - t1.Handle(tcs, res1 => - { - if (res1) - tcs.TrySetException(new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT)); - else - tcs.TrySetResult(result); - }); - }); - } - else - tcs.TrySetResult(default(TSource)); - }); - }); + return default(TSource); + } - return tcs.Task.Finally(e.Dispose); + var result = e.Current; + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + return result; + } + } + throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT); } public static Task SingleOrDefault(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return source.Where(predicate).SingleOrDefault(cancellationToken); } @@ -527,87 +389,69 @@ public static Task SingleOrDefault(this IAsyncEnumerable ElementAt(this IAsyncEnumerable source, int index, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (index < 0) - throw new ArgumentOutOfRangeException("index"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentOutOfRangeException(nameof(index)); - var e = source.GetEnumerator(); + return ElementAt_(source, index, cancellationToken); + } - var next = default(Action); - next = ct => e.MoveNext(ct).Then(t => + private static async Task ElementAt_(IAsyncEnumerable source, int index, CancellationToken cancellationToken) + { + if (index >= 0) { - t.Handle(tcs, res => + using (var e = source.GetEnumerator()) { - if (res) + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { if (index == 0) { - tcs.TrySetResult(e.Current); - } - else - { - index--; - next(ct); + return e.Current; } - } - else - { - tcs.TrySetException(new ArgumentOutOfRangeException("index")); - } - }); - }); - next(cancellationToken); + index--; + } + } + } - return tcs.Task.Finally(e.Dispose); + throw new ArgumentOutOfRangeException(nameof(index)); } public static Task ElementAtOrDefault(this IAsyncEnumerable source, int index, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (index < 0) - throw new ArgumentOutOfRangeException("index"); + throw new ArgumentOutOfRangeException(nameof(index)); - var tcs = new TaskCompletionSource(); - - var e = source.GetEnumerator(); + return ElementAtOrDefault_(source, index, cancellationToken); + } - var next = default(Action); - next = ct => e.MoveNext(ct).Then(t => + private static async Task ElementAtOrDefault_(IAsyncEnumerable source, int index, CancellationToken cancellationToken) + { + if (index >= 0) { - t.Handle(tcs, res => + using (var e = source.GetEnumerator()) { - if (res) + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { if (index == 0) { - tcs.TrySetResult(e.Current); - } - else - { - index--; - next(ct); + return e.Current; } - } - else - { - tcs.TrySetResult(default(TSource)); - } - }); - }); - next(cancellationToken); + index--; + } + } + } - return tcs.Task.Finally(e.Dispose); + return default(TSource); } public static Task ToArray(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(new List(), (list, x) => { list.Add(x); return list; }, list => list.ToArray(), cancellationToken); } @@ -615,7 +459,7 @@ public static Task ToArray(this IAsyncEnumerable so public static Task> ToList(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(new List(), (list, x) => { list.Add(x); return list; }, cancellationToken); } @@ -623,13 +467,13 @@ public static Task> ToList(this IAsyncEnumerable public static Task> ToDictionary(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.Aggregate(new Dictionary(comparer), (d, x) => { d.Add(keySelector(x), elementSelector(x)); return d; }, cancellationToken); } @@ -637,11 +481,11 @@ public static Task> ToDictionary> ToDictionary(this IAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); return source.ToDictionary(keySelector, elementSelector, EqualityComparer.Default, cancellationToken); } @@ -649,11 +493,11 @@ public static Task> ToDictionary> ToDictionary(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.ToDictionary(keySelector, x => x, comparer, cancellationToken); } @@ -661,35 +505,37 @@ public static Task> ToDictionary(this I public static Task> ToDictionary(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.ToDictionary(keySelector, x => x, EqualityComparer.Default, cancellationToken); } - public static Task> ToLookup(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + public static async Task> ToLookup(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); + + var lookup = await Internal.Lookup.CreateAsync(source, keySelector, elementSelector, comparer, cancellationToken).ConfigureAwait(false); - return source.Aggregate(new Lookup(comparer), (lookup, x) => { lookup.Add(keySelector(x), elementSelector(x)); return lookup; }, lookup => (ILookup)lookup, cancellationToken); + return lookup; } public static Task> ToLookup(this IAsyncEnumerable source, Func keySelector, Func elementSelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); return source.ToLookup(keySelector, elementSelector, EqualityComparer.Default, cancellationToken); } @@ -697,11 +543,11 @@ public static Task> ToLookup(th public static Task> ToLookup(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.ToLookup(keySelector, x => x, comparer, cancellationToken); } @@ -709,461 +555,370 @@ public static Task> ToLookup(this IAsyncEn public static Task> ToLookup(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.ToLookup(keySelector, x => x, EqualityComparer.Default, cancellationToken); } - class Lookup : ILookup + public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { - private readonly Dictionary> map; + if (source == null) + throw new ArgumentNullException(nameof(source)); - public Lookup(IEqualityComparer comparer) - { - map = new Dictionary>(comparer); - } + return Average_(source, cancellationToken); + } - public void Add(TKey key, TElement element) + private static async Task Average_(this IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - var g = default(EnumerableGrouping); - if (!map.TryGetValue(key, out g)) + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - g = new EnumerableGrouping(key); - map.Add(key, g); + throw new InvalidOperationException(Strings.NO_ELEMENTS); } - g.Add(element); - } - - public bool Contains(TKey key) - { - return map.ContainsKey(key); - } - - public int Count - { - get { return map.Keys.Count; } - } - - public IEnumerable this[TKey key] - { - get { return map[key]; } - } - - public IEnumerator> GetEnumerator() - { - return map.Values.Cast>().GetEnumerator(); - } - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } - - public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) - { - if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0.0; - - var e = source.GetEnumerator(); - - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => + long sum = e.Current; + long count = 1; + checked { - if (res) + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - count++; sum += e.Current; - f(ct); + ++count; } - else - { - if (count == 0) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(sum / count); - } - }); - }); - - f(cancellationToken); + } - return tcs.Task.Finally(e.Dispose); + return (double)sum / count; + } } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0.0; + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) + var v = e.Current; + if (v.HasValue) { - if (e.Current.HasValue) + long sum = v.GetValueOrDefault(); + long count = 1; + checked { - count++; - sum += e.Current.Value; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + v = e.Current; + if (v.HasValue) + { + sum += v.GetValueOrDefault(); + ++count; + } + } } - f(ct); - } - else - { - if (count == 0) - tcs.TrySetResult(null); - else - tcs.TrySetResult(sum / count); - } - }); - }); - f(cancellationToken); + return (double)sum / count; + } + } + } - return tcs.Task.Finally(e.Dispose); + return null; } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0.0; - - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) + throw new InvalidOperationException(Strings.NO_ELEMENTS); + } + + var sum = e.Current; + long count = 1; + checked + { + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - count++; sum += e.Current; - f(ct); + ++count; } - else - { - if (count == 0) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(sum / count); - } - }); - }); - - f(cancellationToken); + } - return tcs.Task.Finally(e.Dispose); + return (double)sum / count; + } } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(source)); - var count = 0L; - var sum = 0.0; - - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) + var v = e.Current; + if (v.HasValue) { - if (e.Current.HasValue) + var sum = v.GetValueOrDefault(); + long count = 1; + checked { - count++; - sum += e.Current.Value; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + v = e.Current; + if (v.HasValue) + { + sum += v.GetValueOrDefault(); + ++count; + } + } } - f(ct); - } - else - { - if (count == 0) - tcs.TrySetResult(null); - else - tcs.TrySetResult(sum / count); - } - }); - }); - f(cancellationToken); + return (double)sum / count; + } + } + } - return tcs.Task.Finally(e.Dispose); + return null; } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(source)); - var count = 0L; - var sum = 0.0; - - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - count++; - sum += e.Current; - f(ct); - } - else - { - if (count == 0) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(sum / count); - } - }); - }); + throw new InvalidOperationException(Strings.NO_ELEMENTS); + } - f(cancellationToken); + var sum = e.Current; + long count = 1; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + // There is an opportunity to short-circuit here, in that if e.Current is + // ever NaN then the result will always be NaN. Assuming that this case is + // rare enough that not checking is the better approach generally. + sum += e.Current; + ++count; + } - return tcs.Task.Finally(e.Dispose); + return sum / count; + } } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); + throw new ArgumentNullException(nameof(source)); - var count = 0L; - var sum = 0.0; - - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) + var v = e.Current; + if (v.HasValue) { - if (e.Current.HasValue) + var sum = v.GetValueOrDefault(); + long count = 1; + checked { - count++; - sum += e.Current.Value; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + v = e.Current; + if (v.HasValue) + { + sum += v.GetValueOrDefault(); + ++count; + } + } } - f(ct); - } - else - { - if (count == 0) - tcs.TrySetResult(null); - else - tcs.TrySetResult(sum / count); - } - }); - }); - f(cancellationToken); + return sum / count; + } + } + } - return tcs.Task.Finally(e.Dispose); + return null; } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0f; + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - count++; - sum += e.Current; - f(ct); - } - else - { - if (count == 0) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(sum / count); - } - }); - }); + throw new InvalidOperationException(Strings.NO_ELEMENTS); + } - f(cancellationToken); + double sum = e.Current; + long count = 1; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + sum += e.Current; + ++count; + } - return tcs.Task.Finally(e.Dispose); + return (float)(sum / count); + } } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0f; + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) + var v = e.Current; + if (v.HasValue) { - if (e.Current.HasValue) + double sum = v.GetValueOrDefault(); + long count = 1; + checked { - count++; - sum += e.Current.Value; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + v = e.Current; + if (v.HasValue) + { + sum += v.GetValueOrDefault(); + ++count; + } + } } - f(ct); - } - else - { - if (count == 0) - tcs.TrySetResult(null); - else - tcs.TrySetResult(sum / count); - } - }); - }); - f(cancellationToken); + return (float)(sum / count); + } + } + } - return tcs.Task.Finally(e.Dispose); + return null; } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0m; + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) - { - count++; - sum += e.Current; - f(ct); - } - else - { - if (count == 0) - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - else - tcs.TrySetResult(sum / count); - } - }); - }); + throw new InvalidOperationException(Strings.NO_ELEMENTS); + } - f(cancellationToken); + var sum = e.Current; + long count = 1; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + sum += e.Current; + ++count; + } - return tcs.Task.Finally(e.Dispose); + return sum / count; + } } public static Task Average(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); - - var tcs = new TaskCompletionSource(); - - var count = 0L; - var sum = 0m; + throw new ArgumentNullException(nameof(source)); - var e = source.GetEnumerator(); + return Average_(source, cancellationToken); + } - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + private static async Task Average_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (res) + var v = e.Current; + if (v.HasValue) { - if (e.Current.HasValue) + var sum = v.GetValueOrDefault(); + long count = 1; + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - count++; - sum += e.Current.Value; + v = e.Current; + if (v.HasValue) + { + sum += v.GetValueOrDefault(); + ++count; + } } - f(ct); - } - else - { - if (count == 0) - tcs.TrySetResult(null); - else - tcs.TrySetResult(sum / count); - } - }); - }); - f(cancellationToken); + return sum / count; + } + } + } - return tcs.Task.Finally(e.Dispose); + return null; } public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1171,9 +926,9 @@ public static Task Average(this IAsyncEnumerable source, Cance public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1181,9 +936,9 @@ public static Task Average(this IAsyncEnumerable sourc public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1191,9 +946,9 @@ public static Task Average(this IAsyncEnumerable sourc public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1201,9 +956,9 @@ public static Task Average(this IAsyncEnumerable sourc public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1211,9 +966,9 @@ public static Task Average(this IAsyncEnumerable sourc public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1221,9 +976,9 @@ public static Task Average(this IAsyncEnumerable sourc public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1231,9 +986,9 @@ public static Task Average(this IAsyncEnumerable source public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1241,9 +996,9 @@ public static Task Average(this IAsyncEnumerable source public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1251,9 +1006,9 @@ public static Task Average(this IAsyncEnumerable sour public static Task Average(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Average(cancellationToken); } @@ -1261,7 +1016,7 @@ public static Task Average(this IAsyncEnumerable sour public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Max, cancellationToken); } @@ -1269,7 +1024,7 @@ public static Task Max(this IAsyncEnumerable source, CancellationToken public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Max, cancellationToken); } @@ -1277,7 +1032,7 @@ public static Task Max(this IAsyncEnumerable source, CancellationTok public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Max, cancellationToken); } @@ -1285,7 +1040,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellatio public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Max, cancellationToken); } @@ -1293,7 +1048,7 @@ public static Task Max(this IAsyncEnumerable source, CancellationT public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Max, cancellationToken); } @@ -1313,7 +1068,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellat public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(int?), NullableMax, cancellationToken); } @@ -1321,7 +1076,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellat public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(long?), NullableMax, cancellationToken); } @@ -1329,7 +1084,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellat public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(double?), NullableMax, cancellationToken); } @@ -1337,7 +1092,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellat public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(float?), NullableMax, cancellationToken); } @@ -1345,7 +1100,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellat public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(decimal?), NullableMax, cancellationToken); } @@ -1353,7 +1108,7 @@ public static Task Max(this IAsyncEnumerable source, Cancellat public static Task Max(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); var comparer = Comparer.Default; return source.Aggregate((x, y) => comparer.Compare(x, y) >= 0 ? x : y, cancellationToken); @@ -1362,9 +1117,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1372,9 +1127,9 @@ public static Task Max(this IAsyncEnumerable source, Func public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1382,9 +1137,9 @@ public static Task Max(this IAsyncEnumerable source, Fun public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1392,9 +1147,9 @@ public static Task Max(this IAsyncEnumerable source, F public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1402,9 +1157,9 @@ public static Task Max(this IAsyncEnumerable source, Fu public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1412,9 +1167,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1422,9 +1177,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1432,9 +1187,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1442,9 +1197,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1452,9 +1207,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1462,9 +1217,9 @@ public static Task Max(this IAsyncEnumerable source, public static Task Max(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Max(cancellationToken); } @@ -1472,7 +1227,7 @@ public static Task Max(this IAsyncEnumerable public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Min, cancellationToken); } @@ -1480,7 +1235,7 @@ public static Task Min(this IAsyncEnumerable source, CancellationToken public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Min, cancellationToken); } @@ -1488,7 +1243,7 @@ public static Task Min(this IAsyncEnumerable source, CancellationTok public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Min, cancellationToken); } @@ -1496,7 +1251,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellatio public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Min, cancellationToken); } @@ -1504,7 +1259,7 @@ public static Task Min(this IAsyncEnumerable source, CancellationT public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(Math.Min, cancellationToken); } @@ -1524,7 +1279,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellat public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(int?), NullableMin, cancellationToken); } @@ -1532,7 +1287,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellat public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(long?), NullableMin, cancellationToken); } @@ -1540,7 +1295,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellat public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(double?), NullableMin, cancellationToken); } @@ -1548,7 +1303,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellat public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(float?), NullableMin, cancellationToken); } @@ -1556,7 +1311,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellat public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(default(decimal?), NullableMin, cancellationToken); } @@ -1564,7 +1319,7 @@ public static Task Min(this IAsyncEnumerable source, Cancellat public static Task Min(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); var comparer = Comparer.Default; return source.Aggregate((x, y) => comparer.Compare(x, y) <= 0 ? x : y, cancellationToken); @@ -1573,9 +1328,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1583,9 +1338,9 @@ public static Task Min(this IAsyncEnumerable source, Func public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1593,9 +1348,9 @@ public static Task Min(this IAsyncEnumerable source, Fun public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1603,9 +1358,9 @@ public static Task Min(this IAsyncEnumerable source, F public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1613,9 +1368,9 @@ public static Task Min(this IAsyncEnumerable source, Fu public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1623,9 +1378,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1633,9 +1388,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1643,9 +1398,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1653,9 +1408,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1663,9 +1418,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1673,9 +1428,9 @@ public static Task Min(this IAsyncEnumerable source, public static Task Min(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Min(cancellationToken); } @@ -1683,7 +1438,7 @@ public static Task Min(this IAsyncEnumerable public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0, (x, y) => x + y, cancellationToken); } @@ -1691,7 +1446,7 @@ public static Task Sum(this IAsyncEnumerable source, CancellationToken public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0L, (x, y) => x + y, cancellationToken); } @@ -1699,7 +1454,7 @@ public static Task Sum(this IAsyncEnumerable source, CancellationTok public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0.0, (x, y) => x + y, cancellationToken); } @@ -1707,7 +1462,7 @@ public static Task Sum(this IAsyncEnumerable source, Cancellatio public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0f, (x, y) => x + y, cancellationToken); } @@ -1715,7 +1470,7 @@ public static Task Sum(this IAsyncEnumerable source, CancellationT public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate(0m, (x, y) => x + y, cancellationToken); } @@ -1723,7 +1478,7 @@ public static Task Sum(this IAsyncEnumerable source, Cancellat public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate((int?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken); } @@ -1731,7 +1486,7 @@ public static Task Sum(this IAsyncEnumerable source, Cancellat public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate((long?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken); } @@ -1739,7 +1494,7 @@ public static Task Sum(this IAsyncEnumerable source, Cancellat public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate((double?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken); } @@ -1747,7 +1502,7 @@ public static Task Sum(this IAsyncEnumerable source, Cancellat public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate((float?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken); } @@ -1755,7 +1510,7 @@ public static Task Sum(this IAsyncEnumerable source, Cancellat public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Aggregate((decimal?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken); } @@ -1763,9 +1518,9 @@ public static Task Sum(this IAsyncEnumerable source, Cancellat public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1773,9 +1528,9 @@ public static Task Sum(this IAsyncEnumerable source, Func public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1783,9 +1538,9 @@ public static Task Sum(this IAsyncEnumerable source, Fun public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1793,9 +1548,9 @@ public static Task Sum(this IAsyncEnumerable source, F public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1803,9 +1558,9 @@ public static Task Sum(this IAsyncEnumerable source, Fu public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1813,9 +1568,9 @@ public static Task Sum(this IAsyncEnumerable source, public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1823,9 +1578,9 @@ public static Task Sum(this IAsyncEnumerable source, public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1833,9 +1588,9 @@ public static Task Sum(this IAsyncEnumerable source, public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1843,9 +1598,9 @@ public static Task Sum(this IAsyncEnumerable source, public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1853,9 +1608,9 @@ public static Task Sum(this IAsyncEnumerable source, public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return source.Select(selector).Sum(cancellationToken); } @@ -1863,27 +1618,37 @@ public static Task Sum(this IAsyncEnumerable source, public static Task IsEmpty(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); - return source.Any(cancellationToken).Then(t => !t.Result); + return IsEmpty_(source, cancellationToken); + } + + private static async Task IsEmpty_(IAsyncEnumerable source, CancellationToken cancellationToken) + { + return !await source.Any(cancellationToken).ConfigureAwait(false); } public static Task Min(this IAsyncEnumerable source, IComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); - return MinBy(source, x => x, comparer, cancellationToken).Then(t => t.Result.First()); + return Min_(source, comparer, cancellationToken); + } + + private static async Task Min_(IAsyncEnumerable source, IComparer comparer, CancellationToken cancellationToken) + { + return (await MinBy(source, x => x, comparer, cancellationToken).ConfigureAwait(false)).First(); } public static Task> MinBy(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return MinBy(source, keySelector, Comparer.Default, cancellationToken); } @@ -1891,11 +1656,11 @@ public static Task> MinBy(this IAsyncEnumerable> MinBy(this IAsyncEnumerable source, Func keySelector, IComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken); } @@ -1903,19 +1668,24 @@ public static Task> MinBy(this IAsyncEnumerable Max(this IAsyncEnumerable source, IComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); + + return Max_(source, comparer, cancellationToken); + } - return MaxBy(source, x => x, comparer, cancellationToken).Then(t => t.Result.First()); + private static async Task Max_(IAsyncEnumerable source, IComparer comparer, CancellationToken cancellationToken) + { + return (await MaxBy(source, x => x, comparer, cancellationToken).ConfigureAwait(false)).First(); } public static Task> MaxBy(this IAsyncEnumerable source, Func keySelector, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return MaxBy(source, keySelector, Comparer.Default, cancellationToken); } @@ -1923,99 +1693,47 @@ public static Task> MaxBy(this IAsyncEnumerable> MaxBy(this IAsyncEnumerable source, Func keySelector, IComparer comparer, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken); } - private static Task> ExtremaBy(IAsyncEnumerable source, Func keySelector, Func compare, CancellationToken cancellationToken) + private static async Task> ExtremaBy(IAsyncEnumerable source, Func keySelector, Func compare, CancellationToken cancellationToken) { - var tcs = new TaskCompletionSource>(); - - var result = new List(); - - var hasFirst = false; - var current = default(TSource); - var resKey = default(TKey); - - var e = source.GetEnumerator(); + var result = new List(); - var f = default(Action); - f = ct => e.MoveNext(ct).Then(t => + using (var e = source.GetEnumerator()) { - t.Handle(tcs, res => - { - if (!hasFirst) - { - if (!res) - { - tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS)); - return; - } - - current = e.Current; + if (!await e.MoveNext(cancellationToken).ConfigureAwait(false)) + throw new InvalidOperationException(Strings.NO_ELEMENTS); - try - { - resKey = keySelector(current); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + var current = e.Current; + var resKey = keySelector(current); + result.Add(current); - result.Add(current); + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) + { + var cur = e.Current; + var key = keySelector(cur); - hasFirst = true; - f(ct); + var cmp = compare(key, resKey); + if (cmp == 0) + { + result.Add(cur); } - else + else if (cmp > 0) { - if (res) - { - var key = default(TKey); - var cmp = default(int); - - try - { - current = e.Current; - key = keySelector(current); - cmp = compare(key, resKey); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (cmp == 0) - { - result.Add(current); - } - else if (cmp > 0) - { - result = new List { current }; - resKey = key; - } - - f(ct); - } - else - { - tcs.TrySetResult(result); - } + result = new List { cur }; + resKey = key; } - }); - }); - - f(cancellationToken); + } + } - return tcs.Task.Finally(e.Dispose); + return result; } } } diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Conversions.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Conversions.cs index bdc5579620..8a9096801b 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Conversions.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Conversions.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using System.Threading; @@ -14,16 +13,16 @@ public static partial class AsyncEnumerable public static IAsyncEnumerable ToAsyncEnumerable(this IEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return Create(() => { var e = source.GetEnumerator(); return Create( - ct => Task.Factory.StartNew(() => + ct => Task.Run(() => { - var res = default(bool); + var res = false; try { res = e.MoveNext(); @@ -44,7 +43,7 @@ public static IAsyncEnumerable ToAsyncEnumerable(this IEnumera public static IEnumerable ToEnumerable(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return ToEnumerable_(source); } @@ -55,9 +54,7 @@ private static IEnumerable ToEnumerable_(IAsyncEnumerable ToEnumerable_(IAsyncEnumerable ToAsyncEnumerable(this Task task) { if (task == null) - throw new ArgumentNullException("task"); - + throw new ArgumentNullException(nameof(task)); + return Create(() => { var called = 0; + var value = default(TSource); return Create( - (ct, tcs) => + async ct => { if (Interlocked.CompareExchange(ref called, 1, 0) == 0) { - task.Then(continuedTask => - { - if (continuedTask.IsCanceled) - tcs.SetCanceled(); - else if (continuedTask.IsFaulted) - tcs.SetException(continuedTask.Exception.InnerException); - else - tcs.SetResult(true); - }); + value = await task.ConfigureAwait(false); + return true; } - else - tcs.SetResult(false); - - return tcs.Task; + return false; }, - () => task.Result, + () => value, () => { }); }); } -#if !NO_RXINTERFACES public static IAsyncEnumerable ToAsyncEnumerable(this IObservable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return Create(() => { @@ -253,7 +240,7 @@ public void OnNext(T value) public static IObservable ToObservable(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return new ToObservableObservable(source); } @@ -304,6 +291,5 @@ public IDisposable Subscribe(IObserver observer) return Disposable.Create(ctd, e); } } -#endif } } diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Creation.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Creation.cs index 0ec6c9e4b0..98f6363403 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Creation.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Creation.cs @@ -11,14 +11,14 @@ namespace System.Linq { public static partial class AsyncEnumerable { - static IAsyncEnumerable Create(Func> getEnumerator) + private static IAsyncEnumerable Create(Func> getEnumerator) { return new AnonymousAsyncEnumerable(getEnumerator); } - class AnonymousAsyncEnumerable : IAsyncEnumerable + private class AnonymousAsyncEnumerable : IAsyncEnumerable { - Func> getEnumerator; + private Func> getEnumerator; public AnonymousAsyncEnumerable(Func> getEnumerator) { @@ -31,16 +31,41 @@ public IAsyncEnumerator GetEnumerator() } } - static IAsyncEnumerator Create(Func> moveNext, Func current, Action dispose) + private static IAsyncEnumerator Create(Func> moveNext, Func current, + Action dispose, IDisposable enumerator) + { + return Create(async ct => + { + using (ct.Register(dispose)) + { + try + { + var result = await moveNext(ct).ConfigureAwait(false); + if (!result) + { + enumerator?.Dispose(); + } + return result; + } + catch + { + enumerator?.Dispose(); + throw; + } + } + }, current, dispose); + } + + private static IAsyncEnumerator Create(Func> moveNext, Func current, Action dispose) { return new AnonymousAsyncEnumerator(moveNext, current, dispose); } - static IAsyncEnumerator Create(Func, Task> moveNext, Func current, Action dispose) + private static IAsyncEnumerator Create(Func, Task> moveNext, Func current, Action dispose) { var self = default(IAsyncEnumerator); self = new AnonymousAsyncEnumerator( - ct => + async ct => { var tcs = new TaskCompletionSource(); @@ -50,10 +75,10 @@ static IAsyncEnumerator Create(Func Create(Func : IAsyncEnumerator + private class AnonymousAsyncEnumerator : IAsyncEnumerator { private readonly Func> _moveNext; private readonly Func _current; @@ -109,7 +134,7 @@ public static IAsyncEnumerable Return(TValue value) public static IAsyncEnumerable Throw(Exception exception) { if (exception == null) - throw new ArgumentNullException("exception"); + throw new ArgumentNullException(nameof(exception)); return Create(() => Create( ct => TaskExt.Throw(exception), @@ -139,7 +164,7 @@ public static IAsyncEnumerable Empty() public static IAsyncEnumerable Range(int start, int count) { if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Enumerable.Range(start, count).ToAsyncEnumerable(); } @@ -147,7 +172,7 @@ public static IAsyncEnumerable Range(int start, int count) public static IAsyncEnumerable Repeat(TResult element, int count) { if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Enumerable.Repeat(element, count).ToAsyncEnumerable(); } @@ -167,7 +192,7 @@ public static IAsyncEnumerable Repeat(TResult element) public static IAsyncEnumerable Defer(Func> factory) { if (factory == null) - throw new ArgumentNullException("factory"); + throw new ArgumentNullException(nameof(factory)); return Create(() => factory().GetEnumerator()); } @@ -175,11 +200,11 @@ public static IAsyncEnumerable Defer(Func Generate(TState initialState, Func condition, Func iterate, Func resultSelector) { if (condition == null) - throw new ArgumentNullException("condition"); + throw new ArgumentNullException(nameof(condition)); if (iterate == null) - throw new ArgumentNullException("iterate"); + throw new ArgumentNullException(nameof(iterate)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return Create(() => { @@ -223,9 +248,9 @@ public static IAsyncEnumerable Generate(TState initial public static IAsyncEnumerable Using(Func resourceFactory, Func> enumerableFactory) where TResource : IDisposable { if (resourceFactory == null) - throw new ArgumentNullException("resourceFactory"); + throw new ArgumentNullException(nameof(resourceFactory)); if (enumerableFactory == null) - throw new ArgumentNullException("enumerableFactory"); + throw new ArgumentNullException(nameof(enumerableFactory)); return Create(() => { @@ -248,36 +273,29 @@ public static IAsyncEnumerable Using(Func + async ct => { - e.MoveNext(cts.Token).Then(t => + bool res; + try { - t.Handle(tcs, - res => - { - if (res) - { - current = e.Current; - tcs.TrySetResult(true); - } - else - { - d.Dispose(); - tcs.TrySetResult(false); - } - }, - ex => - { - d.Dispose(); - tcs.TrySetException(ex); - } - ); - }); - - return tcs.Task; + res = await e.MoveNext(cts.Token).ConfigureAwait(false); + } + catch (Exception) + { + d.Dispose(); + throw; + } + if (res) + { + current = e.Current; + return true; + } + d.Dispose(); + return false; }, () => current, - d.Dispose + d.Dispose, + null ); }); } diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Exceptions.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Exceptions.cs index 8c344dca9d..f1abe9cc12 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Exceptions.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Exceptions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; @@ -15,9 +16,9 @@ public static IAsyncEnumerable Catch(this IAsyncEn where TException : Exception { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (handler == null) - throw new ArgumentNullException("handler"); + throw new ArgumentNullException(nameof(handler)); return Create(() => { @@ -28,74 +29,32 @@ public static IAsyncEnumerable Catch(this IAsyncEn var d = Disposable.Create(cts, a); var done = false; - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (!done) { - e.MoveNext(ct).Then(t => + try { - t.Handle(tcs, - res => - { - tcs.TrySetResult(res); - }, - ex => - { - var err = default(IAsyncEnumerator); - - try - { - ex.Flatten().Handle(ex_ => - { - var exx = ex_ as TException; - if (exx != null) - { - err = handler(exx).GetEnumerator(); - return true; - } - - return false; - }); - } - catch (Exception ex2) - { - tcs.TrySetException(ex2); - return; - } - - if (err != null) - { - e = err; - a.Disposable = e; - - done = true; - f(tcs, ct); - } - } - ); - }); - } - else - { - e.MoveNext(ct).Then(t => + return await e.MoveNext(ct).ConfigureAwait(false); + } + catch (TException ex) { - t.Handle(tcs, res => - { - tcs.TrySetResult(res); - }); - }); + var err = handler(ex).GetEnumerator(); + e = err; + a.Disposable = e; + done = true; + return await f(ct).ConfigureAwait(false); + } } + return await e.MoveNext(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(a); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + a ); }); } @@ -103,7 +62,7 @@ public static IAsyncEnumerable Catch(this IAsyncEn public static IAsyncEnumerable Catch(this IEnumerable> sources) { if (sources == null) - throw new ArgumentNullException("sources"); + throw new ArgumentNullException(nameof(sources)); return sources.Catch_(); } @@ -111,7 +70,7 @@ public static IAsyncEnumerable Catch(this IEnumerable Catch(params IAsyncEnumerable[] sources) { if (sources == null) - throw new ArgumentNullException("sources"); + throw new ArgumentNullException(nameof(sources)); return sources.Catch_(); } @@ -119,9 +78,9 @@ public static IAsyncEnumerable Catch(params IAsyncEnumerable Catch(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return new[] { first, second }.Catch_(); } @@ -137,36 +96,21 @@ private static IAsyncEnumerable Catch_(this IEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (e == null) { - var b = false; - try + if (se.MoveNext()) { - b = se.MoveNext(); - if (b) - e = se.Current.GetEnumerator(); + e = se.Current.GetEnumerator(); } - catch (Exception ex) + else { - tcs.TrySetException(ex); - return; - } - - if (!b) - { - if (error != null) - { - tcs.TrySetException(error); - return; - } - - tcs.TrySetResult(false); - return; + error?.Throw(); + return false; } error = null; @@ -174,33 +118,24 @@ private static IAsyncEnumerable Catch_(this IEnumerable + try + { + return await e.MoveNext(ct).ConfigureAwait(false); + } + catch (Exception exception) { - t.Handle(tcs, - res => - { - tcs.TrySetResult(res); - }, - ex => - { - e.Dispose(); - e = null; - - error = ex; - f(tcs, ct); - } - ); - }); + e.Dispose(); + e = null; + error = ExceptionDispatchInfo.Capture(exception); + return await f(ct).ConfigureAwait(false); + } }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(a); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + a ); }); } @@ -208,9 +143,9 @@ private static IAsyncEnumerable Catch_(this IEnumerable Finally(this IAsyncEnumerable source, Action finallyAction) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (finallyAction == null) - throw new ArgumentNullException("finallyAction"); + throw new ArgumentNullException(nameof(finallyAction)); return Create(() => { @@ -220,26 +155,11 @@ public static IAsyncEnumerable Finally(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => - { - e.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => - { - tcs.TrySetResult(res); - }); - }); - }; - return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(r); - }, + ct => e.MoveNext(ct), () => e.Current, - d.Dispose + d.Dispose, + r ); }); } @@ -247,9 +167,9 @@ public static IAsyncEnumerable Finally(this IAsyncEnumerable OnErrorResumeNext(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return OnErrorResumeNext_(new[] { first, second }); } @@ -257,7 +177,7 @@ public static IAsyncEnumerable OnErrorResumeNext(this IAsyncEn public static IAsyncEnumerable OnErrorResumeNext(params IAsyncEnumerable[] sources) { if (sources == null) - throw new ArgumentNullException("sources"); + throw new ArgumentNullException(nameof(sources)); return OnErrorResumeNext_(sources); } @@ -265,7 +185,7 @@ public static IAsyncEnumerable OnErrorResumeNext(params IAsync public static IAsyncEnumerable OnErrorResumeNext(this IEnumerable> sources) { if (sources == null) - throw new ArgumentNullException("sources"); + throw new ArgumentNullException(nameof(sources)); return OnErrorResumeNext_(sources); } @@ -281,69 +201,45 @@ private static IAsyncEnumerable OnErrorResumeNext_(IEnumerable var a = new AssignableDisposable(); var d = Disposable.Create(cts, se, a); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (e == null) { var b = false; - try + b = se.MoveNext(); + if (b) + e = se.Current.GetEnumerator(); + else { - b = se.MoveNext(); - if (b) - e = se.Current.GetEnumerator(); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (!b) - { - tcs.TrySetResult(false); - return; + return false; } a.Disposable = e; } - e.MoveNext(ct).Then(t => + try + { + if (await e.MoveNext(ct).ConfigureAwait(false)) + { + return true; + } + } + catch { - t.Handle(tcs, - res => - { - if (res) - { - tcs.TrySetResult(true); - } - else - { - e.Dispose(); - e = null; - - f(tcs, ct); - } - }, - ex => - { - e.Dispose(); - e = null; - - f(tcs, ct); - } - ); - }); + // ignore + } + + e.Dispose(); + e = null; + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(a); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + a ); }); } @@ -351,7 +247,7 @@ private static IAsyncEnumerable OnErrorResumeNext_(IEnumerable public static IAsyncEnumerable Retry(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return new[] { source }.Repeat().Catch(); } @@ -359,9 +255,9 @@ public static IAsyncEnumerable Retry(this IAsyncEnumerable Retry(this IAsyncEnumerable source, int retryCount) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (retryCount < 0) - throw new ArgumentOutOfRangeException("retryCount"); + throw new ArgumentOutOfRangeException(nameof(retryCount)); return new[] { source }.Repeat(retryCount).Catch(); } diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Multiple.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Multiple.cs index c0549e4041..2f58588e8e 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Multiple.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Multiple.cs @@ -14,9 +14,9 @@ public static partial class AsyncEnumerable public static IAsyncEnumerable Concat(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return Create(() => { @@ -27,42 +27,30 @@ public static IAsyncEnumerable Concat(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => e.MoveNext(ct).Then(t => + var f = default(Func>); + f = async ct => { - t.Handle(tcs, res => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - if (res) - { - tcs.TrySetResult(true); - } - else - { - if (switched) - { - tcs.TrySetResult(false); - } - else - { - switched = true; + return true; + } + if (switched) + { + return false; + } + switched = true; - e = second.GetEnumerator(); - a.Disposable = e; + e = second.GetEnumerator(); + a.Disposable = e; - f(tcs, ct); - } - } - }); - }); + return await f(ct).ConfigureAwait(false); + }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(a); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -70,11 +58,11 @@ public static IAsyncEnumerable Concat(this IAsyncEnumerable Zip(this IAsyncEnumerable first, IAsyncEnumerable second, Func selector) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return Create(() => { @@ -86,21 +74,13 @@ public static IAsyncEnumerable Zip(this IAsyn var d = Disposable.Create(cts, e1, e2); return Create( - (ct, tcs) => - { - e1.MoveNext(cts.Token).Zip(e2.MoveNext(cts.Token), (f, s) => + ct => e1.MoveNext(cts.Token).Zip(e2.MoveNext(cts.Token), (f, s) => { var result = f && s; if (result) current = selector(e1.Current, e2.Current); return result; - }).Then(t => - { - t.Handle(tcs, x => tcs.TrySetResult(x)); - }); - - return tcs.Task.UsingEnumerator(e1).UsingEnumerator(e2); - }, + }), () => current, d.Dispose ); @@ -110,11 +90,11 @@ public static IAsyncEnumerable Zip(this IAsyn public static IAsyncEnumerable Except(this IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return Create(() => { @@ -125,40 +105,25 @@ public static IAsyncEnumerable Except(this IAsyncEnumerable>); var getMapTask = new Func>>(ct => - { - if (mapTask == null) - mapTask = second.ToDictionary(x => x, comparer, ct); - return mapTask; - }); + mapTask ?? (mapTask = second.ToDictionary(x => x, comparer, ct))); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Zip(getMapTask(ct), (b, _) => b).Then(t => + if (await e.MoveNext(ct).Zip(getMapTask(ct), (b, _) => b).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - if (!mapTask.Result.ContainsKey(e.Current)) - tcs.TrySetResult(true); - else - f(tcs, ct); - } - else - tcs.TrySetResult(false); - }); - }); + if (!mapTask.Result.ContainsKey(e.Current)) + return true; + return await f(ct).ConfigureAwait(false); + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -166,9 +131,9 @@ public static IAsyncEnumerable Except(this IAsyncEnumerable Except(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return first.Except(second, EqualityComparer.Default); } @@ -176,11 +141,11 @@ public static IAsyncEnumerable Except(this IAsyncEnumerable Intersect(this IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return Create(() => { @@ -197,34 +162,23 @@ public static IAsyncEnumerable Intersect(this IAsyncEnumerable return mapTask; }); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Zip(getMapTask(ct), (b, _) => b).Then(t => + if (await e.MoveNext(ct).Zip(getMapTask(ct), (b, _) => b).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - if (mapTask.Result.ContainsKey(e.Current)) - tcs.TrySetResult(true); - else - f(tcs, ct); - } - else - tcs.TrySetResult(false); - }); - }); + if (mapTask.Result.ContainsKey(e.Current)) + return true; + return await f(ct).ConfigureAwait(false); + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -232,9 +186,9 @@ public static IAsyncEnumerable Intersect(this IAsyncEnumerable public static IAsyncEnumerable Intersect(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return first.Intersect(second, EqualityComparer.Default); } @@ -242,11 +196,11 @@ public static IAsyncEnumerable Intersect(this IAsyncEnumerable public static IAsyncEnumerable Union(this IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return first.Concat(second).Distinct(comparer); } @@ -254,9 +208,9 @@ public static IAsyncEnumerable Union(this IAsyncEnumerable Union(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return first.Union(second, EqualityComparer.Default); } @@ -264,79 +218,39 @@ public static IAsyncEnumerable Union(this IAsyncEnumerable SequenceEqual(this IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); - var tcs = new TaskCompletionSource(); - - var e1 = first.GetEnumerator(); - var e2 = second.GetEnumerator(); + return SequenceEqual_(first, second, comparer, cancellationToken); + } - var run = default(Action); - run = ct => + private static async Task SequenceEqual_(IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer, + CancellationToken cancellationToken) + { + using (var e1 = first.GetEnumerator()) + using (var e2 = second.GetEnumerator()) { - e1.MoveNext(ct).Zip(e2.MoveNext(ct), (f, s) => + while (await e1.MoveNext(cancellationToken).ConfigureAwait(false)) { - if (f ^ s) - { - tcs.TrySetResult(false); - return false; - } - - if (f && s) - { - var eq = default(bool); - try - { - eq = comparer.Equals(e1.Current, e2.Current); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return false; - } - - if (!eq) - { - tcs.TrySetResult(false); - return false; - } - else - return true; - } - else + if (!(await e2.MoveNext(cancellationToken).ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current))) { - tcs.TrySetResult(true); return false; } - }).Then(t => - { - t.Handle(tcs, res => - { - if (res) - run(ct); - }); - }); - }; - - run(cancellationToken); + } - return tcs.Task.Finally(() => - { - e1.Dispose(); - e2.Dispose(); - }); + return !await e2.MoveNext(cancellationToken).ConfigureAwait(false); + } } public static Task SequenceEqual(this IAsyncEnumerable first, IAsyncEnumerable second, CancellationToken cancellationToken) { if (first == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException(nameof(first)); if (second == null) - throw new ArgumentNullException("second"); + throw new ArgumentNullException(nameof(second)); return first.SequenceEqual(second, EqualityComparer.Default, cancellationToken); } @@ -344,131 +258,181 @@ public static Task SequenceEqual(this IAsyncEnumerable f public static IAsyncEnumerable GroupJoin(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer comparer) { if (outer == null) - throw new ArgumentNullException("outer"); + throw new ArgumentNullException(nameof(outer)); if (inner == null) - throw new ArgumentNullException("inner"); + throw new ArgumentNullException(nameof(inner)); if (outerKeySelector == null) - throw new ArgumentNullException("outerKeySelector"); + throw new ArgumentNullException(nameof(outerKeySelector)); if (innerKeySelector == null) - throw new ArgumentNullException("innerKeySelector"); + throw new ArgumentNullException(nameof(innerKeySelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); - - return Create(() => - { - var innerMap = default(Task>); - var getInnerMap = new Func>>(ct => - { - if (innerMap == null) - innerMap = inner.ToLookup(innerKeySelector, comparer, ct); - - return innerMap; - }); - - var outerE = outer.GetEnumerator(); - var current = default(TResult); - - var cts = new CancellationTokenDisposable(); - var d = Disposable.Create(cts, outerE); + throw new ArgumentNullException(nameof(comparer)); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => - { - getInnerMap(ct).Then(ti => - { - ti.Handle(tcs, map => - { - outerE.MoveNext(ct).Then(to => - { - to.Handle(tcs, res => - { - if (res) - { - var element = outerE.Current; - var key = default(TKey); - - try - { - key = outerKeySelector(element); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - var innerE = default(IAsyncEnumerable); - if (!map.Contains(key)) - innerE = AsyncEnumerable.Empty(); - else - innerE = map[key].ToAsyncEnumerable(); - - try - { - current = resultSelector(element, innerE); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - tcs.TrySetResult(true); - } - else - { - tcs.TrySetResult(false); - } - }); - }); - }); - }); - }; - return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(outerE); - }, - () => current, - d.Dispose - ); - }); + return new GroupJoinAsyncEnumerable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer); } public static IAsyncEnumerable GroupJoin(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector) { if (outer == null) - throw new ArgumentNullException("outer"); + throw new ArgumentNullException(nameof(outer)); if (inner == null) - throw new ArgumentNullException("inner"); + throw new ArgumentNullException(nameof(inner)); if (outerKeySelector == null) - throw new ArgumentNullException("outerKeySelector"); + throw new ArgumentNullException(nameof(outerKeySelector)); if (innerKeySelector == null) - throw new ArgumentNullException("innerKeySelector"); + throw new ArgumentNullException(nameof(innerKeySelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); } + + private sealed class GroupJoinAsyncEnumerable : IAsyncEnumerable + { + private readonly IAsyncEnumerable _outer; + private readonly IAsyncEnumerable _inner; + private readonly Func _outerKeySelector; + private readonly Func _innerKeySelector; + private readonly Func, TResult> _resultSelector; + private readonly IEqualityComparer _comparer; + + public GroupJoinAsyncEnumerable( + IAsyncEnumerable outer, + IAsyncEnumerable inner, + Func outerKeySelector, + Func innerKeySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + _outer = outer; + _inner = inner; + _outerKeySelector = outerKeySelector; + _innerKeySelector = innerKeySelector; + _resultSelector = resultSelector; + _comparer = comparer; + } + + public IAsyncEnumerator GetEnumerator() + => new GroupJoinAsyncEnumerator( + _outer.GetEnumerator(), + _inner, + _outerKeySelector, + _innerKeySelector, + _resultSelector, + _comparer); + + private sealed class GroupJoinAsyncEnumerator : IAsyncEnumerator + { + private readonly IAsyncEnumerator _outer; + private readonly IAsyncEnumerable _inner; + private readonly Func _outerKeySelector; + private readonly Func _innerKeySelector; + private readonly Func, TResult> _resultSelector; + private readonly IEqualityComparer _comparer; + + private Internal.Lookup _lookup; + + public GroupJoinAsyncEnumerator( + IAsyncEnumerator outer, + IAsyncEnumerable inner, + Func outerKeySelector, + Func innerKeySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + _outer = outer; + _inner = inner; + _outerKeySelector = outerKeySelector; + _innerKeySelector = innerKeySelector; + _resultSelector = resultSelector; + _comparer = comparer; + } + + public async Task MoveNext(CancellationToken cancellationToken) + { + // nothing to do + if (!await _outer.MoveNext(cancellationToken).ConfigureAwait(false)) + { + return false; + } + + if (_lookup == null) + { + _lookup = await Internal.Lookup.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, cancellationToken).ConfigureAwait(false); + } + + var item = _outer.Current; + Current = _resultSelector(item, new AsyncEnumerableAdapter(_lookup[_outerKeySelector(item)])); + return true; + } + + public TResult Current { get; private set; } + + public void Dispose() + { + _outer.Dispose(); + } + + + } + } + + private sealed class AsyncEnumerableAdapter : IAsyncEnumerable + { + private readonly IEnumerable _source; + + public AsyncEnumerableAdapter(IEnumerable source) + { + _source = source; + } + + public IAsyncEnumerator GetEnumerator() + => new AsyncEnumeratorAdapter(_source.GetEnumerator()); + + private sealed class AsyncEnumeratorAdapter : IAsyncEnumerator + { + private readonly IEnumerator _enumerator; + + public AsyncEnumeratorAdapter(IEnumerator enumerator) + { + _enumerator = enumerator; + } + + public Task MoveNext(CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + +#if HAS_AWAIT + return Task.FromResult(_enumerator.MoveNext()); +#else + return TaskEx.FromResult(_enumerator.MoveNext()); +#endif + } + + public T Current => _enumerator.Current; + + public void Dispose() => _enumerator.Dispose(); + } + } + public static IAsyncEnumerable Join(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer) { if (outer == null) - throw new ArgumentNullException("outer"); + throw new ArgumentNullException(nameof(outer)); if (inner == null) - throw new ArgumentNullException("inner"); + throw new ArgumentNullException(nameof(inner)); if (outerKeySelector == null) - throw new ArgumentNullException("outerKeySelector"); + throw new ArgumentNullException(nameof(outerKeySelector)); if (innerKeySelector == null) - throw new ArgumentNullException("innerKeySelector"); + throw new ArgumentNullException(nameof(innerKeySelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return Create(() => { @@ -484,25 +448,21 @@ public static IAsyncEnumerable Join(this var innerMap = new Dictionary>(comparer); var q = new Queue(); - var gate = new object(); - - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (q.Count > 0) { current = q.Dequeue(); - tcs.TrySetResult(true); - return; + return true; } var b = useOuter; if (ie == null && oe == null) { - tcs.TrySetResult(false); - return; + return false; } - else if (ie == null) + if (ie == null) b = true; else if (oe == null) b = false; @@ -510,135 +470,87 @@ public static IAsyncEnumerable Join(this var enqueue = new Func((o, i) => { - var result = default(TResult); - try - { - result = resultSelector(o, i); - } - catch (Exception exception) - { - tcs.TrySetException(exception); - return false; - } - + var result = resultSelector(o, i); q.Enqueue(result); return true; }); if (b) - oe.MoveNext(ct).Then(t => + { + if (await oe.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => + var element = oe.Current; + var key = default(TKey); + + key = outerKeySelector(element); + + var outerList = default(List); + if (!outerMap.TryGetValue(key, out outerList)) { - if (res) - { - var element = oe.Current; - var key = default(TKey); - - try - { - key = outerKeySelector(element); - } - catch (Exception exception) - { - tcs.TrySetException(exception); - return; - } - - var outerList = default(List); - if (!outerMap.TryGetValue(key, out outerList)) - { - outerList = new List(); - outerMap.Add(key, outerList); - } - - outerList.Add(element); - - var innerList = default(List); - if (!innerMap.TryGetValue(key, out innerList)) - { - innerList = new List(); - innerMap.Add(key, innerList); - } - - foreach (var v in innerList) - { - if (!enqueue(element, v)) - return; - } - - f(tcs, ct); - } - else - { - oe.Dispose(); - oe = null; - f(tcs, ct); - } - }); - }); - else - ie.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => + outerList = new List(); + outerMap.Add(key, outerList); + } + + outerList.Add(element); + + var innerList = default(List); + if (!innerMap.TryGetValue(key, out innerList)) { - if (res) - { - var element = ie.Current; - var key = default(TKey); - - try - { - key = innerKeySelector(element); - } - catch (Exception exception) - { - tcs.TrySetException(exception); - return; - } - - var innerList = default(List); - if (!innerMap.TryGetValue(key, out innerList)) - { - innerList = new List(); - innerMap.Add(key, innerList); - } - - innerList.Add(element); - - var outerList = default(List); - if (!outerMap.TryGetValue(key, out outerList)) - { - outerList = new List(); - outerMap.Add(key, outerList); - } - - foreach (var v in outerList) - { - if (!enqueue(v, element)) - return; - } - - f(tcs, ct); - } - else - { - ie.Dispose(); - ie = null; - f(tcs, ct); - } - }); - }); + innerList = new List(); + innerMap.Add(key, innerList); + } + + foreach (var v in innerList) + { + if (!enqueue(element, v)) + return false; + } + + return await f(ct).ConfigureAwait(false); + } + oe.Dispose(); + oe = null; + return await f(ct).ConfigureAwait(false); + } + if (await ie.MoveNext(ct).ConfigureAwait(false)) + { + var element = ie.Current; + var key = innerKeySelector(element); + + var innerList = default(List); + if (!innerMap.TryGetValue(key, out innerList)) + { + innerList = new List(); + innerMap.Add(key, innerList); + } + + innerList.Add(element); + + var outerList = default(List); + if (!outerMap.TryGetValue(key, out outerList)) + { + outerList = new List(); + outerMap.Add(key, outerList); + } + + foreach (var v in outerList) + { + if (!enqueue(v, element)) + return false; + } + + return await f(ct).ConfigureAwait(false); + } + ie.Dispose(); + ie = null; + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(oe).UsingEnumerator(ie); - }, + f, () => current, - d.Dispose + d.Dispose, + ie ); }); } @@ -646,15 +558,15 @@ public static IAsyncEnumerable Join(this public static IAsyncEnumerable Join(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector) { if (outer == null) - throw new ArgumentNullException("outer"); + throw new ArgumentNullException(nameof(outer)); if (inner == null) - throw new ArgumentNullException("inner"); + throw new ArgumentNullException(nameof(inner)); if (outerKeySelector == null) - throw new ArgumentNullException("outerKeySelector"); + throw new ArgumentNullException(nameof(outerKeySelector)); if (innerKeySelector == null) - throw new ArgumentNullException("innerKeySelector"); + throw new ArgumentNullException(nameof(innerKeySelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer.Default); } @@ -662,7 +574,7 @@ public static IAsyncEnumerable Join(this public static IAsyncEnumerable Concat(this IEnumerable> sources) { if (sources == null) - throw new ArgumentNullException("sources"); + throw new ArgumentNullException(nameof(sources)); return sources.Concat_(); } @@ -670,7 +582,7 @@ public static IAsyncEnumerable Concat(this IEnumerable Concat(params IAsyncEnumerable[] sources) { if (sources == null) - throw new ArgumentNullException("sources"); + throw new ArgumentNullException(nameof(sources)); return sources.Concat_(); } @@ -686,60 +598,39 @@ private static IAsyncEnumerable Concat_(this IEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (e == null) { var b = false; - try - { - b = se.MoveNext(); - if (b) - e = se.Current.GetEnumerator(); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + b = se.MoveNext(); + if (b) + e = se.Current.GetEnumerator(); if (!b) { - tcs.TrySetResult(false); - return; + return false; } a.Disposable = e; } - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - tcs.TrySetResult(true); - } - else - { - e.Dispose(); - e = null; + return true; + } + e.Dispose(); + e = null; - f(tcs, ct); - } - }); - }); + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(a); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + a ); }); } @@ -747,9 +638,9 @@ private static IAsyncEnumerable Concat_(this IEnumerable SelectMany(this IAsyncEnumerable source, IAsyncEnumerable other) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (other == null) - throw new ArgumentNullException("other"); + throw new ArgumentNullException(nameof(other)); return source.SelectMany(_ => other); } diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Single.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Single.cs index c23c22b7fc..4fdcc08d28 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Single.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Single.cs @@ -3,7 +3,7 @@ // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; -using System.Linq; +using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; @@ -14,9 +14,9 @@ public static partial class AsyncEnumerable public static IAsyncEnumerable Select(this IAsyncEnumerable source, Func selector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return Create(() => { @@ -27,37 +27,18 @@ public static IAsyncEnumerable Select(this IAsyncEnum var d = Disposable.Create(cts, e); return Create( - (ct, tcs) => + async ct => { - e.MoveNext(cts.Token).Then(t => + if (await e.MoveNext(cts.Token).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - try - { - current = selector(e.Current); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - tcs.TrySetResult(true); - } - else - { - tcs.TrySetResult(false); - } - }); - }); - - return tcs.Task.UsingEnumerator(e); + current = selector(e.Current); + return true; + } + return false; }, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -65,9 +46,9 @@ public static IAsyncEnumerable Select(this IAsyncEnum public static IAsyncEnumerable Select(this IAsyncEnumerable source, Func selector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return Create(() => { @@ -79,37 +60,18 @@ public static IAsyncEnumerable Select(this IAsyncEnum var d = Disposable.Create(cts, e); return Create( - (ct, tcs) => + async ct => { - e.MoveNext(cts.Token).Then(t => + if (await e.MoveNext(cts.Token).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - try - { - current = selector(e.Current, checked(index++)); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - tcs.TrySetResult(true); - } - else - { - tcs.TrySetResult(false); - } - }); - }); - - return tcs.Task.UsingEnumerator(e); + current = selector(e.Current, checked(index++)); + return true; + } + return false; }, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -117,7 +79,7 @@ public static IAsyncEnumerable Select(this IAsyncEnum public static IAsyncEnumerable AsAsyncEnumerable(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Select(x => x); } @@ -125,9 +87,9 @@ public static IAsyncEnumerable AsAsyncEnumerable(this IAsyncEn public static IAsyncEnumerable Where(this IAsyncEnumerable source, Func predicate) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return Create(() => { @@ -136,45 +98,23 @@ public static IAsyncEnumerable Where(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - var b = false; - try - { - b = predicate(e.Current); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (b) - tcs.TrySetResult(true); - else - f(tcs, ct); - } - else - tcs.TrySetResult(false); - }); - }); + if (predicate(e.Current)) + return true; + return await f(ct).ConfigureAwait(false); + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + ct => f(cts.Token), () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -182,9 +122,9 @@ public static IAsyncEnumerable Where(this IAsyncEnumerable Where(this IAsyncEnumerable source, Func predicate) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return Create(() => { @@ -194,45 +134,23 @@ public static IAsyncEnumerable Where(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - var b = false; - try - { - b = predicate(e.Current, checked(index++)); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (b) - tcs.TrySetResult(true); - else - f(tcs, ct); - } - else - tcs.TrySetResult(false); - }); - }); + if (predicate(e.Current, checked(index++))) + return true; + return await f(ct).ConfigureAwait(false); + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + ct => f(cts.Token), () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -240,9 +158,9 @@ public static IAsyncEnumerable Where(this IAsyncEnumerable SelectMany(this IAsyncEnumerable source, Func> selector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return Create(() => { @@ -254,66 +172,36 @@ public static IAsyncEnumerable SelectMany(this IAsync var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, innerDisposable, e); - var outer = default(Action, CancellationToken>); - var inner = default(Action, CancellationToken>); + var inner = default(Func>); + var outer = default(Func>); - inner = (tcs, ct) => + inner = async ct => { - ie.MoveNext(ct).Then(t => + if (await ie.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - tcs.TrySetResult(true); - } - else - { - innerDisposable.Disposable = null; - outer(tcs, ct); - } - }); - }); + return true; + } + innerDisposable.Disposable = null; + return await outer(ct).ConfigureAwait(false); }; - outer = (tcs, ct) => + outer = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - try - { - ie = selector(e.Current).GetEnumerator(); - innerDisposable.Disposable = ie; - - inner(tcs, ct); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - } - } - else - tcs.TrySetResult(false); - }); - }); - }; + var enumerable = selector(e.Current); + ie = enumerable.GetEnumerator(); + innerDisposable.Disposable = ie; - return Create( - (ct, tcs) => - { - if (ie == null) - outer(tcs, cts.Token); - else - inner(tcs, cts.Token); + return await inner(ct).ConfigureAwait(false); + } + return false; + }; - return tcs.Task.UsingEnumerator(e); - }, + return Create(ct => ie == null ? outer(cts.Token) : inner(cts.Token), () => ie.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -321,9 +209,9 @@ public static IAsyncEnumerable SelectMany(this IAsync public static IAsyncEnumerable SelectMany(this IAsyncEnumerable source, Func> selector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return Create(() => { @@ -337,66 +225,36 @@ public static IAsyncEnumerable SelectMany(this IAsync var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, innerDisposable, e); - var outer = default(Action, CancellationToken>); - var inner = default(Action, CancellationToken>); + var inner = default(Func>); + var outer = default(Func>); - inner = (tcs, ct) => + inner = async ct => { - ie.MoveNext(ct).Then(t => + if (await ie.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - tcs.TrySetResult(true); - } - else - { - innerDisposable.Disposable = null; - outer(tcs, ct); - } - }); - }); + return true; + } + innerDisposable.Disposable = null; + return await outer(ct).ConfigureAwait(false); }; - outer = (tcs, ct) => + outer = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - try - { - ie = selector(e.Current, checked(index++)).GetEnumerator(); - innerDisposable.Disposable = ie; - - inner(tcs, ct); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - } - } - else - tcs.TrySetResult(false); - }); - }); - }; + var enumerable = selector(e.Current, checked(index++)); + ie = enumerable.GetEnumerator(); + innerDisposable.Disposable = ie; - return Create( - (ct, tcs) => - { - if (ie == null) - outer(tcs, cts.Token); - else - inner(tcs, cts.Token); + return await inner(ct).ConfigureAwait(false); + } + return false; + }; - return tcs.Task.UsingEnumerator(e); - }, + return Create(ct => ie == null ? outer(cts.Token) : inner(cts.Token), () => ie.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -404,11 +262,11 @@ public static IAsyncEnumerable SelectMany(this IAsync public static IAsyncEnumerable SelectMany(this IAsyncEnumerable source, Func> selector, Func resultSelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return source.SelectMany(x => selector(x).Select(y => resultSelector(x, y))); } @@ -416,11 +274,11 @@ public static IAsyncEnumerable SelectMany SelectMany(this IAsyncEnumerable source, Func> selector, Func resultSelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return source.SelectMany((x, i) => selector(x, i).Select(y => resultSelector(x, y))); } @@ -428,7 +286,7 @@ public static IAsyncEnumerable SelectMany OfType(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Where(x => x is TType).Cast(); } @@ -436,7 +294,7 @@ public static IAsyncEnumerable OfType(this IAsyncEnumerable Cast(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Select(x => (TResult)x); } @@ -444,9 +302,9 @@ public static IAsyncEnumerable Cast(this IAsyncEnumerable Take(this IAsyncEnumerable source, int count) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Create(() => { @@ -457,28 +315,23 @@ public static IAsyncEnumerable Take(this IAsyncEnumerable + async ct => { if (n == 0) - return TaskExt.False; + return false; - e.MoveNext(cts.Token).Then(t => - { - t.Handle(tcs, res => - { - --n; + var result = await e.MoveNext(cts.Token).ConfigureAwait(false); - if (n == 0) - e.Dispose(); + --n; - tcs.TrySetResult(res); - }); - }); + if (n == 0) + e.Dispose(); - return tcs.Task.UsingEnumerator(e); + return result; }, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -486,9 +339,9 @@ public static IAsyncEnumerable Take(this IAsyncEnumerable TakeWhile(this IAsyncEnumerable source, Func predicate) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return Create(() => { @@ -498,40 +351,17 @@ public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable var d = Disposable.Create(cts, e); return Create( - (ct, tcs) => + async ct => { - e.MoveNext(cts.Token).Then(t => + if (await e.MoveNext(cts.Token).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - var b = false; - - try - { - b = predicate(e.Current); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (b) - { - tcs.TrySetResult(true); - return; - } - } - tcs.TrySetResult(false); - }); - }); - - return tcs.Task.UsingEnumerator(e); + return predicate(e.Current); + } + return false; }, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -539,9 +369,9 @@ public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable source, Func predicate) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return Create(() => { @@ -552,40 +382,17 @@ public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable var d = Disposable.Create(cts, e); return Create( - (ct, tcs) => + async ct => { - e.MoveNext(cts.Token).Then(t => + if (await e.MoveNext(cts.Token).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - var b = false; - - try - { - b = predicate(e.Current, checked(index++)); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (b) - { - tcs.TrySetResult(true); - return; - } - } - tcs.TrySetResult(false); - }); - }); - - return tcs.Task.UsingEnumerator(e); + return predicate(e.Current, checked(index++)); + } + return false; }, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -593,9 +400,9 @@ public static IAsyncEnumerable TakeWhile(this IAsyncEnumerable public static IAsyncEnumerable Skip(this IAsyncEnumerable source, int count) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Create(() => { @@ -605,38 +412,27 @@ public static IAsyncEnumerable Skip(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { + var moveNext = await e.MoveNext(ct).ConfigureAwait(false); if (n == 0) - e.MoveNext(ct).Then(t => - { - t.Handle(tcs, x => tcs.TrySetResult(x)); - }); - else { - --n; - e.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => - { - if (!res) - tcs.TrySetResult(false); - else - f(tcs, ct); - }); - }); + return moveNext; } + --n; + if (!moveNext) + { + return false; + } + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + ct => f(cts.Token), () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -644,9 +440,9 @@ public static IAsyncEnumerable Skip(this IAsyncEnumerable SkipWhile(this IAsyncEnumerable source, Func predicate) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return Create(() => { @@ -656,53 +452,28 @@ public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, e); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (skipping) - e.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => - { - if (res) - { - var result = false; - try - { - result = predicate(e.Current); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - if (result) - f(tcs, ct); - else - { - skipping = false; - tcs.TrySetResult(true); - } - } - else - tcs.TrySetResult(false); - }); - }); - else - e.MoveNext(ct).Then(t => + { + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, x => tcs.TrySetResult(x)); - }); + if (predicate(e.Current)) + return await f(ct).ConfigureAwait(false); + skipping = false; + return true; + } + return false; + } + return await e.MoveNext(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -710,9 +481,9 @@ public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable source, Func predicate) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (predicate == null) - throw new ArgumentNullException("predicate"); + throw new ArgumentNullException(nameof(predicate)); return Create(() => { @@ -723,53 +494,28 @@ public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, e); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (skipping) - e.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => - { - if (res) - { - var result = false; - try - { - result = predicate(e.Current, checked(index++)); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - if (result) - f(tcs, ct); - else - { - skipping = false; - tcs.TrySetResult(true); - } - } - else - tcs.TrySetResult(false); - }); - }); - else - e.MoveNext(ct).Then(t => + { + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, x => tcs.TrySetResult(x)); - }); + if (predicate(e.Current, checked(index++))) + return await f(ct).ConfigureAwait(false); + skipping = false; + return true; + } + return false; + } + return await e.MoveNext(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => e.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -777,7 +523,7 @@ public static IAsyncEnumerable SkipWhile(this IAsyncEnumerable public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnumerable source, TSource defaultValue) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return Create(() => { @@ -789,46 +535,31 @@ public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnume var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, e); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (done) - tcs.TrySetResult(false); - else - e.MoveNext(ct).Then(t => - { - t.Handle(tcs, res => - { - if (res) - { - hasElements = true; - current = e.Current; - tcs.TrySetResult(true); - } - else - { - done = true; - - if (!hasElements) - { - current = defaultValue; - tcs.TrySetResult(true); - } - else - tcs.TrySetResult(false); - } - }); - }); + return false; + if (await e.MoveNext(ct).ConfigureAwait(false)) + { + hasElements = true; + current = e.Current; + return true; + } + done = true; + if (!hasElements) + { + current = defaultValue; + return true; + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -836,7 +567,7 @@ public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnume public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.DefaultIfEmpty(default(TSource)); } @@ -844,9 +575,9 @@ public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnume public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return Defer(() => { @@ -858,7 +589,7 @@ public static IAsyncEnumerable Distinct(this IAsyncEnumerable< public static IAsyncEnumerable Distinct(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.Distinct(EqualityComparer.Default); } @@ -866,7 +597,7 @@ public static IAsyncEnumerable Distinct(this IAsyncEnumerable< public static IAsyncEnumerable Reverse(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return Create(() => { @@ -877,29 +608,19 @@ public static IAsyncEnumerable Reverse(this IAsyncEnumerable + async ct => { if (stack == null) { - Create(() => e).Aggregate(new Stack(), (s, x) => { s.Push(x); return s; }, cts.Token).Then(t => - { - t.Handle(tcs, res => - { - stack = res; - tcs.TrySetResult(stack.Count > 0); - }); - }); + stack = await Create(() => e).Aggregate(new Stack(), (s, x) => { s.Push(x); return s; }, cts.Token).ConfigureAwait(false); + return stack.Count > 0; } - else - { - stack.Pop(); - tcs.TrySetResult(stack.Count > 0); - } - - return tcs.Task.UsingEnumerator(e); + stack.Pop(); + return stack.Count > 0; }, () => stack.Peek(), - d.Dispose + d.Dispose, + e ); }); } @@ -907,11 +628,11 @@ public static IAsyncEnumerable Reverse(this IAsyncEnumerable OrderBy(this IAsyncEnumerable source, Func keySelector, IComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return new OrderedAsyncEnumerable( Create(() => @@ -919,23 +640,14 @@ public static IOrderedAsyncEnumerable OrderBy(this IAsyn var current = default(IEnumerable); return Create( - ct => + async ct => { - var tcs = new TaskCompletionSource(); if (current == null) { - source.ToList(ct).Then(t => - { - t.Handle(tcs, res => - { - current = res; - tcs.TrySetResult(true); - }); - }); + current = await source.ToList(ct).ConfigureAwait(false); + return true; } - else - tcs.TrySetResult(false); - return tcs.Task; + return false; }, () => current, () => { } @@ -949,9 +661,9 @@ public static IOrderedAsyncEnumerable OrderBy(this IAsyn public static IOrderedAsyncEnumerable OrderBy(this IAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.OrderBy(keySelector, Comparer.Default); } @@ -959,11 +671,11 @@ public static IOrderedAsyncEnumerable OrderBy(this IAsyn public static IOrderedAsyncEnumerable OrderByDescending(this IAsyncEnumerable source, Func keySelector, IComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.OrderBy(keySelector, new ReverseComparer(comparer)); } @@ -971,9 +683,9 @@ public static IOrderedAsyncEnumerable OrderByDescending( public static IOrderedAsyncEnumerable OrderByDescending(this IAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.OrderByDescending(keySelector, Comparer.Default); } @@ -981,9 +693,9 @@ public static IOrderedAsyncEnumerable OrderByDescending( public static IOrderedAsyncEnumerable ThenBy(this IOrderedAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.ThenBy(keySelector, Comparer.Default); } @@ -991,11 +703,11 @@ public static IOrderedAsyncEnumerable ThenBy(this IOrder public static IOrderedAsyncEnumerable ThenBy(this IOrderedAsyncEnumerable source, Func keySelector, IComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.CreateOrderedEnumerable(keySelector, comparer, false); } @@ -1003,9 +715,9 @@ public static IOrderedAsyncEnumerable ThenBy(this IOrder public static IOrderedAsyncEnumerable ThenByDescending(this IOrderedAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.ThenByDescending(keySelector, Comparer.Default); } @@ -1013,11 +725,11 @@ public static IOrderedAsyncEnumerable ThenByDescending(t public static IOrderedAsyncEnumerable ThenByDescending(this IOrderedAsyncEnumerable source, Func keySelector, IComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.CreateOrderedEnumerable(keySelector, comparer, true); } @@ -1070,74 +782,35 @@ IAsyncEnumerable> Classes() var d1 = new AssignableDisposable(); var d = Disposable.Create(cts, e, d1); - var f = default(Action, CancellationToken>); - var g = default(Action, CancellationToken>); + var f = default(Func>); - f = (tcs, ct) => + f = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - try - { - foreach (var group in e.Current.OrderBy(keySelector, comparer).GroupUntil(keySelector, x => x, comparer)) - list.Add(group); - f(tcs, ct); - } - catch (Exception exception) - { - tcs.TrySetException(exception); - return; - } - } - else - { - e.Dispose(); - - e1 = list.GetEnumerator(); - d1.Disposable = e1; - - g(tcs, ct); - } - }); - }); - }; - - g = (tcs, ct) => - { - var res = false; - try - { - res = e1.MoveNext(); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; + list.AddRange(e.Current.OrderBy(keySelector, comparer).GroupUntil(keySelector, x => x, comparer)); + return await f(ct).ConfigureAwait(false); } + e.Dispose(); + + e1 = list.GetEnumerator(); + d1.Disposable = e1; - tcs.TrySetResult(res); + return e1.MoveNext(); }; return Create( - (ct, tcs) => + async ct => { if (e1 != null) { - g(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e1); - } - else - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); + return e1.MoveNext(); } + return await f(cts.Token).ConfigureAwait(false); }, () => e1.Current, - d.Dispose + d.Dispose, + e ); }); } @@ -1166,13 +839,13 @@ public int Compare(T x, T y) public static IAsyncEnumerable> GroupBy(this IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return Create(() => { @@ -1187,9 +860,9 @@ public static IAsyncEnumerable> GroupBy); - var faulted = default(Exception); + var faulted = default(ExceptionDispatchInfo); - var task = default(Task); + var res = default(bool?); var cts = new CancellationTokenDisposable(); var refCount = new Disposable( @@ -1202,126 +875,94 @@ public static IAsyncEnumerable> GroupBy>); - iterateSource = ct => + iterateSource = async ct => { - var tcs = default(TaskCompletionSource); lock (gate) { - if (task != null) + if (res != null) { - return task; - } - else - { - tcs = new TaskCompletionSource(); - task = tcs.Task.UsingEnumerator(e); + return res.Value; } + res = null; } - if (faulted != null) - { - tcs.TrySetException(faulted); - return task; - } + faulted?.Throw(); - e.MoveNext(ct).Then(t => + try { - t.Handle(tcs, - res => + res = await e.MoveNext(ct).ConfigureAwait(false); + if (res == true) + { + var key = default(TKey); + var element = default(TElement); + + var cur = e.Current; + try { - if (res) - { - var key = default(TKey); - var element = default(TElement); - - var cur = e.Current; - try - { - key = keySelector(cur); - element = elementSelector(cur); - } - catch (Exception exception) - { - foreach (var v in map.Values) - v.Error(exception); - - tcs.TrySetException(exception); - return; - } - - var group = default(Grouping); - if (!map.TryGetValue(key, out group)) - { - group = new Grouping(key, iterateSource, refCount); - map.Add(key, group); - lock (list) - list.Add(group); - - Interlocked.Increment(ref count); - } - group.Add(element); - } - - tcs.TrySetResult(res); - }, - ex => + key = keySelector(cur); + element = elementSelector(cur); + } + catch (Exception exception) { foreach (var v in map.Values) - v.Error(ex); + v.Error(exception); - faulted = ex; - tcs.TrySetException(ex); + throw; } - ); - lock (gate) - { - task = null; + var group = default(Grouping); + if (!map.TryGetValue(key, out group)) + { + group = new Grouping(key, iterateSource, refCount); + map.Add(key, group); + lock (list) + list.Add(group); + + Interlocked.Increment(ref count); + } + group.Add(element); } - }); - return tcs.Task.UsingEnumerator(e); + return res.Value; + } + catch (Exception ex) + { + foreach (var v in map.Values) + v.Error(ex); + + faulted = ExceptionDispatchInfo.Capture(ex); + throw; + } + finally + { + res = null; + } }; - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - iterateSource(ct).Then(t => + var result = await iterateSource(ct).ConfigureAwait(false); + + current = null; + lock (list) { - t.Handle(tcs, - res => - { - current = null; - lock (list) - { - if (index < list.Count) - current = list[index++]; - } - - if (current != null) - { - tcs.TrySetResult(true); - } - else - { - if (res) - f(tcs, ct); - else - tcs.TrySetResult(false); - } - } - ); - }); + if (index < list.Count) + current = list[index++]; + } + + if (current != null) + { + return true; + } + return result && await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task; - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -1329,11 +970,11 @@ public static IAsyncEnumerable> GroupBy> GroupBy(this IAsyncEnumerable source, Func keySelector, Func elementSelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); return source.GroupBy(keySelector, elementSelector, EqualityComparer.Default); } @@ -1341,11 +982,11 @@ public static IAsyncEnumerable> GroupBy> GroupBy(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.GroupBy(keySelector, x => x, comparer); } @@ -1353,9 +994,9 @@ public static IAsyncEnumerable> GroupBy> GroupBy(this IAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.GroupBy(keySelector, x => x, EqualityComparer.Default); } @@ -1363,15 +1004,15 @@ public static IAsyncEnumerable> GroupBy GroupBy(this IAsyncEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.GroupBy(keySelector, elementSelector, comparer).Select(g => resultSelector(g.Key, g)); } @@ -1379,13 +1020,13 @@ public static IAsyncEnumerable GroupBy GroupBy(this IAsyncEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (elementSelector == null) - throw new ArgumentNullException("elementSelector"); + throw new ArgumentNullException(nameof(elementSelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return source.GroupBy(keySelector, elementSelector, EqualityComparer.Default).Select(g => resultSelector(g.Key, g)); } @@ -1393,13 +1034,13 @@ public static IAsyncEnumerable GroupBy GroupBy(this IAsyncEnumerable source, Func keySelector, Func, TResult> resultSelector, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.GroupBy(keySelector, x => x, comparer).Select(g => resultSelector(g.Key, g)); } @@ -1407,11 +1048,11 @@ public static IAsyncEnumerable GroupBy(this IAs public static IAsyncEnumerable GroupBy(this IAsyncEnumerable source, Func keySelector, Func, TResult> resultSelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); + throw new ArgumentNullException(nameof(resultSelector)); return source.GroupBy(keySelector, x => x, EqualityComparer.Default).Select(g => resultSelector(g.Key, g)); } @@ -1422,7 +1063,7 @@ class Grouping : IAsyncGrouping private readonly IDisposable sourceDisposable; private readonly List elements = new List(); private bool done = false; - private Exception exception = null; + private ExceptionDispatchInfo exception = null; public Grouping(TKey key, Func> iterateSource, IDisposable sourceDisposable) { @@ -1446,7 +1087,7 @@ public void Add(TElement element) public void Error(Exception exception) { done = true; - this.exception = exception; + this.exception = ExceptionDispatchInfo.Capture(exception); } public IAsyncEnumerator GetEnumerator() @@ -1456,8 +1097,8 @@ public IAsyncEnumerator GetEnumerator() var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, sourceDisposable); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { var size = 0; lock (elements) @@ -1465,39 +1106,29 @@ public IAsyncEnumerator GetEnumerator() if (index < size) { - tcs.TrySetResult(true); + return true; } - else if (done) + if (done) { - if (exception != null) - tcs.TrySetException(exception); - else - tcs.TrySetResult(false); + exception?.Throw(); + return false; } - else + if (await iterateSource(ct).ConfigureAwait(false)) { - iterateSource(ct).Then(t => - { - t.Handle(tcs, res => - { - if (res) - f(tcs, ct); - else - tcs.TrySetResult(false); - }); - }); + return await f(ct).ConfigureAwait(false); } + return false; }; return Create( - (ct, tcs) => + ct => { ++index; - f(tcs, cts.Token); - return tcs.Task; + return f(cts.Token); }, () => elements[index], - d.Dispose + d.Dispose, + null ); } } @@ -1507,9 +1138,9 @@ public IAsyncEnumerator GetEnumerator() public static IAsyncEnumerable Do(this IAsyncEnumerable source, Action onNext) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (onNext == null) - throw new ArgumentNullException("onNext"); + throw new ArgumentNullException(nameof(onNext)); return DoHelper(source, onNext, _ => { }, () => { }); } @@ -1517,11 +1148,11 @@ public static IAsyncEnumerable Do(this IAsyncEnumerable Do(this IAsyncEnumerable source, Action onNext, Action onCompleted) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (onNext == null) - throw new ArgumentNullException("onNext"); + throw new ArgumentNullException(nameof(onNext)); if (onCompleted == null) - throw new ArgumentNullException("onCompleted"); + throw new ArgumentNullException(nameof(onCompleted)); return DoHelper(source, onNext, _ => { }, onCompleted); } @@ -1529,11 +1160,11 @@ public static IAsyncEnumerable Do(this IAsyncEnumerable Do(this IAsyncEnumerable source, Action onNext, Action onError) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (onNext == null) - throw new ArgumentNullException("onNext"); + throw new ArgumentNullException(nameof(onNext)); if (onError == null) - throw new ArgumentNullException("onError"); + throw new ArgumentNullException(nameof(onError)); return DoHelper(source, onNext, onError, () => { }); } @@ -1541,13 +1172,13 @@ public static IAsyncEnumerable Do(this IAsyncEnumerable Do(this IAsyncEnumerable source, Action onNext, Action onError, Action onCompleted) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (onNext == null) - throw new ArgumentNullException("onNext"); + throw new ArgumentNullException(nameof(onNext)); if (onError == null) - throw new ArgumentNullException("onError"); + throw new ArgumentNullException(nameof(onError)); if (onCompleted == null) - throw new ArgumentNullException("onCompleted"); + throw new ArgumentNullException(nameof(onCompleted)); return DoHelper(source, onNext, onError, onCompleted); } @@ -1556,9 +1187,9 @@ public static IAsyncEnumerable Do(this IAsyncEnumerable Do(this IAsyncEnumerable source, IObserver observer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (observer == null) - throw new ArgumentNullException("observer"); + throw new ArgumentNullException(nameof(observer)); return DoHelper(source, observer.OnNext, observer.OnError, observer.OnCompleted); } @@ -1575,51 +1206,39 @@ private static IAsyncEnumerable DoHelper(this IAsyncEnumerable var current = default(TSource); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + try { - if (!t.IsCanceled) + var result = await e.MoveNext(ct).ConfigureAwait(false); + if (!result) { - try - { - if (t.IsFaulted) - { - onError(t.Exception); - } - else if (!t.Result) - { - onCompleted(); - } - else - { - current = e.Current; - onNext(current); - } - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + onCompleted(); } - - t.Handle(tcs, res => + else { - tcs.TrySetResult(res); - }); - }); + current = e.Current; + onNext(current); + } + return result; + } + catch (OperationCanceledException) + { + throw; + } + catch (Exception ex) + { + onError(ex); + throw; + } }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -1627,9 +1246,9 @@ private static IAsyncEnumerable DoHelper(this IAsyncEnumerable public static void ForEach(this IAsyncEnumerable source, Action action, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (action == null) - throw new ArgumentNullException("action"); + throw new ArgumentNullException(nameof(action)); source.ForEachAsync(action, cancellationToken).Wait(cancellationToken); } @@ -1637,9 +1256,9 @@ public static void ForEach(this IAsyncEnumerable source, Actio public static Task ForEachAsync(this IAsyncEnumerable source, Action action, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (action == null) - throw new ArgumentNullException("action"); + throw new ArgumentNullException(nameof(action)); return source.ForEachAsync((x, i) => action(x), cancellationToken); } @@ -1647,9 +1266,9 @@ public static Task ForEachAsync(this IAsyncEnumerable source, public static void ForEach(this IAsyncEnumerable source, Action action, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (action == null) - throw new ArgumentNullException("action"); + throw new ArgumentNullException(nameof(action)); source.ForEachAsync(action, cancellationToken).Wait(cancellationToken); } @@ -1657,54 +1276,31 @@ public static void ForEach(this IAsyncEnumerable source, Actio public static Task ForEachAsync(this IAsyncEnumerable source, Action action, CancellationToken cancellationToken) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (action == null) - throw new ArgumentNullException("action"); - - var tcs = new TaskCompletionSource(); - - var e = source.GetEnumerator(); + throw new ArgumentNullException(nameof(action)); - var i = 0; + return ForEachAsync_(source, action, cancellationToken); + } - var f = default(Action); - f = ct => + private static async Task ForEachAsync_(IAsyncEnumerable source, Action action, CancellationToken cancellationToken) + { + var index = 0; + using (var e = source.GetEnumerator()) { - e.MoveNext(ct).Then(t => + while (await e.MoveNext(cancellationToken).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - try - { - action(e.Current, i++); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - f(ct); - } - else - tcs.TrySetResult(true); - }); - }); - }; - - f(cancellationToken); - - return tcs.Task.UsingEnumerator(e); + action(e.Current, checked(index++)); + } + } } public static IAsyncEnumerable Repeat(this IAsyncEnumerable source, int count) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Create(() => { @@ -1716,56 +1312,35 @@ public static IAsyncEnumerable Repeat(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (e == null) { if (n-- == 0) { - tcs.TrySetResult(false); - return; + return false; } - try - { - e = source.GetEnumerator(); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + e = source.GetEnumerator(); a.Disposable = e; } - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - current = e.Current; - tcs.TrySetResult(true); - } - else - { - e = null; - f(tcs, ct); - } - }); - }); + current = e.Current; + return true; + } + e = null; + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(d); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -1773,7 +1348,7 @@ public static IAsyncEnumerable Repeat(this IAsyncEnumerable Repeat(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return Create(() => { @@ -1784,50 +1359,30 @@ public static IAsyncEnumerable Repeat(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (e == null) { - try - { - e = source.GetEnumerator(); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + e = source.GetEnumerator(); a.Disposable = e; } - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - current = e.Current; - tcs.TrySetResult(true); - } - else - { - e = null; - f(tcs, ct); - } - }); - }); + current = e.Current; + return true; + } + e = null; + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(d); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -1835,7 +1390,7 @@ public static IAsyncEnumerable Repeat(this IAsyncEnumerable IgnoreElements(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return Create(() => { @@ -1844,32 +1399,22 @@ public static IAsyncEnumerable IgnoreElements(this IAsyncEnume var cts = new CancellationTokenDisposable(); var d = Disposable.Create(cts, e); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (!await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (!res) - { - tcs.TrySetResult(false); - return; - } + return false; + } - f(tcs, ct); - }); - }); + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => { throw new InvalidOperationException(); }, - d.Dispose + d.Dispose, + e ); }); } @@ -1877,7 +1422,7 @@ public static IAsyncEnumerable IgnoreElements(this IAsyncEnume public static IAsyncEnumerable StartWith(this IAsyncEnumerable source, params TSource[] values) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return values.ToAsyncEnumerable().Concat(source); } @@ -1885,9 +1430,9 @@ public static IAsyncEnumerable StartWith(this IAsyncEnumerable public static IAsyncEnumerable> Buffer(this IAsyncEnumerable source, int count) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count <= 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return source.Buffer_(count, count); } @@ -1895,11 +1440,11 @@ public static IAsyncEnumerable> Buffer(this IAsyncEnumer public static IAsyncEnumerable> Buffer(this IAsyncEnumerable source, int count, int skip) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count <= 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); if (skip <= 0) - throw new ArgumentOutOfRangeException("skip"); + throw new ArgumentOutOfRangeException(nameof(skip)); return source.Buffer_(count, skip); } @@ -1920,66 +1465,46 @@ private static IAsyncEnumerable> Buffer_(this IAsyncEnum var current = default(IList); var stopped = false; - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (!stopped) { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => + var item = e.Current; + + if (i++ % skip == 0) + buffers.Enqueue(new List(count)); + + foreach (var buffer in buffers) + buffer.Add(item); + + if (buffers.Count > 0 && buffers.Peek().Count == count) { - if (res) - { - var item = e.Current; - - if (i++ % skip == 0) - buffers.Enqueue(new List(count)); - - foreach (var buffer in buffers) - buffer.Add(item); - - if (buffers.Count > 0 && buffers.Peek().Count == count) - { - current = buffers.Dequeue(); - tcs.TrySetResult(true); - return; - } - - f(tcs, ct); - } - else - { - stopped = true; - e.Dispose(); - - f(tcs, ct); - } - }); - }); + current = buffers.Dequeue(); + return true; + } + return await f(ct).ConfigureAwait(false); + } + stopped = true; + e.Dispose(); + + return await f(ct).ConfigureAwait(false); } - else + if (buffers.Count > 0) { - if (buffers.Count > 0) - { - current = buffers.Dequeue(); - tcs.TrySetResult(true); - } - else - { - tcs.TrySetResult(false); - } + current = buffers.Dequeue(); + return true; } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -1987,11 +1512,11 @@ private static IAsyncEnumerable> Buffer_(this IAsyncEnum public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return Defer(() => { @@ -2003,9 +1528,9 @@ public static IAsyncEnumerable Distinct(this IAsyncEnume public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.Distinct(keySelector, EqualityComparer.Default); } @@ -2013,7 +1538,7 @@ public static IAsyncEnumerable Distinct(this IAsyncEnume public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); return source.DistinctUntilChanged_(x => x, EqualityComparer.Default); } @@ -2021,9 +1546,9 @@ public static IAsyncEnumerable DistinctUntilChanged(this IAsyn public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.DistinctUntilChanged_(x => x, comparer); } @@ -2031,9 +1556,9 @@ public static IAsyncEnumerable DistinctUntilChanged(this IAsyn public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func keySelector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); return source.DistinctUntilChanged_(keySelector, EqualityComparer.Default); } @@ -2041,11 +1566,11 @@ public static IAsyncEnumerable DistinctUntilChanged(this public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (keySelector == null) - throw new ArgumentNullException("keySelector"); + throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) - throw new ArgumentNullException("comparer"); + throw new ArgumentNullException(nameof(comparer)); return source.DistinctUntilChanged_(keySelector, comparer); } @@ -2063,63 +1588,40 @@ private static IAsyncEnumerable DistinctUntilChanged_(th var hasCurrentKey = false; var current = default(TSource); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => + var item = e.Current; + var key = default(TKey); + var comparerEquals = false; + + key = keySelector(item); + + if (hasCurrentKey) { - if (res) - { - var item = e.Current; - var key = default(TKey); - var comparerEquals = false; - - try - { - key = keySelector(item); - - if (hasCurrentKey) - { - comparerEquals = comparer.Equals(currentKey, key); - } - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - if (!hasCurrentKey || !comparerEquals) - { - hasCurrentKey = true; - currentKey = key; - - current = item; - tcs.TrySetResult(true); - } - else - { - f(tcs, ct); - } - } - else - { - tcs.TrySetResult(false); - } - }); - }); + comparerEquals = comparer.Equals(currentKey, key); + } + + if (!hasCurrentKey || !comparerEquals) + { + hasCurrentKey = true; + currentKey = key; + + current = item; + return true; + } + return await f(ct).ConfigureAwait(false); + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -2127,9 +1629,9 @@ private static IAsyncEnumerable DistinctUntilChanged_(th public static IAsyncEnumerable Expand(this IAsyncEnumerable source, Func> selector) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (selector == null) - throw new ArgumentNullException("selector"); + throw new ArgumentNullException(nameof(selector)); return Create(() => { @@ -2144,8 +1646,8 @@ public static IAsyncEnumerable Expand(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (e == null) { @@ -2153,67 +1655,31 @@ public static IAsyncEnumerable Expand(this IAsyncEnumerable - { - t.Handle(tcs, res => - { - if (res) - { - var item = e.Current; - - var next = default(IAsyncEnumerable); - try - { - next = selector(item); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } - - queue.Enqueue(next); - current = item; - tcs.TrySetResult(true); - } - else - { - e = null; - f(tcs, ct); - } - }); - }); + var item = e.Current; + var next = selector(item); + + queue.Enqueue(next); + current = item; + return true; } + e = null; + return await f(ct).ConfigureAwait(false); }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(a); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -2221,9 +1687,9 @@ public static IAsyncEnumerable Expand(this IAsyncEnumerable Scan(this IAsyncEnumerable source, TAccumulate seed, Func accumulator) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (accumulator == null) - throw new ArgumentNullException("accumulator"); + throw new ArgumentNullException(nameof(accumulator)); return Create(() => { @@ -2235,44 +1701,26 @@ public static IAsyncEnumerable Scan(this IAsy var acc = seed; var current = default(TAccumulate); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (!await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (!res) - { - tcs.TrySetResult(false); - return; - } + return false; + } - var item = e.Current; - try - { - acc = accumulator(acc, item); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + var item = e.Current; + acc = accumulator(acc, item); - current = acc; - tcs.TrySetResult(true); - }); - }); + current = acc; + return true; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -2280,9 +1728,9 @@ public static IAsyncEnumerable Scan(this IAsy public static IAsyncEnumerable Scan(this IAsyncEnumerable source, Func accumulator) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (accumulator == null) - throw new ArgumentNullException("accumulator"); + throw new ArgumentNullException(nameof(accumulator)); return Create(() => { @@ -2295,53 +1743,34 @@ public static IAsyncEnumerable Scan(this IAsyncEnumerable, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (!await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (!res) - { - tcs.TrySetResult(false); - return; - } + return false; + } - var item = e.Current; + var item = e.Current; - if (!hasSeed) - { - hasSeed = true; - acc = item; - f(tcs, ct); - return; - } + if (!hasSeed) + { + hasSeed = true; + acc = item; + return await f(ct).ConfigureAwait(false); + } - try - { - acc = accumulator(acc, item); - } - catch (Exception ex) - { - tcs.TrySetException(ex); - return; - } + acc = accumulator(acc, item); - current = acc; - tcs.TrySetResult(true); - }); - }); + current = acc; + return true; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -2349,9 +1778,9 @@ public static IAsyncEnumerable Scan(this IAsyncEnumerable TakeLast(this IAsyncEnumerable source, int count) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Create(() => { @@ -2364,57 +1793,42 @@ public static IAsyncEnumerable TakeLast(this IAsyncEnumerable< var done = false; var current = default(TSource); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { if (!done) { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => + if (count > 0) { - if (res) - { - if (count > 0) - { - var item = e.Current; - if (q.Count >= count) - q.Dequeue(); - q.Enqueue(item); - } - } - else - { - done = true; - e.Dispose(); - } - - f(tcs, ct); - }); - }); - } - else - { - if (q.Count > 0) - { - current = q.Dequeue(); - tcs.TrySetResult(true); + var item = e.Current; + if (q.Count >= count) + q.Dequeue(); + q.Enqueue(item); + } } else { - tcs.TrySetResult(false); + done = true; + e.Dispose(); } + + return await f(ct).ConfigureAwait(false); + } + if (q.Count > 0) + { + current = q.Dequeue(); + return true; } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } @@ -2422,9 +1836,9 @@ public static IAsyncEnumerable TakeLast(this IAsyncEnumerable< public static IAsyncEnumerable SkipLast(this IAsyncEnumerable source, int count) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException(nameof(source)); if (count < 0) - throw new ArgumentOutOfRangeException("count"); + throw new ArgumentOutOfRangeException(nameof(count)); return Create(() => { @@ -2436,44 +1850,29 @@ public static IAsyncEnumerable SkipLast(this IAsyncEnumerable< var q = new Queue(); var current = default(TSource); - var f = default(Action, CancellationToken>); - f = (tcs, ct) => + var f = default(Func>); + f = async ct => { - e.MoveNext(ct).Then(t => + if (await e.MoveNext(ct).ConfigureAwait(false)) { - t.Handle(tcs, res => - { - if (res) - { - var item = e.Current; + var item = e.Current; - q.Enqueue(item); - if (q.Count > count) - { - current = q.Dequeue(); - tcs.TrySetResult(true); - } - else - { - f(tcs, ct); - } - } - else - { - tcs.TrySetResult(false); - } - }); - }); + q.Enqueue(item); + if (q.Count > count) + { + current = q.Dequeue(); + return true; + } + return await f(ct).ConfigureAwait(false); + } + return false; }; return Create( - (ct, tcs) => - { - f(tcs, cts.Token); - return tcs.Task.UsingEnumerator(e); - }, + f, () => current, - d.Dispose + d.Dispose, + e ); }); } diff --git a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerator.cs b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerator.cs index b854b29a2f..71fd60b2f0 100644 --- a/Ix.NET/Source/System.Interactive.Async/AsyncEnumerator.cs +++ b/Ix.NET/Source/System.Interactive.Async/AsyncEnumerator.cs @@ -20,7 +20,7 @@ public static class AsyncEnumerator public static Task MoveNext(this IAsyncEnumerator enumerator) { if (enumerator == null) - throw new ArgumentNullException("enumerator"); + throw new ArgumentNullException(nameof(enumerator)); return enumerator.MoveNext(CancellationToken.None); } diff --git a/Ix.NET/Source/System.Interactive.Async/EmptyArray.cs b/Ix.NET/Source/System.Interactive.Async/EmptyArray.cs new file mode 100644 index 0000000000..02757b7d97 --- /dev/null +++ b/Ix.NET/Source/System.Interactive.Async/EmptyArray.cs @@ -0,0 +1,17 @@ +// 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. +#if NO_ARRAY_EMPTY +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace System.Linq +{ + internal sealed class EmptyArray + { + public static readonly TElement[] Value = new TElement[0]; + } +} +#endif diff --git a/Ix.NET/Source/System.Interactive.Async/Grouping.cs b/Ix.NET/Source/System.Interactive.Async/Grouping.cs new file mode 100644 index 0000000000..60d1c897cf --- /dev/null +++ b/Ix.NET/Source/System.Interactive.Async/Grouping.cs @@ -0,0 +1,131 @@ +// 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.Collections; +using System.Collections.Generic; + +namespace System.Linq.Internal +{ + /// Adapted from System.Linq.Grouping from .NET Framework + /// Source: https://github.com/dotnet/corefx/blob/b90532bc97b07234a7d18073819d019645285f1c/src/System.Linq/src/System/Linq/Grouping.cs#L64 + internal class Grouping : IGrouping, IList + { + internal TKey _key; + internal int _hashCode; + internal TElement[] _elements; + internal int _count; + internal Grouping _hashNext; + internal Grouping _next; + + internal Grouping() + { + } + + internal void Add(TElement element) + { + if (_elements.Length == _count) + { + Array.Resize(ref _elements, checked(_count * 2)); + } + + _elements[_count] = element; + _count++; + } + + internal void Trim() + { + if (_elements.Length != _count) + { + Array.Resize(ref _elements, _count); + } + } + + public IEnumerator GetEnumerator() + { + for (int i = 0; i < _count; i++) + { + yield return _elements[i]; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + // DDB195907: implement IGrouping<>.Key implicitly + // so that WPF binding works on this property. + public TKey Key + { + get { return _key; } + } + + int ICollection.Count + { + get { return _count; } + } + + bool ICollection.IsReadOnly + { + get { return true; } + } + + void ICollection.Add(TElement item) + { + throw new NotSupportedException(Strings.NOT_SUPPORTED); + } + + void ICollection.Clear() + { + throw new NotSupportedException(Strings.NOT_SUPPORTED); + } + + bool ICollection.Contains(TElement item) + { + return Array.IndexOf(_elements, item, 0, _count) >= 0; + } + + void ICollection.CopyTo(TElement[] array, int arrayIndex) + { + Array.Copy(_elements, 0, array, arrayIndex, _count); + } + + bool ICollection.Remove(TElement item) + { + throw new NotSupportedException(Strings.NOT_SUPPORTED); + } + + int IList.IndexOf(TElement item) + { + return Array.IndexOf(_elements, item, 0, _count); + } + + void IList.Insert(int index, TElement item) + { + throw new NotSupportedException(Strings.NOT_SUPPORTED); + } + + void IList.RemoveAt(int index) + { + throw new NotSupportedException(Strings.NOT_SUPPORTED); + } + + TElement IList.this[int index] + { + get + { + if (index < 0 || index >= _count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + + return _elements[index]; + } + + set + { + throw new NotSupportedException(Strings.NOT_SUPPORTED); + } + } + } +} diff --git a/Ix.NET/Source/System.Interactive.Async/Lookup.cs b/Ix.NET/Source/System.Interactive.Async/Lookup.cs new file mode 100644 index 0000000000..e1e6703bc4 --- /dev/null +++ b/Ix.NET/Source/System.Interactive.Async/Lookup.cs @@ -0,0 +1,275 @@ +// 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.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace System.Linq.Internal +{ + internal class Lookup : ILookup + { + private readonly IEqualityComparer _comparer; + private Grouping[] _groupings; + private Grouping _lastGrouping; + private int _count; + + internal static Lookup Create(IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) + { + Debug.Assert(source != null); + Debug.Assert(keySelector != null); + Debug.Assert(elementSelector != null); + + Lookup lookup = new Lookup(comparer); + foreach (TSource item in source) + { + lookup.GetGrouping(keySelector(item), create: true).Add(elementSelector(item)); + } + + return lookup; + } + + internal static async Task> CreateAsync(IAsyncEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + Debug.Assert(source != null); + Debug.Assert(keySelector != null); + Debug.Assert(elementSelector != null); + + Lookup lookup = new Lookup(comparer); + using (var enu = source.GetEnumerator()) + { + while (await enu.MoveNext(cancellationToken) + .ConfigureAwait(false)) + { + lookup.GetGrouping(keySelector(enu.Current), create: true).Add(elementSelector(enu.Current)); + } + } + + return lookup; + } + + internal static Lookup Create(IEnumerable source, Func keySelector, IEqualityComparer comparer) + { + Debug.Assert(source != null); + Debug.Assert(keySelector != null); + + Lookup lookup = new Lookup(comparer); + foreach (TElement item in source) + { + lookup.GetGrouping(keySelector(item), create: true).Add(item); + } + + return lookup; + } + + internal static Lookup CreateForJoin(IEnumerable source, Func keySelector, IEqualityComparer comparer) + { + Lookup lookup = new Lookup(comparer); + foreach (TElement item in source) + { + TKey key = keySelector(item); + if (key != null) + { + lookup.GetGrouping(key, create: true).Add(item); + } + } + + return lookup; + } + + internal static async Task> CreateForJoinAsync(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer, CancellationToken cancellationToken) + { + Lookup lookup = new Lookup(comparer); + using (var enu = source.GetEnumerator()) + { + while (await enu.MoveNext(cancellationToken) + .ConfigureAwait(false)) + { + TKey key = keySelector(enu.Current); + if (key != null) + { + lookup.GetGrouping(key, create: true).Add(enu.Current); + } + } + } + + return lookup; + } + + private Lookup(IEqualityComparer comparer) + { + _comparer = comparer ?? EqualityComparer.Default; + _groupings = new Grouping[7]; + } + + public int Count + { + get { return _count; } + } + + public IEnumerable this[TKey key] + { + get + { + Grouping grouping = GetGrouping(key, create: false); + if (grouping != null) + { + return grouping; + } + +#if NO_ARRAY_EMPTY + return EmptyArray.Value; +#else + return Array.Empty(); +#endif + } + } + + public bool Contains(TKey key) + { + return GetGrouping(key, create: false) != null; + } + + public IEnumerator> GetEnumerator() + { + Grouping g = _lastGrouping; + if (g != null) + { + do + { + g = g._next; + yield return g; + } + while (g != _lastGrouping); + } + } + + internal TResult[] ToArray(Func, TResult> resultSelector) + { + TResult[] array = new TResult[_count]; + int index = 0; + Grouping g = _lastGrouping; + if (g != null) + { + do + { + g = g._next; + g.Trim(); + array[index] = resultSelector(g._key, g._elements); + ++index; + } + while (g != _lastGrouping); + } + + return array; + } + + + internal List ToList(Func, TResult> resultSelector) + { + List list = new List(_count); + Grouping g = _lastGrouping; + if (g != null) + { + do + { + g = g._next; + g.Trim(); + list.Add(resultSelector(g._key, g._elements)); + } + while (g != _lastGrouping); + } + + return list; + } + + public IEnumerable ApplyResultSelector(Func, TResult> resultSelector) + { + Grouping g = _lastGrouping; + if (g != null) + { + do + { + g = g._next; + g.Trim(); + yield return resultSelector(g._key, g._elements); + } + while (g != _lastGrouping); + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + internal int InternalGetHashCode(TKey key) + { + // Handle comparer implementations that throw when passed null + return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF; + } + + internal Grouping GetGrouping(TKey key, bool create) + { + int hashCode = InternalGetHashCode(key); + for (Grouping g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext) + { + if (g._hashCode == hashCode && _comparer.Equals(g._key, key)) + { + return g; + } + } + + if (create) + { + if (_count == _groupings.Length) + { + Resize(); + } + + int index = hashCode % _groupings.Length; + Grouping g = new Grouping(); + g._key = key; + g._hashCode = hashCode; + g._elements = new TElement[1]; + g._hashNext = _groupings[index]; + _groupings[index] = g; + if (_lastGrouping == null) + { + g._next = g; + } + else + { + g._next = _lastGrouping._next; + _lastGrouping._next = g; + } + + _lastGrouping = g; + _count++; + return g; + } + + return null; + } + + private void Resize() + { + int newSize = checked((_count * 2) + 1); + Grouping[] newGroupings = new Grouping[newSize]; + Grouping g = _lastGrouping; + do + { + g = g._next; + int index = g._hashCode % newSize; + g._hashNext = newGroupings[index]; + newGroupings[index] = g; + } + while (g != _lastGrouping); + + _groupings = newGroupings; + } + } + +} diff --git a/Ix.NET/Source/System.Interactive.Async/Strings.cs b/Ix.NET/Source/System.Interactive.Async/Strings.cs index 3578ff1f8b..1fbcb54807 100644 --- a/Ix.NET/Source/System.Interactive.Async/Strings.cs +++ b/Ix.NET/Source/System.Interactive.Async/Strings.cs @@ -7,5 +7,6 @@ internal static class Strings { public static string NO_ELEMENTS = "Source sequence doesn't contain any elements."; public static string MORE_THAN_ONE_ELEMENT = "Source sequence contains more than one element."; + public static string NOT_SUPPORTED = "Specified method is not supported."; } } diff --git a/Ix.NET/Source/System.Interactive.Async/TaskExt.cs b/Ix.NET/Source/System.Interactive.Async/TaskExt.cs index 009a8024bd..cd584fbda4 100644 --- a/Ix.NET/Source/System.Interactive.Async/TaskExt.cs +++ b/Ix.NET/Source/System.Interactive.Async/TaskExt.cs @@ -1,30 +1,15 @@ // 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.Collections.Generic; -using System.Linq; namespace System.Threading.Tasks { static class TaskExt { - public static readonly Task True; - public static readonly Task False; - - static TaskExt() - { - True = Return(true); - False = Return(false); - } - - public static Task Return(T value) - { - var tcs = new TaskCompletionSource(); - tcs.TrySetResult(value); - return tcs.Task; - } - + public static readonly Task True = Task.FromResult(true); + public static readonly Task False = Task.FromResult(false); + public static Task Throw(Exception exception) { var tcs = new TaskCompletionSource(); @@ -32,98 +17,8 @@ public static Task Throw(Exception exception) return tcs.Task; } - public static void Handle(this Task task, TaskCompletionSource tcs, Action success) - { - if (task.IsFaulted) - tcs.TrySetException(task.Exception.InnerExceptions); - else if (task.IsCanceled) - tcs.TrySetCanceled(); - else if (task.IsCompleted) - success(task.Result); - } - - public static void Handle(this Task task, TaskCompletionSource tcs, Action success, Action error) - { - if (task.IsFaulted) - error(task.Exception); - else if (task.IsCanceled) - tcs.TrySetCanceled(); - else if (task.IsCompleted) - success(task.Result); - } - - public static void Handle(this Task task, TaskCompletionSource tcs, Action success, Action error, Action canceled) - { - if (task.IsFaulted) - error(task.Exception); - else if (task.IsCanceled) - canceled(); - else if (task.IsCompleted) - success(task.Result); - } - - public static Task Then(this Task task, Action> continuation) - { - // - // Central location to deal with continuations; allows for experimentation with flags. - // Note that right now, we don't go for synchronous execution. Users can block on the - // task returned from MoveNext, which can cause deadlocks (e.g. typical uses of GroupBy - // involve some aggregate). We'd need deeper asynchrony to make this work with less - // spawning of tasks. - // - return task.ContinueWith(continuation); - } - - public static Task Then(this Task task, Func, R> continuation) - { - // - // See comment on Then for rationale. - // - return task.ContinueWith(continuation); - } - - public static Task UsingEnumerator(this Task task, IDisposable disposable) - { - return task.Finally(() => - { - if (task.IsFaulted || task.IsCanceled || !task.Result) - disposable.Dispose(); - }); - } - - public static Task Finally(this Task task, Action action) - { - var tcs = new TaskCompletionSource(); - - task.ContinueWith(t => - { - try - { - action(); - } - finally - { - switch (t.Status) - { - case TaskStatus.Canceled: - tcs.SetCanceled(); - break; - case TaskStatus.Faulted: - tcs.SetException(t.Exception.InnerException); - break; - case TaskStatus.RanToCompletion: - tcs.SetResult(t.Result); - break; - } - } - }, TaskContinuationOptions.ExecuteSynchronously); - - return tcs.Task; - } - public static Task Zip(this Task t1, Task t2, Func f) { - var gate = new object(); var tcs = new TaskCompletionSource(); var i = 2; diff --git a/Ix.NET/Source/System.Interactive.Async/project.json b/Ix.NET/Source/System.Interactive.Async/project.json index a09ce4ec6a..df3ca529ee 100644 --- a/Ix.NET/Source/System.Interactive.Async/project.json +++ b/Ix.NET/Source/System.Interactive.Async/project.json @@ -33,12 +33,33 @@ "define": [ "HAS_AWAIT", "HAS_APTCA", + "NO_ARRAY_EMPTY", "DESKTOPCLR", "DESKTOPCLR45" ] } }, + "net46": { + "buildOptions": { + "define": [ + "HAS_AWAIT", + "HAS_APTCA", + "DESKTOPCLR", + "DESKTOPCLR46" + ] + } + }, "netstandard1.0": { + "buildOptions": { + "define": [ + "HAS_AWAIT", + "NO_ARRAY_EMPTY", + "CRIPPLED_REFLECTION", + "PLIB" + ] + } + }, + "netstandard1.3": { "buildOptions": { "define": [ "HAS_AWAIT", diff --git a/Ix.NET/Source/Tests/AsyncTests.Aggregates.cs b/Ix.NET/Source/Tests/AsyncTests.Aggregates.cs index a38c738597..92ceef1c15 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Aggregates.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Aggregates.cs @@ -6,37 +6,42 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using Xunit; using System.Collections; using System.Threading; +using System.Threading.Tasks; +// ReSharper disable InvokeAsExtensionMethod +// ReSharper disable InconsistentNaming +// ReSharper disable RedundantTypeArgumentsOfMethod namespace Tests { public partial class AsyncTests { + private const int WaitTimeoutMs = 5000; + [Fact] - public void Aggregate_Null() + public async Task Aggregate_Null() { - AssertThrows(() => AsyncEnumerable.Aggregate(null, (x, y) => x + y)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(null, (x, y) => x + y)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null)); - AssertThrows(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y, z => z)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null, z => z)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, (x, y) => x + y, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y, z => z)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null, z => z)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, (x, y) => x + y, null)); - AssertThrows(() => AsyncEnumerable.Aggregate(null, (x, y) => x + y, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(null, (x, y) => x + y, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y, z => z, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null, z => z, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, (x, y) => x + y, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(null, 0, (x, y) => x + y, z => z, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, null, z => z, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Aggregate(AsyncEnumerable.Return(42), 0, (x, y) => x + y, null, CancellationToken.None)); } [Fact] @@ -52,7 +57,7 @@ public void Aggregate2() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.Aggregate((x, y) => x * y); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -61,7 +66,7 @@ public void Aggregate3() var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Aggregate((x, y) => x * y); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -70,7 +75,7 @@ public void Aggregate4() var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Aggregate((x, y) => { throw ex; }); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -95,7 +100,7 @@ public void Aggregate7() var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Aggregate(1, (x, y) => x * y); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -104,7 +109,7 @@ public void Aggregate8() var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Aggregate(1, (x, y) => { throw ex; }); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -129,7 +134,7 @@ public void Aggregate11() var ex = new Exception("Bang!"); var xs = AsyncEnumerable.Throw(ex); var ys = xs.Aggregate(1, (x, y) => x * y, x => x + 1); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -138,7 +143,7 @@ public void Aggregate12() var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Aggregate(1, (x, y) => { throw ex; }, x => x + 1); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -147,19 +152,19 @@ public void Aggregate13() var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.Aggregate(1, (x, y) => x * y, x => { throw ex; }); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void Count_Null() + public async Task Count_Null() { - AssertThrows(() => AsyncEnumerable.Count(null)); - AssertThrows(() => AsyncEnumerable.Count(null, x => true)); - AssertThrows(() => AsyncEnumerable.Count(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Count(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Count(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Count(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.Count(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Count(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Count(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Count(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Count(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Count(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -167,7 +172,7 @@ public void Count1() { Assert.Equal(new int[0].ToAsyncEnumerable().Count().Result, 0); Assert.Equal(new[] { 1, 2, 3 }.ToAsyncEnumerable().Count().Result, 3); - AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).Count().Wait()); + AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).Count().Wait(WaitTimeoutMs)); } [Fact] @@ -175,7 +180,7 @@ public void Count2() { Assert.Equal(new int[0].ToAsyncEnumerable().Count(x => x < 3).Result, 0); Assert.Equal(new[] { 1, 2, 3 }.ToAsyncEnumerable().Count(x => x < 3).Result, 2); - AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).Count(x => x < 3).Wait()); + AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).Count(x => x < 3).Wait(WaitTimeoutMs)); } [Fact] @@ -183,19 +188,19 @@ public void Count3() { var ex = new Exception("Bang!"); var ys = new[] { 1, 2, 3 }.ToAsyncEnumerable().Count(x => { throw ex; }); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void LongCount_Null() + public async Task LongCount_Null() { - AssertThrows(() => AsyncEnumerable.LongCount(null)); - AssertThrows(() => AsyncEnumerable.LongCount(null, x => true)); - AssertThrows(() => AsyncEnumerable.LongCount(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LongCount(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LongCount(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LongCount(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.LongCount(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.LongCount(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.LongCount(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LongCount(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LongCount(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LongCount(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -203,7 +208,7 @@ public void LongCount1() { Assert.Equal(new int[0].ToAsyncEnumerable().LongCount().Result, 0); Assert.Equal(new[] { 1, 2, 3 }.ToAsyncEnumerable().LongCount().Result, 3); - AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).LongCount().Wait()); + AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).LongCount().Wait(WaitTimeoutMs)); } [Fact] @@ -211,7 +216,7 @@ public void LongCount2() { Assert.Equal(new int[0].ToAsyncEnumerable().LongCount(x => x < 3).Result, 0); Assert.Equal(new[] { 1, 2, 3 }.ToAsyncEnumerable().LongCount(x => x < 3).Result, 2); - AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).LongCount(x => x < 3).Wait()); + AssertThrows(() => AsyncEnumerable.Throw(new Exception("Bang!")).LongCount(x => x < 3).Wait(WaitTimeoutMs)); } [Fact] @@ -219,17 +224,17 @@ public void LongCount3() { var ex = new Exception("Bang!"); var ys = new[] { 1, 2, 3 }.ToAsyncEnumerable().LongCount(x => { throw ex; }); - AssertThrows(() => ys.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void All_Null() + public async Task All_Null() { - AssertThrows(() => AsyncEnumerable.All(null, x => true)); - AssertThrows(() => AsyncEnumerable.All(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.All(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.All(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.All(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.All(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.All(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.All(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -251,7 +256,7 @@ public void All3() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).All(x => x % 2 == 0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -259,19 +264,19 @@ public void All4() { var ex = new Exception("Bang!"); var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().All(x => { throw ex; }); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void Any_Null() + public async Task Any_Null() { - AssertThrows(() => AsyncEnumerable.Any(null)); - AssertThrows(() => AsyncEnumerable.Any(null, x => true)); - AssertThrows(() => AsyncEnumerable.Any(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Any(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Any(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Any(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.Any(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Any(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Any(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Any(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Any(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Any(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -293,7 +298,7 @@ public void Any3() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).Any(x => x % 2 == 0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -301,7 +306,7 @@ public void Any4() { var ex = new Exception("Bang!"); var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().Any(x => { throw ex; }); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -319,15 +324,15 @@ public void Any6() } [Fact] - public void Contains_Null() + public async Task Contains_Null() { - AssertThrows(() => AsyncEnumerable.Contains(null, 42)); - AssertThrows(() => AsyncEnumerable.Contains(null, 42, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.Contains(AsyncEnumerable.Return(42), 42, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Contains(null, 42)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Contains(null, 42, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Contains(AsyncEnumerable.Return(42), 42, null)); - AssertThrows(() => AsyncEnumerable.Contains(null, 42, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Contains(null, 42, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Contains(AsyncEnumerable.Return(42), 42, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Contains(null, 42, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Contains(null, 42, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Contains(AsyncEnumerable.Return(42), 42, null, CancellationToken.None)); } [Fact] @@ -376,36 +381,36 @@ public int GetHashCode(int obj) } [Fact] - public void First_Null() + public async Task First_Null() { - AssertThrows(() => AsyncEnumerable.First(null)); - AssertThrows(() => AsyncEnumerable.First(null, x => true)); - AssertThrows(() => AsyncEnumerable.First(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.First(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.First(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.First(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.First(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.First(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.First(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.First(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.First(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.First(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] public void First1() { var res = AsyncEnumerable.Empty().First(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void First2() { var res = AsyncEnumerable.Empty().First(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void First3() { var res = AsyncEnumerable.Return(42).First(x => x % 2 != 0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -427,7 +432,7 @@ public void First6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).First(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -435,7 +440,7 @@ public void First7() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).First(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -453,15 +458,15 @@ public void First9() } [Fact] - public void FirstOrDefault_Null() + public async Task FirstOrDefault_Null() { - AssertThrows(() => AsyncEnumerable.FirstOrDefault(null)); - AssertThrows(() => AsyncEnumerable.FirstOrDefault(null, x => true)); - AssertThrows(() => AsyncEnumerable.FirstOrDefault(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.FirstOrDefault(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.FirstOrDefault(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.FirstOrDefault(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.FirstOrDefault(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.FirstOrDefault(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.FirstOrDefault(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.FirstOrDefault(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.FirstOrDefault(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.FirstOrDefault(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -504,7 +509,7 @@ public void FirstOrDefault6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).FirstOrDefault(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -512,7 +517,7 @@ public void FirstOrDefault7() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).FirstOrDefault(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -537,36 +542,36 @@ public void FirstOrDefault10() } [Fact] - public void Last_Null() + public async Task Last_Null() { - AssertThrows(() => AsyncEnumerable.Last(null)); - AssertThrows(() => AsyncEnumerable.Last(null, x => true)); - AssertThrows(() => AsyncEnumerable.Last(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Last(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Last(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Last(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.Last(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Last(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Last(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Last(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Last(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Last(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] public void Last1() { var res = AsyncEnumerable.Empty().Last(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void Last2() { var res = AsyncEnumerable.Empty().Last(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void Last3() { var res = AsyncEnumerable.Return(42).Last(x => x % 2 != 0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -588,7 +593,7 @@ public void Last6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).Last(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -596,7 +601,7 @@ public void Last7() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).Last(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -614,15 +619,15 @@ public void Last9() } [Fact] - public void LastOrDefault_Null() + public async Task LastOrDefault_Null() { - AssertThrows(() => AsyncEnumerable.LastOrDefault(null)); - AssertThrows(() => AsyncEnumerable.LastOrDefault(null, x => true)); - AssertThrows(() => AsyncEnumerable.LastOrDefault(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LastOrDefault(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LastOrDefault(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LastOrDefault(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.LastOrDefault(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.LastOrDefault(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.LastOrDefault(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LastOrDefault(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LastOrDefault(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.LastOrDefault(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -665,7 +670,7 @@ public void LastOrDefault6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).LastOrDefault(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -673,7 +678,7 @@ public void LastOrDefault7() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).LastOrDefault(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -698,36 +703,36 @@ public void LastOrDefault10() } [Fact] - public void Single_Null() + public async Task Single_Null() { - AssertThrows(() => AsyncEnumerable.Single(null)); - AssertThrows(() => AsyncEnumerable.Single(null, x => true)); - AssertThrows(() => AsyncEnumerable.Single(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Single(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Single(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Single(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.Single(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Single(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Single(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Single(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Single(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Single(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] public void Single1() { var res = AsyncEnumerable.Empty().Single(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void Single2() { var res = AsyncEnumerable.Empty().Single(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void Single3() { var res = AsyncEnumerable.Return(42).Single(x => x % 2 != 0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -749,7 +754,7 @@ public void Single6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).Single(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -757,14 +762,14 @@ public void Single7() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).Single(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void Single8() { var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().Single(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -778,19 +783,19 @@ public void Single9() public void Single10() { var res = new[] { 42, 23, 45, 90 }.ToAsyncEnumerable().Single(x => x % 2 != 0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] - public void SingleOrDefault_Null() + public async Task SingleOrDefault_Null() { - AssertThrows(() => AsyncEnumerable.SingleOrDefault(null)); - AssertThrows(() => AsyncEnumerable.SingleOrDefault(null, x => true)); - AssertThrows(() => AsyncEnumerable.SingleOrDefault(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SingleOrDefault(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SingleOrDefault(null, x => true)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SingleOrDefault(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.SingleOrDefault(null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.SingleOrDefault(null, x => true, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.SingleOrDefault(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SingleOrDefault(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SingleOrDefault(null, x => true, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SingleOrDefault(AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -833,7 +838,7 @@ public void SingleOrDefault6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).SingleOrDefault(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -841,14 +846,14 @@ public void SingleOrDefault7() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).SingleOrDefault(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void SingleOrDefault8() { var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().SingleOrDefault(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -869,24 +874,24 @@ public void SingleOrDefault10() public void SingleOrDefault11() { var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().SingleOrDefault(x => true); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] - public void ElementAt_Null() + public async Task ElementAt_Null() { - AssertThrows(() => AsyncEnumerable.ElementAt(null, 0)); - AssertThrows(() => AsyncEnumerable.ElementAt(AsyncEnumerable.Return(42), -1)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAt(null, 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAt(AsyncEnumerable.Return(42), -1)); - AssertThrows(() => AsyncEnumerable.ElementAt(null, 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ElementAt(AsyncEnumerable.Return(42), -1, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAt(null, 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAt(AsyncEnumerable.Return(42), -1, CancellationToken.None)); } [Fact] public void ElementAt1() { var res = AsyncEnumerable.Empty().ElementAt(0); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentOutOfRangeException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentOutOfRangeException); } [Fact] @@ -900,7 +905,7 @@ public void ElementAt2() public void ElementAt3() { var res = AsyncEnumerable.Return(42).ElementAt(1); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentOutOfRangeException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentOutOfRangeException); } [Fact] @@ -914,7 +919,7 @@ public void ElementAt4() public void ElementAt5() { var res = new[] { 1, 42, 3 }.ToAsyncEnumerable().ElementAt(7); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentOutOfRangeException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentOutOfRangeException); } [Fact] @@ -922,17 +927,17 @@ public void ElementAt6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).ElementAt(15); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void ElementAtOrDefault_Null() + public async Task ElementAtOrDefault_Null() { - AssertThrows(() => AsyncEnumerable.ElementAtOrDefault(null, 0)); - AssertThrows(() => AsyncEnumerable.ElementAtOrDefault(AsyncEnumerable.Return(42), -1)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAtOrDefault(null, 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAtOrDefault(AsyncEnumerable.Return(42), -1)); - AssertThrows(() => AsyncEnumerable.ElementAtOrDefault(null, 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ElementAtOrDefault(AsyncEnumerable.Return(42), -1, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAtOrDefault(null, 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ElementAtOrDefault(AsyncEnumerable.Return(42), -1, CancellationToken.None)); } [Fact] @@ -975,14 +980,14 @@ public void ElementAtOrDefault6() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).ElementAtOrDefault(15); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void ToList_Null() + public async Task ToList_Null() { - AssertThrows(() => AsyncEnumerable.ToList(null)); - AssertThrows(() => AsyncEnumerable.ToList(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToList(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToList(null, CancellationToken.None)); } [Fact] @@ -1006,14 +1011,14 @@ public void ToList3() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).ToList(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void ToArray_Null() + public async Task ToArray_Null() { - AssertThrows(() => AsyncEnumerable.ToArray(null)); - AssertThrows(() => AsyncEnumerable.ToArray(null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToArray(null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToArray(null, CancellationToken.None)); } [Fact] @@ -1037,43 +1042,43 @@ public void ToArray3() { var ex = new Exception("Bang!"); var res = AsyncEnumerable.Throw(ex).ToArray(); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void ToDictionary_Null() + public async Task ToDictionary_Null() { - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, x => 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, x => 0, null)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, x => 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(null, x => 0, x => 0, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, x => 0, null, CancellationToken.None)); } [Fact] @@ -1089,7 +1094,7 @@ public void ToDictionary1() public void ToDictionary2() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); - AssertThrows(() => xs.ToDictionary(x => x % 2).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); + AssertThrows(() => xs.ToDictionary(x => x % 2).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); } [Fact] @@ -1105,7 +1110,7 @@ public void ToDictionary3() public void ToDictionary4() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); - AssertThrows(() => xs.ToDictionary(x => x % 2, x => x + 1).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); + AssertThrows(() => xs.ToDictionary(x => x % 2, x => x + 1).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); } [Fact] @@ -1121,7 +1126,7 @@ public void ToDictionary5() public void ToDictionary6() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); - AssertThrows(() => xs.ToDictionary(x => x % 2, new Eq()).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); + AssertThrows(() => xs.ToDictionary(x => x % 2, new Eq()).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); } [Fact] @@ -1134,39 +1139,39 @@ public void ToDictionary7() } [Fact] - public void ToLookup_Null() + public async Task ToLookup_Null() { - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, x => 0, null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, x => 0, null)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, x => 0, null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(null, x => 0, x => 0, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), null, x => 0, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, null, EqualityComparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, x => 0, null, CancellationToken.None)); } [Fact] @@ -1276,73 +1281,73 @@ public void ToLookup9() } [Fact] - public void Average_Null() - { - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); - - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); - - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); - - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + public async Task Average_Null() + { + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(default(IAsyncEnumerable), x => x, CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Average(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); } [Fact] @@ -1440,7 +1445,7 @@ public void Average11() { var xs = new int[0]; var ys = xs.ToAsyncEnumerable(); - AssertThrows(() => ys.Average().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => ys.Average().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -1456,7 +1461,7 @@ public void Average13() { var xs = new long[0]; var ys = xs.ToAsyncEnumerable(); - AssertThrows(() => ys.Average().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => ys.Average().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -1472,7 +1477,7 @@ public void Average15() { var xs = new double[0]; var ys = xs.ToAsyncEnumerable(); - AssertThrows(() => ys.Average().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => ys.Average().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -1488,7 +1493,7 @@ public void Average17() { var xs = new float[0]; var ys = xs.ToAsyncEnumerable(); - AssertThrows(() => ys.Average().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => ys.Average().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -1504,7 +1509,7 @@ public void Average19() { var xs = new decimal[0]; var ys = xs.ToAsyncEnumerable(); - AssertThrows(() => ys.Average().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => ys.Average().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -1516,87 +1521,87 @@ public void Average20() } [Fact] - public void Min_Null() - { - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), Comparer.Default)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(IComparer))); - - AssertThrows(() => AsyncEnumerable.Min(default(IAsyncEnumerable), Comparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(IComparer), CancellationToken.None)); + public async Task Min_Null() + { + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), Comparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(IComparer))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(default(IAsyncEnumerable), Comparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Min(AsyncEnumerable.Empty(), default(IComparer), CancellationToken.None)); } [Fact] @@ -1699,87 +1704,87 @@ public void Min11() } [Fact] - public void Max_Null() - { - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), Comparer.Default)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(IComparer))); - - AssertThrows(() => AsyncEnumerable.Max(default(IAsyncEnumerable), Comparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(IComparer), CancellationToken.None)); + public async Task Max_Null() + { + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), Comparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(IComparer))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(default(IAsyncEnumerable), Comparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Max(AsyncEnumerable.Empty(), default(IComparer), CancellationToken.None)); } [Fact] @@ -1882,73 +1887,73 @@ public void Max11() } [Fact] - public void Sum_Null() - { - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); - - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); - - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); - - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); - - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + public async Task Sum_Null() + { + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func))); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(default(IAsyncEnumerable), x => x, CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.Sum(AsyncEnumerable.Empty(), default(Func), CancellationToken.None)); } [Fact] @@ -2042,21 +2047,21 @@ public void Sum10() } [Fact] - public void MinBy_Null() + public async Task MinBy_Null() { - AssertThrows(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func))); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func))); - AssertThrows(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x, Comparer.Default)); - AssertThrows(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default)); - AssertThrows(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), x => x, default(IComparer))); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x, Comparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), x => x, default(IComparer))); - AssertThrows(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), x => x, default(IComparer), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MinBy(AsyncEnumerable.Return(42), x => x, default(IComparer), CancellationToken.None)); } [Fact] @@ -2073,7 +2078,7 @@ public void MinBy2() { var xs = new int[0].ToAsyncEnumerable().MinBy(x => x / 2); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -2082,7 +2087,7 @@ public void MinBy3() var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => { if (x == 3) throw ex; return x; }); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2091,7 +2096,7 @@ public void MinBy4() var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => { if (x == 4) throw ex; return x; }); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2100,25 +2105,25 @@ public void MinBy5() var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw(ex)).MinBy(x => x, Comparer.Default); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] - public void MaxBy_Null() + public async Task MaxBy_Null() { - AssertThrows(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x)); - AssertThrows(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func))); - - AssertThrows(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x, Comparer.Default)); - AssertThrows(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default)); - AssertThrows(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), x => x, default(IComparer))); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func))); - AssertThrows(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x, Comparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), x => x, default(IComparer))); - AssertThrows(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), x => x, default(IComparer), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); + + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.MaxBy(AsyncEnumerable.Return(42), x => x, default(IComparer), CancellationToken.None)); } [Fact] @@ -2135,7 +2140,7 @@ public void MaxBy2() { var xs = new int[0].ToAsyncEnumerable().MaxBy(x => x / 2); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] @@ -2144,7 +2149,7 @@ public void MaxBy3() var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxBy(x => { if (x == 3) throw ex; return x; }); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2153,7 +2158,7 @@ public void MaxBy4() var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxBy(x => { if (x == 4) throw ex; return x; }); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2162,7 +2167,7 @@ public void MaxBy5() var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw(ex)).MaxBy(x => x, Comparer.Default); - AssertThrows(() => xs.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } } } diff --git a/Ix.NET/Source/Tests/AsyncTests.Bugs.cs b/Ix.NET/Source/Tests/AsyncTests.Bugs.cs index a77f853a75..5a47249d2c 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Bugs.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Bugs.cs @@ -157,7 +157,7 @@ public void CorrectCancel() try { - t.Wait(); + t.Wait(WaitTimeoutMs); } catch { @@ -191,7 +191,7 @@ public void CanCancelMoveNext() try { - t.Wait(); + t.Wait(WaitTimeoutMs); Assert.True(false); } catch diff --git a/Ix.NET/Source/Tests/AsyncTests.Conversions.cs b/Ix.NET/Source/Tests/AsyncTests.Conversions.cs index c137a7a3f7..bee6a767e9 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Conversions.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Conversions.cs @@ -40,7 +40,7 @@ public void ToAsyncEnumerable2() var xs = ToAsyncEnumerable_Sequence(ex).ToAsyncEnumerable(); var e = xs.GetEnumerator(); HasNext(e, 42); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); } private IEnumerable ToAsyncEnumerable_Sequence(Exception e) @@ -95,7 +95,7 @@ public void ToAsyncEnumerable4() Assert.True(subscribed); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); } [Fact] @@ -121,7 +121,7 @@ public void ToAsyncEnumerable_With_Faulted_Task() var xs = tcs.Task.ToAsyncEnumerable(); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); } [Fact] @@ -133,7 +133,7 @@ public void ToAsyncEnumerable_With_Canceled_Task() var xs = tcs.Task.ToAsyncEnumerable(); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() is TaskCanceledException); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() is TaskCanceledException); } class MyObservable : IObservable diff --git a/Ix.NET/Source/Tests/AsyncTests.Creation.cs b/Ix.NET/Source/Tests/AsyncTests.Creation.cs index 127d500fca..1e9e7e755b 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Creation.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Creation.cs @@ -71,7 +71,7 @@ public void Throw() var xs = AsyncEnumerable.Throw(ex); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); AssertThrows(() => Nop(e.Current)); } @@ -207,7 +207,7 @@ public void Generate2() var xs = AsyncEnumerable.Generate(0, x => { throw ex; }, x => x + 1, x => x * x); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); } [Fact] @@ -218,7 +218,7 @@ public void Generate3() var e = xs.GetEnumerator(); HasNext(e, 0); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); } [Fact] @@ -229,7 +229,7 @@ public void Generate4() var e = xs.GetEnumerator(); HasNext(e, 0); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).InnerExceptions.Single() == ex); } [Fact] @@ -354,7 +354,7 @@ public void Using5() var e = xs.GetEnumerator(); Assert.Equal(1, i); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); Assert.True(disposed.Task.Result); } @@ -388,7 +388,7 @@ public void Using6() try { - t.Wait(); + t.Wait(WaitTimeoutMs); } catch (AggregateException ex) { diff --git a/Ix.NET/Source/Tests/AsyncTests.Exceptions.cs b/Ix.NET/Source/Tests/AsyncTests.Exceptions.cs index ac41331130..931e60a4fb 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Exceptions.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Exceptions.cs @@ -114,7 +114,7 @@ public void Catch4() HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); Assert.False(err); } @@ -135,7 +135,7 @@ public void Catch5() HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex2); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex2); } [Fact] @@ -162,7 +162,7 @@ public void Catch6() HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -233,7 +233,7 @@ public void Catch10() HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } private IEnumerable> CatchXss() @@ -259,7 +259,7 @@ public void Catch11() HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -323,7 +323,7 @@ public void Finally3() var e = xs.GetEnumerator(); Assert.False(b); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); Assert.True(b); } @@ -379,7 +379,7 @@ public void Finally6() var t = e.MoveNext(cts.Token); cts.Cancel(); - t.Wait(); + t.Wait(WaitTimeoutMs); Assert.True(b); } @@ -467,7 +467,7 @@ public void OnErrorResumeNext10() HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } private IEnumerable> OnErrorResumeNextXss() diff --git a/Ix.NET/Source/Tests/AsyncTests.Multiple.cs b/Ix.NET/Source/Tests/AsyncTests.Multiple.cs index 58e4f50ab2..8b86235710 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Multiple.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Multiple.cs @@ -9,6 +9,7 @@ using System.Text; using Xunit; using System.Threading; +using System.Threading.Tasks; namespace Tests { @@ -48,7 +49,7 @@ public void Concat2() HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -58,7 +59,7 @@ public void Concat3() var ys = AsyncEnumerable.Throw(ex).Concat(new[] { 4, 5, 6 }.ToAsyncEnumerable()); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -98,7 +99,7 @@ public void Concat5() HasNext(e, 3); HasNext(e, 4); HasNext(e, 5); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -112,7 +113,7 @@ public void Concat6() HasNext(e, 3); HasNext(e, 4); HasNext(e, 5); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } static IEnumerable> ConcatXss() @@ -181,7 +182,7 @@ public void Zip4() var res = xs.Zip(ys, (x, y) => x * y); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -193,7 +194,7 @@ public void Zip5() var res = xs.Zip(ys, (x, y) => x * y); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -205,7 +206,7 @@ public void Zip6() var res = xs.Zip(ys, (x, y) => { if (x > 0) throw ex; return x * y; }); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -324,21 +325,21 @@ public void Except2() } [Fact] - public void SequenceEqual_Null() + public async Task SequenceEqual_Null() { - AssertThrows(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42))); - AssertThrows(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42))); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42), new Eq())); - AssertThrows(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null, new Eq())); - AssertThrows(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42), new Eq())); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null, new Eq())); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null)); - AssertThrows(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42), new Eq(), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null, new Eq(), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(null, AsyncEnumerable.Return(42), new Eq(), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), null, new Eq(), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.SequenceEqual(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null, CancellationToken.None)); } [Fact] @@ -392,7 +393,7 @@ public void SequenceEqual6() var ys = AsyncEnumerable.Throw(ex); var res = xs.SequenceEqual(ys); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -403,7 +404,7 @@ public void SequenceEqual7() var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var res = xs.SequenceEqual(ys); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -457,7 +458,7 @@ public void SequenceEqual13() var ys = AsyncEnumerable.Throw(ex); var res = xs.SequenceEqual(ys, new Eq()); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -468,7 +469,7 @@ public void SequenceEqual14() var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var res = xs.SequenceEqual(ys, new Eq()); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -486,7 +487,7 @@ public void SequenceEqual16() var xs = new[] { 1, 2, -3, 4 }.ToAsyncEnumerable(); var ys = new[] { 1, -2, 3, 4 }.ToAsyncEnumerable(); var res = xs.SequenceEqual(ys, new EqEx()); - AssertThrows(() => res.Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); + AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); } class EqEx : IEqualityComparer @@ -559,7 +560,7 @@ public void GroupJoin3() var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -572,7 +573,7 @@ public void GroupJoin4() var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -585,7 +586,7 @@ public void GroupJoin5() var res = xs.GroupJoin(ys, x => { throw ex; }, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -598,7 +599,7 @@ public void GroupJoin6() var res = xs.GroupJoin(ys, x => x % 3, y => { throw ex; }, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -616,7 +617,7 @@ public void GroupJoin7() var e = res.GetEnumerator(); HasNext(e, "0 - 36"); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -704,7 +705,7 @@ public void Join5() var res = xs.Join(ys, x => x % 3, y => y % 3, (x, y) => x + y); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -717,7 +718,7 @@ public void Join6() var res = xs.Join(ys, x => x % 3, y => y % 3, (x, y) => x + y); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -730,7 +731,7 @@ public void Join7() var res = xs.Join(ys, x => { throw ex; }, y => y, (x, y) => x + y); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -743,7 +744,7 @@ public void Join8() var res = xs.Join(ys, x => x, y => { throw ex; }, (x, y) => x + y); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -756,7 +757,7 @@ public void Join9() var res = xs.Join(ys, x => x, y => y, (x, y) => { throw ex; }); var e = res.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] diff --git a/Ix.NET/Source/Tests/AsyncTests.Single.cs b/Ix.NET/Source/Tests/AsyncTests.Single.cs index fdc5956f26..ab21d77b74 100644 --- a/Ix.NET/Source/Tests/AsyncTests.Single.cs +++ b/Ix.NET/Source/Tests/AsyncTests.Single.cs @@ -9,6 +9,7 @@ using System.Text; using Xunit; using System.Threading; +using System.Threading.Tasks; namespace Tests { @@ -56,7 +57,7 @@ public void Select3() var ys = xs.Select(x => 1 / x); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is DivideByZeroException); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is DivideByZeroException); } [Fact] @@ -66,7 +67,7 @@ public void Select4() var ys = xs.Select((x, i) => 1 / i); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is DivideByZeroException); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is DivideByZeroException); } [Fact] @@ -118,7 +119,7 @@ public void Where3() HasNext(e, 8); HasNext(e, 5); HasNext(e, 7); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -132,7 +133,7 @@ public void Where4() HasNext(e, 8); HasNext(e, 5); HasNext(e, 7); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -143,7 +144,7 @@ public void Where5() var ys = xs.Where(x => true); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -154,7 +155,7 @@ public void Where6() var ys = xs.Where((x, i) => true); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -206,7 +207,7 @@ public void SelectMany2() HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -217,7 +218,7 @@ public void SelectMany3() var ys = xs.SelectMany(x => Enumerable.Range(0, x).ToAsyncEnumerable()); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -237,7 +238,7 @@ public void SelectMany4() HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -273,7 +274,7 @@ public void SelectMany6() HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -284,7 +285,7 @@ public void SelectMany7() var ys = xs.SelectMany((x, i) => Enumerable.Range(0, x).ToAsyncEnumerable()); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -304,7 +305,7 @@ public void SelectMany8() HasNext(e, 0); HasNext(e, 0); HasNext(e, 1); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -356,7 +357,7 @@ public void SelectMany11() HasNext(e, 6); HasNext(e, 8); HasNext(e, 9); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -375,7 +376,7 @@ public void SelectMany12() HasNext(e, 3); HasNext(e, 8); HasNext(e, 10); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -484,7 +485,7 @@ public void Do2() var ys = xs.Do(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -544,11 +545,11 @@ public void Do5() var ys = xs.Do(x => { hasv = true; }, exx => { exa = exx; }, () => { done = true; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ex_.InnerException == ex); Assert.False(hasv); Assert.False(done); - Assert.Same(((AggregateException)exa).Flatten().InnerExceptions.Single(), ex); + Assert.Same(exa, ex); } [Fact] @@ -561,24 +562,24 @@ public void Do6() var ys = xs.Do(x => { hasv = true; }, exx => { exa = exx; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ex_.InnerException == ex); Assert.False(hasv); - Assert.Same(((AggregateException)exa).Flatten().InnerExceptions.Single(), ex); + Assert.Same(exa, ex); } [Fact] - public void ForEachAsync_Null() + public async Task ForEachAsync_Null() { - AssertThrows(() => AsyncEnumerable.ForEachAsync(null, x => { })); - AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action))); - AssertThrows(() => AsyncEnumerable.ForEachAsync(null, (x, i) => { })); - AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action))); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(null, x => { })); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action))); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(null, (x, i) => { })); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action))); - AssertThrows(() => AsyncEnumerable.ForEachAsync(null, x => { }, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ForEachAsync(null, (x, i) => { }, CancellationToken.None)); - AssertThrows(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(null, x => { }, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(null, (x, i) => { }, CancellationToken.None)); + await Assert.ThrowsAsync(() => AsyncEnumerable.ForEachAsync(AsyncEnumerable.Return(42), default(Action), CancellationToken.None)); } [Fact] @@ -601,7 +602,7 @@ public void ForEachAsync1() var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); - xs.ForEachAsync(x => sum += x).Wait(); + xs.ForEachAsync(x => sum += x).Wait(WaitTimeoutMs); Assert.Equal(10, sum); } @@ -631,7 +632,7 @@ public void ForEach2() var sum = 0; var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); - xs.ForEachAsync((x, i) => sum += x * i).Wait(); + xs.ForEachAsync((x, i) => sum += x * i).Wait(WaitTimeoutMs); Assert.Equal(1 * 0 + 2 * 1 + 3 * 2 + 4 * 3, sum); } @@ -641,7 +642,7 @@ public void ForEachAsync3() var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); - AssertThrows(() => xs.ForEachAsync(x => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.ForEachAsync(x => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -659,7 +660,7 @@ public void ForEachAsync4() var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); - AssertThrows(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -677,7 +678,7 @@ public void ForEachAsync5() var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); - AssertThrows(() => xs.ForEachAsync(x => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.ForEachAsync(x => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -695,7 +696,7 @@ public void ForEachAsync6() var ex = new Exception("Bang"); var xs = AsyncEnumerable.Throw(ex); - AssertThrows(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => xs.ForEachAsync((x, i) => { throw ex; }).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -758,7 +759,7 @@ public void Take4() var ys = xs.Take(2); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -826,7 +827,7 @@ public void TakeWhile5() var ys = xs.TakeWhile(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -873,7 +874,7 @@ public void TakeWhile9() var ys = xs.TakeWhile((x, i) => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -927,7 +928,7 @@ public void Skip4() var ys = xs.Skip(2); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -998,7 +999,7 @@ public void SkipWhile5() var ys = xs.SkipWhile(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1045,7 +1046,7 @@ public void SkipWhile9() var ys = xs.SkipWhile((x, i) => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1128,7 +1129,7 @@ public void DefaultIfEmpty7() var xs = AsyncEnumerable.Throw(ex).DefaultIfEmpty(); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1138,7 +1139,7 @@ public void DefaultIfEmpty8() var xs = AsyncEnumerable.Throw(ex).DefaultIfEmpty(24); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1225,7 +1226,7 @@ public void Reverse4() var ys = xs.Reverse(); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1282,7 +1283,7 @@ public void OrderBy2() var ys = xs.OrderBy(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1293,7 +1294,7 @@ public void ThenBy2() var ys = xs.OrderBy(x => x).ThenBy(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1316,7 +1317,7 @@ public void OrderByDescending2() var ys = xs.OrderByDescending(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1327,7 +1328,7 @@ public void ThenByDescending2() var ys = xs.OrderBy(x => x).ThenByDescending(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1578,7 +1579,7 @@ public void GroupBy4() var ys = xs.GroupBy(x => x); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1601,9 +1602,9 @@ public void GroupBy5() var g2e = g2.GetEnumerator(); HasNext(g2e, 43); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); - AssertThrows(() => g1e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); - AssertThrows(() => g2e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => g1e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => g2e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } [Fact] @@ -1619,9 +1620,9 @@ public void GroupBy6() Assert.Equal(g1.Key, 42); var g1e = g1.GetEnumerator(); HasNext(g1e, 42); - AssertThrows(() => g1e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => g1e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!"); } static IEnumerable GetXs() @@ -1639,7 +1640,7 @@ public void GroupBy7() var ys = xs.GroupBy(x => { throw ex; }); var e = ys.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1663,9 +1664,9 @@ public void GroupBy8() var g2e = g2.GetEnumerator(); HasNext(g2e, 2); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); - AssertThrows(() => g1e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); - AssertThrows(() => g2e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => g1e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => g2e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -1941,7 +1942,7 @@ public int GetHashCode(int obj) [Fact] public void AsAsyncEnumerable_Null() { - AssertThrows(() => AsyncEnumerable.AsAsyncEnumerable(null)); + AssertThrows(() => AsyncEnumerable.AsAsyncEnumerable((IAsyncEnumerable)null)); } [Fact] @@ -2032,7 +2033,7 @@ public void RepeatSeq4() var xs = new FailRepeat().ToAsyncEnumerable().Repeat(); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); } [Fact] @@ -2041,7 +2042,7 @@ public void RepeatSeq5() var xs = new FailRepeat().ToAsyncEnumerable().Repeat(3); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException); } class FailRepeat : IEnumerable @@ -2103,7 +2104,7 @@ public void IgnoreElements4() var xs = AsyncEnumerable.Throw(ex).IgnoreElements(); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2145,7 +2146,7 @@ public void StartWith3() var e = xs.GetEnumerator(); HasNext(e, 1); HasNext(e, 2); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2271,7 +2272,7 @@ public void DistinctUntilChanged3() HasNext(e, 1); HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2304,7 +2305,7 @@ public void Expand2() var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => { throw ex; }); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2315,7 +2316,7 @@ public void Expand3() var e = xs.GetEnumerator(); HasNext(e, 2); HasNext(e, 3); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NullReferenceException); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NullReferenceException); } [Fact] @@ -2358,7 +2359,7 @@ public void Scan3() var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, (x, y) => { throw ex; }); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2368,7 +2369,7 @@ public void Scan4() var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan((x, y) => { throw ex; }); var e = xs.GetEnumerator(); - AssertThrows(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); + AssertThrows(() => e.MoveNext().Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] @@ -2432,7 +2433,7 @@ public void TakeLast_BugFix_TakeLast_Zero_TakesForever() new int[] { 1, 2, 3, 4 }.ToAsyncEnumerable() .TakeLast(0) .ForEachAsync(_ => { isSet = true; }) - .Wait(); + .Wait(WaitTimeoutMs); Assert.False(isSet); diff --git a/Ix.NET/Source/Tests/AsyncTests.cs b/Ix.NET/Source/Tests/AsyncTests.cs index c7fb76c35e..5aa5fa9491 100644 --- a/Ix.NET/Source/Tests/AsyncTests.cs +++ b/Ix.NET/Source/Tests/AsyncTests.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Xunit; namespace Tests @@ -19,6 +20,13 @@ public void AssertThrows(Action a) Assert.Throws(a); } + [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)] + public Task AssertThrows(Func func) + where E : Exception + { + return Assert.ThrowsAsync(func); + } + public void AssertThrows(Action a, Func assert) where E : Exception {