Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

[Timeout] attribute on [Before(Assembly)], [Before(Class)], [Before(TestSession)], and [Before(TestDiscovery)] hooks was ignored—always using the default 5-minute timeout regardless of the specified value.

[Timeout(900_000)] // 15 minutes - WAS IGNORED, always used 300000ms
[Before(Assembly)]
public static async Task SetUp(CancellationToken cancellationToken)
{
    await Task.Delay(TimeSpan.FromMinutes(6), cancellationToken);
}

Root Cause

ProcessHookRegistrationAsync (which invokes TimeoutAttribute.OnHookRegistered) was only called for test hooks. The delegate creation methods for class/assembly/session/discovery hooks were synchronous and bypassed hook registration entirely.

Changes

  • Added async delegate creation methods (CreateClassHookDelegateAsync, CreateAssemblyHookDelegateAsync, etc.) that call ProcessHookRegistrationAsync
  • Updated all hook building methods to use the async creators
  • Converted CollectBefore/AfterClassHooksAsync and CollectBefore/AfterAssemblyHooksAsync to properly process hook registration events

Verification

Both execution modes (Source Generated and Reflection) now respect hook timeouts:

Hook 'ClassHookTimeoutTests.BeforeClass(cancellationToken)' exceeded timeout of 100ms
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: TimeoutAttribute on [Before(Assembly)] hook is ignored - default 5 minute timeout always used</issue_title>
<issue_description>### Description

The [Timeout] attribute placed on a [Before(Assembly)] hook method is not being applied. The hook always uses the default 5-minute timeout (300,000ms) defined in HookMethod.cs, regardless of the timeout value specified in the attribute.

Expected Behavior

The hook should use the 15-minute timeout specified in the [Timeout(900_000)] attribute.

Actual Behavior

The hook fails with:

TUnit.Engine.Exceptions.TestFailedException: BeforeAssemblyException: BeforeAssembly hook failed:
Hook 'IntegrationTestsBase.SetUp()' exceeded timeout of 300000ms

The timeout is always 300,000ms (5 minutes) - the default value from HookMethod.Timeout.

Steps to Reproduce

  1. Create a test class with a [Before(Assembly)] hook that takes longer than 5 minutes on slow CI/CD environments
  2. Add [Timeout(900_000)] attribute to increase timeout to 15 minutes
  3. Run the tests
public abstract class IntegrationTestsBase                                                                                                                                     
{                                                                                                                                                                              
    [Timeout(900_000)] // 15 minutes - THIS IS IGNORED                                                                                                                         
    [Before(Assembly)]                                                                                                                                                         
    public static async Task SetUp(CancellationToken cancellationToken)                                                                                                        
    {                                                                                                                                                                          
        // Long-running setup (starting containers, etc.)                                                                                                                      
        await Task.Delay(TimeSpan.FromMinutes(6), cancellationToken);                                                                                                          
    }                                                                                                                                                                          
}                                                                                                                                                                              

TUnit Version

1.5.80

.NET Version

.NET 10.0

Operating System

Linux

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Tra...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 19, 2025
Copilot AI and others added 2 commits December 19, 2025 20:22
The TimeoutAttribute on [Before(Assembly)], [Before(Class)], [Before(TestSession)],
and [Before(TestDiscovery)] hooks was being ignored because ProcessHookRegistrationAsync
was not being called for these hooks. This commit:

- Adds async versions of hook delegate creation methods that call ProcessHookRegistrationAsync
- Updates InitializeAsync to await the async hook building methods
- Converts CollectBeforeAssemblyHooksAsync, CollectAfterAssemblyHooksAsync,
  CollectBeforeClassHooksAsync, and CollectAfterClassHooksAsync to properly
  process hook registration events
- Adds HookTimeoutTests to verify the fix works for class and assembly hooks

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ignored timeout attribute on before assembly hook Fix TimeoutAttribute not being applied to non-test hooks Dec 19, 2025
Copilot AI requested a review from thomhurst December 19, 2025 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TimeoutAttribute on [Before(Assembly)] hook is ignored - default 5 minute timeout always used

2 participants