|
| 1 | +using System; |
| 2 | +using NSubstitute; |
| 3 | +using Sentry.Integrations; |
| 4 | +using Sentry.Internal; |
| 5 | +using Xunit; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Sentry.Extensibility; |
| 9 | +using Sentry.Protocol; |
| 10 | +using System.Threading; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | +namespace Sentry.Tests |
| 14 | +{ |
| 15 | + public class TaskUnobservedTaskExceptionIntegrationTests |
| 16 | + { |
| 17 | + private class Fixture |
| 18 | + { |
| 19 | + public IHub Hub { get; set; } = Substitute.For<IHub, IDisposable>(); |
| 20 | + public IAppDomain AppDomain { get; set; } = Substitute.For<IAppDomain>(); |
| 21 | + |
| 22 | + public Fixture() => Hub.IsEnabled.Returns(true); |
| 23 | + |
| 24 | + public TaskUnobservedTaskExceptionIntegration GetSut() |
| 25 | + => new TaskUnobservedTaskExceptionIntegration(AppDomain); |
| 26 | + } |
| 27 | + |
| 28 | + private readonly Fixture _fixture = new Fixture(); |
| 29 | + public SentryOptions SentryOptions { get; set; } = new SentryOptions(); |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void Handle_WithException_CaptureEvent() |
| 33 | + { |
| 34 | + var sut = _fixture.GetSut(); |
| 35 | + sut.Register(_fixture.Hub, SentryOptions); |
| 36 | + |
| 37 | + sut.Handle(this, new UnobservedTaskExceptionEventArgs(new AggregateException())); |
| 38 | + |
| 39 | + _ = _fixture.Hub.Received(1).CaptureEvent(Arg.Any<SentryEvent>()); |
| 40 | + } |
| 41 | + |
| 42 | +// Only triggers in release mode. |
| 43 | +#if RELEASE |
| 44 | + [Fact] // Integration test. |
| 45 | + public void Handle_UnobservedTaskException_CaptureEvent() |
| 46 | + { |
| 47 | + _fixture.AppDomain = AppDomainAdapter.Instance; |
| 48 | + var evt = new ManualResetEvent(false); |
| 49 | + _fixture.Hub.When(x => x.CaptureEvent(Arg.Any<SentryEvent>())) |
| 50 | + .Do(_ => evt.Set()); |
| 51 | + |
| 52 | + var sut = _fixture.GetSut(); |
| 53 | + sut.Register(_fixture.Hub, SentryOptions); |
| 54 | + try |
| 55 | + { |
| 56 | + Task.Factory.StartNew(() => { throw new Exception("Unhandled on Task"); }); |
| 57 | + Thread.Sleep(2000); |
| 58 | + GC.Collect(); |
| 59 | + GC.WaitForPendingFinalizers(); |
| 60 | + |
| 61 | + Assert.True(evt.WaitOne(TimeSpan.FromMilliseconds(1))); |
| 62 | + } |
| 63 | + finally |
| 64 | + { |
| 65 | + sut.Unregister(_fixture.Hub); |
| 66 | + } |
| 67 | + } |
| 68 | +#endif |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void Handle_NoException_NoCaptureEvent() |
| 72 | + { |
| 73 | + var sut = _fixture.GetSut(); |
| 74 | + sut.Register(_fixture.Hub, SentryOptions); |
| 75 | + |
| 76 | + sut.Handle(this, new UnobservedTaskExceptionEventArgs(null)); |
| 77 | + |
| 78 | + _ = _fixture.Hub.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>()); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void Register_UnhandledException_Subscribes() |
| 83 | + { |
| 84 | + var sut = _fixture.GetSut(); |
| 85 | + sut.Register(_fixture.Hub, SentryOptions); |
| 86 | + |
| 87 | + _fixture.AppDomain.Received().UnobservedTaskException += sut.Handle; |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void Unregister_UnhandledException_Unsubscribes() |
| 92 | + { |
| 93 | + var sut = _fixture.GetSut(); |
| 94 | + sut.Register(_fixture.Hub, SentryOptions); |
| 95 | + sut.Unregister(_fixture.Hub); |
| 96 | + |
| 97 | + _fixture.AppDomain.Received(1).UnobservedTaskException -= sut.Handle; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments