-
-
Notifications
You must be signed in to change notification settings - Fork 250
test: add ASP.NET Core blocking detection integration tests #5578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jamescrosswell
merged 2 commits into
getsentry:main
from
z0rimo:test/aspnetcore-blocking-detection-5394
Sep 18, 2026
+196
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
test/Sentry.AspNetCore.Tests/IntegrationsTests.BlockingDetection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| using Sentry.AspNetCore.TestUtils; | ||
| using Sentry.Ben.BlockingDetector; | ||
|
|
||
| namespace Sentry.AspNetCore.Tests; | ||
|
|
||
| public partial class IntegrationsTests | ||
| { | ||
| private const string BlockingCallDetectorMechanism = "BlockingCallDetector"; | ||
|
|
||
| [Fact] | ||
| public async Task InvokeAsync_CaptureBlockingCallsEnabled_ReusesListenerAcrossRequests() | ||
| { | ||
| var middlewareInstances = new ConcurrentQueue<SentryMiddleware>(); | ||
|
|
||
| Configure = options => options.CaptureBlockingCalls = true; | ||
| AfterConfigureBuilder = builder => builder.ConfigureServices(services => | ||
| { | ||
| services.RemoveAll<SentryMiddleware>(); | ||
| services.AddTransient(serviceProvider => | ||
| { | ||
| var middleware = ActivatorUtilities.CreateInstance<SentryMiddleware>(serviceProvider); | ||
| middlewareInstances.Enqueue(middleware); | ||
| return middleware; | ||
| }); | ||
| }); | ||
|
|
||
| Build(); | ||
|
|
||
| _ = await HttpClient.GetAsync("/"); | ||
| _ = await HttpClient.GetAsync("/"); | ||
|
|
||
| var instances = middlewareInstances.ToArray(); | ||
| Assert.Equal(2, instances.Length); | ||
| Assert.NotSame(instances[0], instances[1]); | ||
| Assert.NotNull(instances[0].Monitor); | ||
| Assert.NotNull(instances[0].Listener); | ||
| Assert.Same(instances[0].Monitor, instances[1].Monitor); | ||
| Assert.Same(instances[0].Listener, instances[1].Listener); | ||
| Assert.Same(ServiceProvider.GetRequiredService<IBlockingMonitor>(), instances[0].Monitor); | ||
| Assert.Same(ServiceProvider.GetRequiredService<TaskBlockingListener>(), instances[0].Listener); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeAsync_BlockingCallDetectionEnabled_CapturesBlockingCallEvent() | ||
| { | ||
| var events = new ConcurrentQueue<SentryEvent>(); | ||
| Configure = options => | ||
| { | ||
| options.CaptureBlockingCalls = true; | ||
| options.SetBeforeSend(@event => | ||
| { | ||
| events.Enqueue(@event); | ||
| return @event; | ||
| }); | ||
| }; | ||
| Handlers = | ||
| [ | ||
| new RequestHandler | ||
| { | ||
| Path = "/blocking", | ||
| Handler = _ => | ||
| { | ||
| Task.Delay(25).Wait(); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| Build(); | ||
|
|
||
| _ = await HttpClient.GetAsync("/blocking"); | ||
|
|
||
| var blockingEvent = Assert.Single(events, IsBlockingCallDetectorEvent); | ||
| var mechanism = blockingEvent.SentryExceptions!.Single(exception => exception.Mechanism?.Type == BlockingCallDetectorMechanism).Mechanism; | ||
| Assert.Equal("EventListener", mechanism!.Source); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeAsync_BlockingCallDetectionEnabled_SynchronizationContextWait_CapturesBlockingCallEvent() | ||
| { | ||
| var events = new ConcurrentQueue<SentryEvent>(); | ||
| Configure = options => | ||
| { | ||
| options.CaptureBlockingCalls = true; | ||
| options.SetBeforeSend(@event => | ||
| { | ||
| events.Enqueue(@event); | ||
| return @event; | ||
| }); | ||
| }; | ||
| Handlers = | ||
| [ | ||
| new RequestHandler | ||
| { | ||
| Path = "/blocking", | ||
| Handler = _ => | ||
| { | ||
| using var manualResetEvent = new ManualResetEvent(false); | ||
| manualResetEvent.WaitOne(25); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| Build(); | ||
|
|
||
| _ = await HttpClient.GetAsync("/blocking"); | ||
|
|
||
| var blockingEvent = Assert.Single(events, IsBlockingCallDetectorEvent); | ||
| var mechanism = blockingEvent.SentryExceptions!.Single(exception => exception.Mechanism?.Type == BlockingCallDetectorMechanism).Mechanism; | ||
| Assert.Equal("SynchronizationContext", mechanism!.Source); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeAsync_BlockingCallDetectionDisabled_DoesNotCaptureOrInstantiateDetector() | ||
| { | ||
| var events = new ConcurrentQueue<SentryEvent>(); | ||
| var middlewareCount = 0; | ||
| var monitorCount = 0; | ||
| var listenerCount = 0; | ||
| Configure = options => | ||
| { | ||
| options.CaptureBlockingCalls = false; | ||
| options.SetBeforeSend(@event => | ||
| { | ||
| events.Enqueue(@event); | ||
| return @event; | ||
| }); | ||
| }; | ||
| ConfigureCountingBlockingDetectionServices( | ||
| () => Interlocked.Increment(ref middlewareCount), | ||
| () => Interlocked.Increment(ref monitorCount), | ||
| () => Interlocked.Increment(ref listenerCount)); | ||
| Handlers = | ||
| [ | ||
| new RequestHandler | ||
| { | ||
| Path = "/blocking", | ||
| Handler = _ => | ||
| { | ||
| Task.Delay(25).Wait(); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| Build(); | ||
|
|
||
| _ = await HttpClient.GetAsync("/blocking"); | ||
|
|
||
| Assert.Equal(1, middlewareCount); | ||
| Assert.Equal(0, monitorCount); | ||
| Assert.Equal(0, listenerCount); | ||
| Assert.DoesNotContain(events, IsBlockingCallDetectorEvent); | ||
| } | ||
|
|
||
| private void ConfigureCountingBlockingDetectionServices( | ||
| Action middlewareCreated, | ||
| Action monitorCreated, | ||
| Action listenerCreated) | ||
| { | ||
| AfterConfigureBuilder = builder => builder.ConfigureServices(services => | ||
| { | ||
| services.RemoveAll<SentryMiddleware>(); | ||
| services.AddTransient(serviceProvider => | ||
| { | ||
| middlewareCreated(); | ||
| return ActivatorUtilities.CreateInstance<SentryMiddleware>(serviceProvider); | ||
| }); | ||
|
|
||
| services.RemoveAll<IBlockingMonitor>(); | ||
| services.AddSingleton<IBlockingMonitor>(serviceProvider => | ||
| { | ||
| monitorCreated(); | ||
| return ActivatorUtilities.CreateInstance<BlockingMonitor>(serviceProvider); | ||
| }); | ||
|
|
||
| services.RemoveAll<TaskBlockingListener>(); | ||
| services.AddSingleton(serviceProvider => | ||
| { | ||
| listenerCreated(); | ||
| return ActivatorUtilities.CreateInstance<TaskBlockingListener>(serviceProvider); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private static bool IsBlockingCallDetectorEvent(SentryEvent @event) => | ||
| @event.SentryExceptions?.Any(exception => exception.Mechanism?.Type == BlockingCallDetectorMechanism) == true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.