Never.cs 1.5 KB

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