AsyncTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  6. using System.Collections.Generic;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Xunit;
  13. using FluentAssertions;
  14. namespace Tests
  15. {
  16. public partial class AsyncTests
  17. {
  18. #pragma warning disable xUnit1013 // Public method should be marked as test
  19. public void AssertThrows<E>(Action a)
  20. where E : Exception
  21. {
  22. Assert.Throws<E>(a);
  23. }
  24. [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)]
  25. public Task AssertThrows<E>(Func<Task> func)
  26. where E : Exception
  27. {
  28. return Assert.ThrowsAsync<E>(func);
  29. }
  30. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  31. where E : Exception
  32. {
  33. var hasFailed = false;
  34. try
  35. {
  36. a();
  37. }
  38. catch (E e)
  39. {
  40. Assert.True(assert(e));
  41. hasFailed = true;
  42. }
  43. if (!hasFailed)
  44. {
  45. Assert.True(false);
  46. }
  47. }
  48. public void NoNext<T>(IAsyncEnumerator<T> e)
  49. {
  50. Assert.False(e.MoveNext().Result);
  51. }
  52. public void HasNext<T>(IAsyncEnumerator<T> e, T value)
  53. {
  54. Assert.True(e.MoveNext().Result);
  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. en1.Dispose();
  63. en2.Dispose();
  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. /// <summary>
  73. /// Waits WaitTimeoutMs or until cancellation is requested. If cancellation was not requested, MoveNext returns true.
  74. /// </summary>
  75. internal sealed class CancellationTestAsyncEnumerable : IAsyncEnumerable<int>
  76. {
  77. private readonly int iterationsBeforeDelay;
  78. public CancellationTestAsyncEnumerable(int iterationsBeforeDelay = 0)
  79. {
  80. this.iterationsBeforeDelay = iterationsBeforeDelay;
  81. }
  82. IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator() => GetEnumerator();
  83. public TestEnumerator GetEnumerator() => new TestEnumerator(iterationsBeforeDelay);
  84. internal sealed class TestEnumerator : IAsyncEnumerator<int>
  85. {
  86. private readonly int iterationsBeforeDelay;
  87. public TestEnumerator(int iterationsBeforeDelay)
  88. {
  89. this.iterationsBeforeDelay = iterationsBeforeDelay;
  90. }
  91. int i = -1;
  92. public void Dispose()
  93. {
  94. }
  95. public bool MoveNextWasCalled { get; private set; }
  96. public int Current => i;
  97. public async Task<bool> MoveNextAsync()
  98. {
  99. MoveNextWasCalled = true;
  100. i++;
  101. if (Current >= iterationsBeforeDelay)
  102. {
  103. await Task.Delay(WaitTimeoutMs);
  104. }
  105. return true;
  106. }
  107. }
  108. }
  109. }
  110. }