Never.cs 1.2 KB

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