Never.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 async Task CancelToken_UnblocksAsync()
  22. {
  23. using var cts = new CancellationTokenSource();
  24. var en = AsyncEnumerableEx.Never<int>().GetAsyncEnumerator(cts.Token);
  25. try
  26. {
  27. cts.CancelAfter(100);
  28. await Assert.ThrowsAsync<TaskCanceledException>(() => en.MoveNextAsync().AsTask());
  29. }
  30. finally
  31. {
  32. await en.DisposeAsync();
  33. }
  34. }
  35. }
  36. }