-
-
Notifications
You must be signed in to change notification settings - Fork 86
Add timeout functionality for hooks with 5-minute default timeout #2891
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
Conversation
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
@@ -22,7 +22,7 @@ public record InstanceHookMethod : IExecutableHook<TestContext> | |||
|
|||
public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute => Attributes.OfType<TAttribute>().FirstOrDefault(); | |||
|
|||
public TimeSpan? Timeout => GetAttribute<TimeoutAttribute>()?.Timeout; | |||
public TimeSpan? Timeout => GetAttribute<TimeoutAttribute>()?.Timeout ?? TimeSpan.FromMinutes(5); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of getting it from the attribute directly, the attribute should instead have its IHookRegisteredEventReceiver
method invoked, and that should be used to override this property
TUnit.Core/Hooks/StaticHookMethod.cs
Outdated
@@ -32,7 +32,7 @@ public abstract record StaticHookMethod | |||
|
|||
public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute => Attributes.OfType<TAttribute>().FirstOrDefault(); | |||
|
|||
public TimeSpan? Timeout => GetAttribute<TimeoutAttribute>()?.Timeout; | |||
public TimeSpan? Timeout => GetAttribute<TimeoutAttribute>()?.Timeout ?? TimeSpan.FromMinutes(5); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of getting it from the attribute directly, the attribute should instead have its IHookRegisteredEventReceiver method invoked, and that should be used to override this property
@claude Review this |
Claude finished @thomhurst's task —— View job Code Review Progress
Code Review FeedbackI've reviewed the hook timeout implementation and identified several critical issues that need to be addressed, particularly around the concerns raised in the review comments. Critical Issues1. TimeoutAttribute.OnHookRegistered not setting timeout (
|
Claude finished @Copilot's task —— View job Code Review Progress
Code Review FeedbackI've reviewed the hook timeout implementation and identified several critical issues that need to be addressed, particularly around the concerns raised in the review comments. Critical Issues1. TimeoutAttribute.OnHookRegistered not setting timeout (
|
@claude Can you take over and make the changes suggested in the code reviews? |
Claude finished @thomhurst's task —— View job I'll take over and implement the changes suggested in the code reviews. ✅ Implementation Plan
Issues Identified and FixedAll four critical issues from the code review have been addressed:
Changes ImplementedCore Architecture Fixes:
Service Integration:
Implementation DetailsThe solution now properly follows TUnit's established event receiver pattern:
This maintains behavioral parity with how test timeouts work and ensures all hook types (static class hooks, instance test hooks, assembly hooks, etc.) can use timeout attributes consistently. Commit: dc3e186 - Implement proper hook timeout functionality with event receiver pattern |
- Fix TimeoutAttribute.OnHookRegistered to actually set timeout on context - Extend HookRegisteredContext to support both static and instance hook methods - Update hook method classes to use settable timeout property instead of direct attribute access - Add hook registration event receiver infrastructure to EventReceiverOrchestrator - Modify HookCollectionService to trigger hook registration events during delegate creation - Reorder service initialization to inject EventReceiverOrchestrator into HookCollectionService This implementation follows the established event receiver pattern used for tests, ensuring that TimeoutAttribute and other hook attributes can modify hook properties during registration rather than relying on direct attribute access. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
@claude Can StaticHookMethod and InstanceHookMethod share a base type that all common properties sit in? This was simplify things as hook event receivers would only largely then need to worry about a |
Claude finished @thomhurst's task —— View job Refactoring Hook Methods to Share Base Type ✅
Refactoring Complete ✅Successfully created a unified
Key ImprovementsSimplified Event Receiver Pattern:
Code Reduction:
Backwards Compatibility:
Files Modified
Commit: f576397 - Refactor hook methods to share unified HookMethod base class |
- Created abstract HookMethod base record with all shared properties - StaticHookMethod and InstanceHookMethod now inherit from HookMethod - Simplified HookRegisteredContext to work with single HookMethod type - Reduced EventReceiverOrchestrator hook processing from 15 lines to 3 - Simplified HookCollectionService hook registration logic - Hook event receivers now only need to handle HookMethod instead of both types Co-authored-by: Tom Longhurst <thomhurst@users.noreply.github.com>
This PR implements timeout functionality for TUnit hooks as requested in the issue. Hooks now support timeouts using the same
[Timeout]
attribute as tests, with a default timeout of 5 minutes for hooks without explicit timeout specification.Changes Made
Core Implementation
IHookRegisteredEventReceiver
for hook registration events, following the same pattern asITestRegisteredEventReceiver
HookRegisteredContext
class to provide context during hook registrationIHookRegisteredEventReceiver
, enabling unified timeout support for both tests and hooksStaticHookMethod
andInstanceHookMethod
to provide 5-minute default timeout when no explicit[Timeout]
attribute is presentEngine Changes
HookTimeoutHelper
utility class that implements the same timeout enforcement pattern used for testsHookCollectionService
to wrap hook execution with timeout functionalityTask
andValueTask
return types with timeout-scoped cancellation tokensUsage Examples
Hook with default 5-minute timeout:
Hook with custom timeout:
Backwards Compatibility
[Timeout]
attributes work exactly as beforeAPI Changes
Added the following public APIs (approved via updated API snapshots):
IHookRegisteredEventReceiver
interfaceHookRegisteredContext
classTimeoutAttribute.OnHookRegistered
methodThe implementation maintains behavioral parity between source-generated and reflection modes and follows TUnit's established architectural patterns.
Fixes #2890.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.