ResetExceptionDispatchStateTest.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 System;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Linq;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Assert = Xunit.Assert;
  9. using System.Threading.Tasks;
  10. namespace ReactiveTests.Tests
  11. {
  12. [TestClass]
  13. public class ResetExceptionDispatchStateTest
  14. {
  15. [TestMethod]
  16. public async Task ResetExceptionDispatchState_Throw_Consistent_StackTrace_On_Await()
  17. {
  18. var ts = Observable.Throw<int>(new Exception("Aaargh!")).ResetExceptionDispatchState();
  19. string stackTrace = null;
  20. for (int i = 0; i < 3; i++)
  21. {
  22. try
  23. {
  24. _ = await ts;
  25. }
  26. catch (Exception e)
  27. {
  28. if (stackTrace is null)
  29. {
  30. stackTrace = e.StackTrace;
  31. }
  32. else
  33. {
  34. Assert.Equal(stackTrace, e.StackTrace);
  35. }
  36. }
  37. }
  38. }
  39. [TestMethod]
  40. public async Task ResetExceptionDispatchState_Replay_Consistent_StackTrace_On_Await()
  41. {
  42. var cts = Observable.Throw<int>(new Exception("Aaargh!"), CurrentThreadScheduler.Instance).Replay(1);
  43. cts.Connect();
  44. var ts = cts.ResetExceptionDispatchState();
  45. string stackTrace = null;
  46. for (int i = 0; i < 3; i++)
  47. {
  48. try
  49. {
  50. _ = await ts;
  51. }
  52. catch (Exception e)
  53. {
  54. if (stackTrace is null)
  55. {
  56. stackTrace = e.StackTrace;
  57. }
  58. else
  59. {
  60. Assert.Equal(stackTrace, e.StackTrace);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }