TaskExtTests.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. using Xunit;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace Tests
  10. {
  11. public partial class AsyncTests
  12. {
  13. [Fact]
  14. public async Task ExceptionHandling_ShouldThrowUnwrappedException()
  15. {
  16. try
  17. {
  18. var asyncEnumerable = AsyncEnumerable.ToAsyncEnumerable(GetEnumerableWithError());
  19. await asyncEnumerable.ToArrayAsync();
  20. }
  21. catch (AggregateException)
  22. {
  23. Assert.Fail("AggregateException has been thrown instead of InvalidOperationException");
  24. }
  25. catch (InvalidOperationException)
  26. {
  27. }
  28. }
  29. private IEnumerable<int> GetEnumerableWithError()
  30. {
  31. yield return 5;
  32. throw new InvalidOperationException();
  33. }
  34. [Fact]
  35. public async Task ExceptionHandling_ShouldThrowUnwrappedException2()
  36. {
  37. try
  38. {
  39. var asyncEnumerable = AsyncEnumerableEx.Generate(15, (x) => { throw new InvalidOperationException(); }, (x) => { return 20; }, (x) => { return 2; });
  40. await asyncEnumerable.ToArrayAsync();
  41. }
  42. catch (AggregateException)
  43. {
  44. Assert.Fail("AggregateException has been thrown instead of InvalidOperationException");
  45. }
  46. catch (InvalidOperationException)
  47. {
  48. }
  49. }
  50. }
  51. }