You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR fixes a critical bug where nested ClassDataSource properties with SharedType.PerTestSession were creating multiple instances instead of sharing a single instance across the test session. The issue was in the ClassDataSources.Create method, which was handling nested dependency injection manually instead of relying on the proper dual-mode architecture.
✅ Positive Aspects
1. Correct Problem Identification
The bug was properly identified: nested properties marked with SharedType.PerTestSession were not respecting the sharing behavior because the manual property injection was bypassing the established TestDataContainer infrastructure.
2. Clean, Minimal Fix
The fix correctly removes the problematic manual property injection code and simplifies the Create method to rely on the framework's existing dual-mode architecture.
3. Comprehensive Test Coverage
Excellent test implementation that:
Tests across multiple test classes to verify session-level sharing
Uses thread-safe counters to track instance creation
Includes detailed logging for debugging
Verifies both initialization and disposal behaviors
Tests the complete dependency chain (Factory → Containers)
4. Proper Use of Threading Constructs
Test containers use Interlocked.Increment for thread-safe counter management, which is appropriate for concurrent test execution.
🔧 Technical Analysis
Root Cause
The old CreateWithNestedDependencies method was manually handling property injection using PropertySourceRegistry, which bypassed the TestDataContainer.GetGlobalInstance() calls that implement the sharing behavior. This created new instances for each nested property instead of reusing shared instances.
Reflection Mode: Property discovery and injection respects the sharing types
💡 Recommendations
1. Consider Dual-Mode Verification⚠️
According to TUnit's Rule 1 (Dual-Mode Implementation), this fix should be tested in both execution modes. Consider adding explicit test cases:
[Test][Arguments(ExecutionMode.SourceGenerated)][Arguments(ExecutionMode.Reflection)]publicasyncTaskSharedContainers_WorkInBothModes(ExecutionModemode){// Test the fix works in both source-gen and reflection modes}
2. Performance Consideration ✅
The fix actually improves performance by:
Removing unnecessary reflection calls in PropertySourceRegistry.GetSource()
Eliminating recursive property traversal
Reducing object allocations through proper sharing
3. Add AOT Compatibility Test
Since this touches object creation, verify AOT compatibility:
cd TUnit.TestProject
dotnet publish -c Release -p:PublishAot=true --use-current-runtime
🐛 Potential Concerns
1. Missing Error Handling Context
The simplified Create method loses the context about which properties were being injected. If property injection fails in the new approach, the error messages might be less informative.
Mitigation: The framework's property injection should handle this, but monitor for any error message degradation.
2. Regression Risk Assessment
Low Risk - The fix removes problematic code rather than adding complexity. The existing dual-mode architecture handles the functionality correctly.
📊 Test Quality Assessment
Strengths
✅ Tests actual bug scenario with realistic nested dependencies
✅ Uses proper async/await patterns
✅ Includes detailed logging for debugging
✅ Tests multiple test classes (session-level sharing)
✅ Verifies both creation and initialization counts
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
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.
Fixes #3803