TaskExtTests.cs 1.5 KB

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