Never.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Never : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public async Task Never1()
  15. {
  16. var xs = AsyncEnumerableEx.Never<int>();
  17. var e = xs.GetAsyncEnumerator();
  18. Assert.False(e.MoveNextAsync().IsCompleted); // Very rudimentary check
  19. await e.DisposeAsync();
  20. }
  21. [Fact]
  22. public void CancelToken_Unblocks()
  23. {
  24. var cts = new CancellationTokenSource();
  25. var en = AsyncEnumerableEx.Never<int>().GetAsyncEnumerator(cts.Token);
  26. try
  27. {
  28. var t = Task.Run(async () =>
  29. {
  30. await Task.Delay(100);
  31. cts.Cancel();
  32. });
  33. try
  34. {
  35. Assert.True(en.MoveNextAsync().AsTask().Wait(2000));
  36. }
  37. catch (AggregateException ex)
  38. {
  39. if (!(ex.InnerException is TaskCanceledException))
  40. {
  41. throw;
  42. }
  43. }
  44. }
  45. finally
  46. {
  47. en.DisposeAsync().AsTask().Wait(2000);
  48. }
  49. }
  50. }
  51. }