Timeout.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Timeout : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public async Task Timeout_Never()
  15. {
  16. var source = AsyncEnumerableEx.Never<int>().Timeout(TimeSpan.FromMilliseconds(100));
  17. var en = source.GetAsyncEnumerator();
  18. try
  19. {
  20. await en.MoveNextAsync();
  21. Assert.Fail("MoveNextAsync should have thrown");
  22. }
  23. catch (TimeoutException)
  24. {
  25. // expected
  26. }
  27. finally
  28. {
  29. await en.DisposeAsync();
  30. }
  31. }
  32. [Fact]
  33. public async Task Timeout_Double_Never()
  34. {
  35. var source = AsyncEnumerableEx.Never<int>()
  36. .Timeout(TimeSpan.FromMilliseconds(300))
  37. .Timeout(TimeSpan.FromMilliseconds(100));
  38. var en = source.GetAsyncEnumerator();
  39. try
  40. {
  41. await en.MoveNextAsync();
  42. Assert.Fail("MoveNextAsync should have thrown");
  43. }
  44. catch (TimeoutException)
  45. {
  46. // expected
  47. }
  48. finally
  49. {
  50. await en.DisposeAsync();
  51. }
  52. }
  53. [Fact]
  54. public async Task Timeout_Delayed_Main()
  55. {
  56. var source = AsyncEnumerable.Range(1, 5)
  57. .SelectAwait(async v =>
  58. {
  59. await Task.Delay(300);
  60. return v;
  61. })
  62. .Timeout(TimeSpan.FromMilliseconds(100));
  63. var en = source.GetAsyncEnumerator();
  64. try
  65. {
  66. await en.MoveNextAsync();
  67. Assert.Fail("MoveNextAsync should have thrown");
  68. }
  69. catch (TimeoutException)
  70. {
  71. // expected
  72. }
  73. finally
  74. {
  75. await en.DisposeAsync();
  76. }
  77. }
  78. [Fact]
  79. public async Task Timeout_Delayed_Main_Canceled()
  80. {
  81. var tcs = new TaskCompletionSource<int>();
  82. var source = AsyncEnumerable.Range(1, 5)
  83. .SelectAwaitWithCancellation(async (v, ct) =>
  84. {
  85. try
  86. {
  87. await Task.Delay(500, ct);
  88. }
  89. catch (TaskCanceledException)
  90. {
  91. tcs.SetResult(0);
  92. }
  93. return v;
  94. })
  95. .Timeout(TimeSpan.FromMilliseconds(250));
  96. var en = source.GetAsyncEnumerator();
  97. try
  98. {
  99. await en.MoveNextAsync();
  100. Assert.Fail("MoveNextAsync should have thrown");
  101. }
  102. catch (TimeoutException)
  103. {
  104. // expected
  105. }
  106. finally
  107. {
  108. await en.DisposeAsync();
  109. }
  110. Assert.Equal(0, await tcs.Task);
  111. }
  112. }
  113. }