1
0

AsyncEnumerableTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 AssertThrowsAsync<TException>(Task t)
  19. {
  20. AssertThrows<AggregateException>(() => t.Wait(WaitTimeoutMs), ex => ex.Flatten().InnerExceptions.Single() is TException);
  21. }
  22. public void AssertThrowsAsync(Task t, Exception e)
  23. {
  24. AssertThrows(() => t.Wait(WaitTimeoutMs), SingleInnerExceptionMatches(e));
  25. }
  26. public void AssertThrowsAsync<T>(ValueTask<T> t, Exception e)
  27. {
  28. AssertThrows(() => t.Wait(WaitTimeoutMs), SingleInnerExceptionMatches(e));
  29. }
  30. public async Task NoNextAsync<T>(IAsyncEnumerator<T> e)
  31. {
  32. Assert.False(await e.MoveNextAsync());
  33. }
  34. public async Task HasNextAsync<T>(IAsyncEnumerator<T> e, T value)
  35. {
  36. Assert.True(await e.MoveNextAsync());
  37. Assert.Equal(value, e.Current);
  38. }
  39. public async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
  40. {
  41. var en1 = enumerable.GetAsyncEnumerator();
  42. var en2 = enumerable.GetAsyncEnumerator();
  43. Assert.Equal(en1.GetType(), en2.GetType());
  44. await en1.DisposeAsync();
  45. await en2.DisposeAsync();
  46. var res1 = await enumerable.ToList();
  47. var res2 = await enumerable.ToList();
  48. res1.ShouldAllBeEquivalentTo(res2);
  49. }
  50. #pragma warning restore xUnit1013 // Public method should be marked as test
  51. protected static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  52. {
  53. if (exception == null)
  54. throw new ArgumentNullException(nameof(exception));
  55. #if NO_TASK_FROMEXCEPTION
  56. var tcs = new TaskCompletionSource<bool>();
  57. tcs.TrySetException(exception);
  58. var moveNextThrows = new ValueTask<bool>(tcs.Task);
  59. #else
  60. var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
  61. #endif
  62. return AsyncEnumerable.CreateEnumerable(
  63. _ => AsyncEnumerable.CreateEnumerator<TValue>(
  64. () => moveNextThrows,
  65. current: null,
  66. dispose: null)
  67. );
  68. }
  69. private void AssertThrows<E>(Action a, Func<E, bool> assert)
  70. where E : Exception
  71. {
  72. var hasFailed = false;
  73. try
  74. {
  75. a();
  76. }
  77. catch (E e)
  78. {
  79. Assert.True(assert(e));
  80. hasFailed = true;
  81. }
  82. if (!hasFailed)
  83. {
  84. Assert.True(false);
  85. }
  86. }
  87. }
  88. }