Skip to content

Commit be6c749

Browse files
authored
[release/11.0-preview2]: Fix for OOM exception while running Analyzer tests on x86 platform (#14332)
2 parents 6668f68 + 1964ff7 commit be6c749

8 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Reflection;
5+
using Xunit.v3;
6+
7+
namespace Xunit;
8+
9+
/// <summary>
10+
/// Apply this attribute to a test class or method to force a full GC collection
11+
/// after each test. This helps prevent <see cref="OutOfMemoryException"/> in x86
12+
/// test runs where memory-intensive operations (e.g. Roslyn compilations) accumulate.
13+
/// </summary>
14+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
15+
public sealed class ForceGCAttribute : BeforeAfterTestAttribute
16+
{
17+
public override void After(MethodInfo methodUnderTest, IXunitTest test)
18+
{
19+
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true);
20+
GC.WaitForPendingFinalizers();
21+
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true);
22+
}
23+
}

src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/Analyzers/AvoidPassingTaskWithoutCancellationToken/AvoidPassingTaskWithoutCancellationTokenTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace System.Windows.Forms.Analyzers.Tests;
1111

12+
[ForceGC]
1213
public sealed class AvoidPassingTaskWithoutCancellationTokenTests
1314
{
1415
private const string TestCode = """

src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/Analyzers/MissingPropertySerializationConfiguration/ControlPropertySerializationDiagnosticAnalyzerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace System.Windows.Forms.Analyzers.Tests;
1111

12+
[ForceGC]
1213
public sealed class ControlPropertySerializationDiagnosticAnalyzerTest
1314
{
1415
private const string GlobalUsingCode = """

src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/Analyzers/WFO1001/ImplementITypedDataObjectTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace System.Windows.Forms.Analyzers.Tests;
1111

12+
[ForceGC]
1213
public sealed class ImplementITypedDataObjectTests
1314
{
1415
private const string DiagnosticId = DiagnosticIDs.ImplementITypedDataObject;

src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/Generators/ApplicationConfigurationGenerator/ApplicationConfigurationGeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace System.Windows.Forms.Analyzers.Tests;
1313

14+
[ForceGC]
1415
public partial class ApplicationConfigurationGeneratorTests
1516
{
1617
private const string SourceCompilable = """

src/System.Windows.Forms.Analyzers.VisualBasic/tests/UnitTests/System.Windows.Forms.Analyzers.VisualBasic.Tests/Analyzers/AvoidPassingTaskWithoutCancellationTokenTests.vb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Imports Microsoft.CodeAnalysis.Testing
1010
Imports Microsoft.CodeAnalysis.VisualBasic.Testing
1111
Imports Xunit
1212

13+
<ForceGC()>
1314
Public Class AvoidPassingTaskWithoutCancellationTokenTests
1415

1516
Private Const TestCode As String = "

src/System.Windows.Forms.Analyzers.VisualBasic/tests/UnitTests/System.Windows.Forms.Analyzers.VisualBasic.Tests/Analyzers/MissingPropertySerializationConfigurationAnalyzerTest.vb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Imports Microsoft.CodeAnalysis.Testing
55
Imports Microsoft.CodeAnalysis.VisualBasic.Testing
66
Imports Xunit
77

8+
<ForceGC()>
89
Public Class ControlPropertySerializationDiagnosticAnalyzerTest
910

1011
Private Const ProblematicCode As String =

src/System.Windows.Forms.Analyzers.VisualBasic/tests/UnitTests/System.Windows.Forms.Analyzers.VisualBasic.Tests/Analyzers/WFO1001/ImplementITypedDataObjectTests.vb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Imports Microsoft.CodeAnalysis.Testing
88
Imports Microsoft.CodeAnalysis.VisualBasic.Testing
99
Imports Xunit
1010

11+
<ForceGC()>
1112
Public NotInheritable Class ImplementITypedDataObjectTests
1213

1314
Private Const DiagnosticId As String = DiagnosticIDs.ImplementITypedDataObject

0 commit comments

Comments
 (0)