1
0

AsyncEnumerableTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using FluentAssertions;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class AsyncEnumerableTests
  13. {
  14. protected static readonly IAsyncEnumerable<int> Return42 = new[] { 42 }.ToAsyncEnumerable();
  15. private static Func<Exception, bool> SingleInnerExceptionMatches(Exception ex) => e => ((AggregateException)e).Flatten().InnerExceptions.Single() == ex;
  16. private const int WaitTimeoutMs = 5000;
  17. #pragma warning disable xUnit1013 // Public method should be marked as test
  18. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  19. where E : Exception
  20. {
  21. var hasFailed = false;
  22. try
  23. {
  24. a();
  25. }
  26. catch (E e)
  27. {
  28. Assert.True(assert(e));
  29. hasFailed = true;
  30. }
  31. if (!hasFailed)
  32. {
  33. Assert.True(false);
  34. }
  35. }
  36. public void AssertThrowsAsync<TException>(Task t)
  37. {
  38. AssertThrows<AggregateException>(() => t.Wait(WaitTimeoutMs), ex => ex.Flatten().InnerExceptions.Single() is TException);
  39. }
  40. public void AssertThrowsAsync(Task t, Exception e)
  41. {
  42. AssertThrows(() => t.Wait(WaitTimeoutMs), SingleInnerExceptionMatches(e));
  43. }
  44. public void AssertThrowsAsync<T>(ValueTask<T> t, Exception e)
  45. {
  46. AssertThrows(() => t.Wait(WaitTimeoutMs), SingleInnerExceptionMatches(e));
  47. }
  48. public async Task NoNextAsync<T>(IAsyncEnumerator<T> e)
  49. {
  50. Assert.False(await e.MoveNextAsync());
  51. }
  52. public async Task HasNextAsync<T>(IAsyncEnumerator<T> e, T value)
  53. {
  54. Assert.True(await e.MoveNextAsync());
  55. Assert.Equal(value, e.Current);
  56. }
  57. public async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
  58. {
  59. var en1 = enumerable.GetAsyncEnumerator();
  60. var en2 = enumerable.GetAsyncEnumerator();
  61. Assert.Equal(en1.GetType(), en2.GetType());
  62. await en1.DisposeAsync();
  63. await en2.DisposeAsync();
  64. var e1t = enumerable.ToList();
  65. var e2t = enumerable.ToList();
  66. await Task.WhenAll(e1t, e2t);
  67. var e1Result = e1t.Result;
  68. var e2Result = e2t.Result;
  69. e1Result.ShouldAllBeEquivalentTo(e2Result);
  70. }
  71. #pragma warning restore xUnit1013 // Public method should be marked as test
  72. protected static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  73. {
  74. if (exception == null)
  75. throw new ArgumentNullException(nameof(exception));
  76. #if NO_TASK_FROMEXCEPTION
  77. var tcs = new TaskCompletionSource<bool>();
  78. tcs.TrySetException(exception);
  79. var moveNextThrows = new ValueTask<bool>(tcs.Task);
  80. #else
  81. var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
  82. #endif
  83. return AsyncEnumerable.CreateEnumerable(
  84. _ => AsyncEnumerable.CreateEnumerator<TValue>(
  85. () => moveNextThrows,
  86. current: null,
  87. dispose: null)
  88. );
  89. }
  90. }
  91. }