Never.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. AssertThrows<InvalidOperationException>(() => Nop(e.Current));
  20. await e.DisposeAsync();
  21. }
  22. private void Nop(object o)
  23. {
  24. }
  25. [Fact]
  26. public void CancelToken_Unblocks()
  27. {
  28. var cts = new CancellationTokenSource();
  29. var en = AsyncEnumerableEx.Never<int>().GetAsyncEnumerator(cts.Token);
  30. try
  31. {
  32. var t = Task.Run(async () =>
  33. {
  34. await Task.Delay(100);
  35. cts.Cancel();
  36. });
  37. try
  38. {
  39. Assert.True(en.MoveNextAsync().AsTask().Wait(2000));
  40. }
  41. catch (AggregateException ex)
  42. {
  43. if (!(ex.InnerException is TaskCanceledException))
  44. {
  45. throw;
  46. }
  47. }
  48. }
  49. finally
  50. {
  51. en.DisposeAsync().AsTask().Wait(2000);
  52. }
  53. }
  54. }
  55. }