Skip to content

Commit

Permalink
avoid code duplication for disabling parallelization with xUnit (#62132)
Browse files Browse the repository at this point in the history
The collection definitions must be in the same assembly as the test that uses them
  • Loading branch information
adamsitnik authored Nov 30, 2021
1 parent fe68bf8 commit 8f87ac7
Show file tree
Hide file tree
Showing 41 changed files with 68 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System
{
// The collection definitions must be in the same assembly as the test that uses them.
// So please use "Compile Include" in the project file to include this class.
[CollectionDefinition(nameof(DisableParallelization), DisableParallelization = true)]
public class DisableParallelization { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

namespace System.ComponentModel.Tests
{
[CollectionDefinition("NoParallelTests", DisableParallelization = true)]
public partial class NoParallelTests { }

// Mutable static comparision in the implementation
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public class MemberDescriptorTests
{
[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace System.ComponentModel.Tests
{
[SimpleUpdateTest]
[Collection("NoParallelTests")] // Clears the cache which disrupts concurrent tests
[Collection(nameof(DisableParallelization))] // Clears the cache which disrupts concurrent tests
public class ReflectionCachesUpdateHandlerTests
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@
<Compile Include="Security\Authentication\ExtendedProtection\ExtendedProtectionPolicyTypeConverterTests.cs" />
<Compile Include="XTypeDescriptionProviderTests.cs" />

<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs"
Link="Common\TestUtilities\System\DisableParallelization.cs" />
<TrimmerRootDescriptor Include="$(ILLinkDescriptorsPath)ILLink.Descriptors.Castle.xml" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace System.ComponentModel.Tests
{
[Collection("NoParallelTests")] // manipulates cache
[Collection(nameof(DisableParallelization))] // manipulates cache
public class TypeDescriptorTests
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoAOT))] // Mock will try to JIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ public void FileSystemWatcher_ModifyFiltersConcurrentWithEvents()
}
}

[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public partial class DangerousFileSystemWatcherTests : FileSystemWatcherTest
{
private readonly ITestOutputHelper _output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
Link="Common\System\IO\TempFile.cs" />
<Compile Include="$(CommonTestPath)System\IO\TempDirectory.cs"
Link="Common\System\IO\TempDirectory.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs"
Link="Common\TestUtilities\System\DisableParallelization.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsLinux)' == 'true' or '$(TargetsOSX)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">
<Compile Include="FileSystemWatcher.Unix.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

namespace System.IO.Tests
{
[CollectionDefinition("NoParallelTests", DisableParallelization = true)]
public partial class NoParallelTests { }

public abstract partial class FileSystemWatcherTest : FileCleanupTestBase
{
// Events are reported asynchronously by the OS, so allow an amount of time for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="..\PortedCommon\IOInputs.cs" />
<Compile Include="..\PortedCommon\IOServices.cs" />
<Compile Include="$(CommonTestPath)System\IO\PathFeatures.cs" Link="Common\System\IO\PathFeatures.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
<Compile Remove="..\**\*.Windows.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected override string GetTestFilePath(int? index = null, [CallerMemberName]
}

[PlatformSpecific(TestPlatforms.Windows)] // the test setup is Windows-specifc
[Collection("NoParallelTests")] // don't run in parallel, as file sharing logic is not thread-safe
[Collection(nameof(DisableParallelization))] // don't run in parallel, as file sharing logic is not thread-safe
[OuterLoop("Requires admin privileges to create a file share")]
[ConditionalClass(typeof(UncFilePathFileStreamStandaloneConformanceTests), nameof(CanShareFiles))]
public class UncFilePathFileStreamStandaloneConformanceTests : UnbufferedAsyncFileStreamStandaloneConformanceTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.IO.Tests
// Don't run in parallel as the WhenDiskIsFullTheErrorMessageContainsAllDetails test
// consumes entire available free space on the disk (only on Linux, this is how posix_fallocate works)
// and if we try to run other disk-writing test in the meantime we are going to get "No space left on device" exception.
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public partial class FileStream_ctor_options : FileStream_ctor_str_fm_fa_fs_buffer_fo
{
protected override string GetExpectedParamName(string paramName) => "value";
Expand Down Expand Up @@ -166,7 +166,4 @@ public void WhenDiskIsFullTheErrorMessageContainsAllDetails(FileMode mode)
Assert.False(exists);
}
}

[CollectionDefinition("NoParallelTests", DisableParallelization = true)]
public partial class NoParallelTests { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
<Compile Include="$(CommonTestPath)System\IO\PathFeatures.cs" Link="Common\System\IO\PathFeatures.cs" />
<Content Include="DirectoryInfo\test-dir\dummy.txt" Link="test-dir\dummy.txt" />
<Compile Include="$(CommonPath)System\IO\PathInternal.CaseSensitivity.cs" Link="Common\System\IO\PathInternal.CaseSensitivity.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@

namespace System.Net.Http.Functional.Tests
{
[CollectionDefinition(nameof(NonParallelTestCollection), DisableParallelization = true)]
public class NonParallelTestCollection
{
}

// This test class contains tests which are strongly timing-dependent.
// There are two mitigations avoid flaky behavior on CI:
// - Parallel test execution is disabled
// - Using extreme parameters, and checks which are very unlikely to fail, if the implementation is correct
[Collection(nameof(NonParallelTestCollection))]
[Collection(nameof(DisableParallelization))]
[ConditionalClass(typeof(SocketsHttpHandler_Http2FlowControl_Test), nameof(IsSupported))]
public sealed class SocketsHttpHandler_Http2FlowControl_Test : HttpClientHandlerTestBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace System.Net.Http.Functional.Tests
{
[Collection(nameof(NonParallelTestCollection))]
[Collection(nameof(DisableParallelization))]
[ConditionalClass(typeof(SocketsHttpHandler_Http2KeepAlivePing_Test), nameof(IsSupported))]
public sealed class SocketsHttpHandler_Http2KeepAlivePing_Test : HttpClientHandlerTestBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3143,23 +3143,23 @@ public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http2(ITestOutputH
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_Http3_MsQuic : HttpClientHandlerTest_Http3
{
public SocketsHttpHandlerTest_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
protected override QuicImplementationProvider UseQuicImplementationProvider => QuicImplementationProviders.MsQuic;
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_Http3_Mock : HttpClientHandlerTest_Http3
{
public SocketsHttpHandlerTest_Http3_Mock(ITestOutputHelper output) : base(output) { }
protected override QuicImplementationProvider UseQuicImplementationProvider => QuicImplementationProviders.Mock;
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_HttpClientHandlerTest_Http3_MsQuic : HttpClientHandlerTest
{
public SocketsHttpHandlerTest_HttpClientHandlerTest_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
Expand All @@ -3168,7 +3168,7 @@ public SocketsHttpHandlerTest_HttpClientHandlerTest_Http3_MsQuic(ITestOutputHelp
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_HttpClientHandlerTest_Http3_Mock : HttpClientHandlerTest
{
public SocketsHttpHandlerTest_HttpClientHandlerTest_Http3_Mock(ITestOutputHelper output) : base(output) { }
Expand All @@ -3177,7 +3177,7 @@ public SocketsHttpHandlerTest_HttpClientHandlerTest_Http3_Mock(ITestOutputHelper
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_Cookies_Http3_MsQuic : HttpClientHandlerTest_Cookies
{
public SocketsHttpHandlerTest_Cookies_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
Expand All @@ -3186,7 +3186,7 @@ public SocketsHttpHandlerTest_Cookies_Http3_MsQuic(ITestOutputHelper output) : b
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_Cookies_Http3_Mock : HttpClientHandlerTest_Cookies
{
public SocketsHttpHandlerTest_Cookies_Http3_Mock(ITestOutputHelper output) : base(output) { }
Expand All @@ -3195,7 +3195,7 @@ public SocketsHttpHandlerTest_Cookies_Http3_Mock(ITestOutputHelper output) : bas
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_MsQuic : HttpClientHandlerTest_Headers
{
public SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
Expand All @@ -3204,7 +3204,7 @@ public SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_MsQuic(ITestOu
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_Mock : HttpClientHandlerTest_Headers
{
public SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_Mock(ITestOutputHelper output) : base(output) { }
Expand All @@ -3213,7 +3213,7 @@ public SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_Mock(ITestOutp
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_MsQuic : SocketsHttpHandler_Cancellation_Test
{
public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
Expand All @@ -3222,7 +3222,7 @@ public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_MsQuic(ITest
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_Mock : SocketsHttpHandler_Cancellation_Test
{
public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_Mock(ITestOutputHelper output) : base(output) { }
Expand All @@ -3231,7 +3231,7 @@ public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_Mock(ITestOu
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandler_HttpClientHandler_AltSvc_Test_Http3_MsQuic : HttpClientHandler_AltSvc_Test
{
public SocketsHttpHandler_HttpClientHandler_AltSvc_Test_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
Expand All @@ -3240,7 +3240,7 @@ public SocketsHttpHandler_HttpClientHandler_AltSvc_Test_Http3_MsQuic(ITestOutput
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandler_HttpClientHandler_AltSvc_Test_Http3_Mock : HttpClientHandler_AltSvc_Test
{
public SocketsHttpHandler_HttpClientHandler_AltSvc_Test_Http3_Mock(ITestOutputHelper output) : base(output) { }
Expand All @@ -3249,7 +3249,7 @@ public SocketsHttpHandler_HttpClientHandler_AltSvc_Test_Http3_Mock(ITestOutputHe
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandler_HttpClientHandler_Finalization_Http3_MsQuic : HttpClientHandler_Finalization_Test
{
public SocketsHttpHandler_HttpClientHandler_Finalization_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
Expand All @@ -3258,18 +3258,14 @@ public SocketsHttpHandler_HttpClientHandler_Finalization_Http3_MsQuic(ITestOutpu
}

[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMockQuicSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class SocketsHttpHandler_HttpClientHandler_Finalization_Http3_Mock : HttpClientHandler_Finalization_Test
{
public SocketsHttpHandler_HttpClientHandler_Finalization_Http3_Mock(ITestOutputHelper output) : base(output) { }
protected override Version UseVersion => HttpVersion.Version30;
protected override QuicImplementationProvider UseQuicImplementationProvider => QuicImplementationProviders.Mock;
}

// Define test collection for tests to avoid all other tests.
[CollectionDefinition("NoParallelTests", DisableParallelization = true)]
public partial class NoParallelTests { }

[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public abstract class SocketsHttpHandler_RequestValidationTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
Link="Common\System\Net\Http\HttpClientHandlerTest.Cookies.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\HttpClientHandlerTest.DefaultProxyCredentials.cs"
Link="Common\System\Net\Http\HttpClientHandlerTest.DefaultProxyCredentials.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs"
Link="Common\TestUtilities\System\DisableParallelization.cs" />
<Compile Include="HttpClientHandlerTest.AltSvc.cs" />
<Compile Include="SocketsHttpHandlerTest.Cancellation.cs" />
<Compile Include="SocketsHttpHandlerTest.Http2FlowControl.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Net.NameResolution.Tests
{
using Configuration = System.Net.Test.Common.Configuration;

[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public class LoggingTest
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
Link="Common\System\Net\Configuration.Sockets.cs" />
<Compile Include="$(CommonTestPath)System\Diagnostics\Tracing\TestEventListener.cs"
Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs"
Link="Common\TestUtilities\System\DisableParallelization.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

namespace System.Net.NameResolution.Tests
{
[CollectionDefinition("NoParallelTests", DisableParallelization = true)]
public partial class NoParallelTests { }

internal static class TestSettings
{
// A hostname that will not exist in any DNS caches, forcing some I/O to lookup.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace System.Net.Quic.Tests
{
[ConditionalClass(typeof(QuicTestBase<MsQuicProviderFactory>), nameof(IsSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public class MsQuicTests : QuicTestBase<MsQuicProviderFactory>
{
private static byte[] s_data = Encoding.UTF8.GetBytes("Hello world!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class MockQuicStreamConformanceTests : QuicStreamConformanceTests
}

[ConditionalClass(typeof(QuicTestBase<MsQuicProviderFactory>), nameof(QuicTestBase<MsQuicProviderFactory>.IsSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class MsQuicQuicStreamConformanceTests : QuicStreamConformanceTests
{
protected override QuicImplementationProvider Provider => QuicImplementationProviders.MsQuic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,13 +994,9 @@ public QuicStreamTests_MockProvider(ITestOutputHelper output) : base(output) { }
}

[ConditionalClass(typeof(QuicTestBase<MsQuicProviderFactory>), nameof(QuicTestBase<MsQuicProviderFactory>.IsSupported))]
[Collection("NoParallelTests")]
[Collection(nameof(DisableParallelization))]
public sealed class QuicStreamTests_MsQuicProvider : QuicStreamTests<MsQuicProviderFactory>
{
public QuicStreamTests_MsQuicProvider(ITestOutputHelper output) : base(output) { }
}

// Define test collection for tests to avoid all other tests.
[CollectionDefinition("NoParallelTests", DisableParallelization = true)]
public partial class NoParallelTests { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Compile Include="$(CommonTestPath)System\Security\Cryptography\X509Certificates\CertificateAuthority.cs" Link="CommonTest\System\Security\Cryptography\X509Certificates\CertificateAuthority.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\X509Certificates\RevocationResponder.cs" Link="CommonTest\System\Security\Cryptography\X509Certificates\RevocationResponder.cs" />
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs" Link="TestCommon\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
<Compile Include="$(CommonTestPath)TestUtilities\System\DisableParallelization.cs" Link="Common\TestUtilities\System\DisableParallelization.cs" />
<Compile Include="..\..\..\System.Net.Security\tests\FunctionalTests\TestHelper.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public async Task FailedConnect_ConnectedReturnsFalse()
// The test class is declared non-parallel because of possible IPv4/IPv6 port-collision on Unix:
// When running these tests in parallel with other tests, there is some chance that the DualMode client
// will connect to an IPv4 server of a parallel test case.
[Collection(nameof(NoParallelTests))]
[Collection(nameof(DisableParallelization))]
public abstract class Connect_NonParallel<T> : SocketTestHelperBase<T> where T : SocketHelperBase, new()
{
protected Connect_NonParallel(ITestOutputHelper output) : base(output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ private static void CreateSocketWithDanglingReference()
}
}

[Collection(nameof(NoParallelTests))]
[Collection(nameof(DisableParallelization))]
public class DisposedSocketTestsNonParallel
{
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
Expand Down
Loading

0 comments on commit 8f87ac7

Please sign in to comment.