AsyncEnumerableTests.cs 3.1 KB

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