Never.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #if NET6_0_OR_GREATER
  5. #pragma warning disable CA2012 // Use ValueTasks correctly. These tests need to use Result to verify correct operation, so we can't avoid breaking this rule.
  6. #endif
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Xunit;
  11. namespace Tests
  12. {
  13. public class Never : AsyncEnumerableExTests
  14. {
  15. [Fact]
  16. public async Task Never1()
  17. {
  18. var xs = AsyncEnumerableEx.Never<int>();
  19. var e = xs.GetAsyncEnumerator();
  20. Assert.False(e.MoveNextAsync().IsCompleted); // Very rudimentary check
  21. await e.DisposeAsync();
  22. }
  23. [Fact]
  24. public async Task CancelToken_UnblocksAsync()
  25. {
  26. using var cts = new CancellationTokenSource();
  27. var en = AsyncEnumerableEx.Never<int>().GetAsyncEnumerator(cts.Token);
  28. try
  29. {
  30. cts.CancelAfter(100);
  31. await Assert.ThrowsAsync<TaskCanceledException>(() => en.MoveNextAsync().AsTask());
  32. }
  33. finally
  34. {
  35. await en.DisposeAsync();
  36. }
  37. }
  38. }
  39. }