Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Common/tests/TestUtilities/XUnit/ForceGCAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using Xunit.v3;

namespace Xunit;

/// <summary>
/// Apply this attribute to a test class or method to force a full GC collection
/// after each test. This helps prevent <see cref="OutOfMemoryException"/> in x86
/// test runs where memory-intensive operations (e.g. Roslyn compilations) accumulate.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ForceGCAttribute : BeforeAfterTestAttribute
{
public override void After(MethodInfo methodUnderTest, IXunitTest test)
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace System.Windows.Forms.Analyzers.Tests;

[ForceGC]
public sealed class AvoidPassingTaskWithoutCancellationTokenTests
{
private const string TestCode = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace System.Windows.Forms.Analyzers.Tests;

[ForceGC]
public sealed class ControlPropertySerializationDiagnosticAnalyzerTest
{
private const string GlobalUsingCode = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace System.Windows.Forms.Analyzers.Tests;

[ForceGC]
public sealed class ImplementITypedDataObjectTests
{
private const string DiagnosticId = DiagnosticIDs.ImplementITypedDataObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace System.Windows.Forms.Analyzers.Tests;

[ForceGC]
public partial class ApplicationConfigurationGeneratorTests
{
private const string SourceCompilable = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Imports Microsoft.CodeAnalysis.Testing
Imports Microsoft.CodeAnalysis.VisualBasic.Testing
Imports Xunit

<ForceGC()>
Public Class AvoidPassingTaskWithoutCancellationTokenTests

Private Const TestCode As String = "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Imports Microsoft.CodeAnalysis.Testing
Imports Microsoft.CodeAnalysis.VisualBasic.Testing
Imports Xunit

<ForceGC()>
Public Class ControlPropertySerializationDiagnosticAnalyzerTest

Private Const ProblematicCode As String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Imports Microsoft.CodeAnalysis.Testing
Imports Microsoft.CodeAnalysis.VisualBasic.Testing
Imports Xunit

<ForceGC()>
Public NotInheritable Class ImplementITypedDataObjectTests

Private Const DiagnosticId As String = DiagnosticIDs.ImplementITypedDataObject
Expand Down