Xunit.DependencyInjection icon indicating copy to clipboard operation
Xunit.DependencyInjection copied to clipboard

MaxParallelThreads does not work with Conservative parallelAlgorithm

Open StanislavKlo opened this issue 11 months ago • 11 comments

Describe the bug I've created small project with Xunit.DependencyInjection v9.8.0 with 2 simple test classes (just copy paste to reproduce the issue) :

using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = false, MaxParallelThreads = 1)] 
namespace Test.Xunit.Project;

public class Class1
{
    [Fact]
    public async Task TestOne()
    {
        await Task.Delay(10000);
        Assert.True(true);
    }
} 
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
      <PackageReference Include="xunit.assert" Version="2.9.3" />
      <PackageReference Include="Xunit.DependencyInjection" Version="9.8.0" />
     <PackageReference Include="xunit" Version="2.9.3" />
    </ItemGroup>
    
     <PropertyGroup>
    <ParallelizationMode>Enhance</ParallelizationMode>
  </PropertyGroup>

    <ItemGroup>
      <None Update="appsettings.json">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
    </ItemGroup>

</Project>

When I run both classes, I expect that they are run sequentially (because MaxParallelThreads = 1), but they run in parallel. Meanwhile, when I use the xunit package without your library, it works perfectly, running as many tests as I specify in MaxParallelThreads.

I saw a similar issue which was closed: https://github.com/pengweiqhca/Xunit.DependencyInjection/issues/111#issuecomment-2109083805, but this does not work for me.

Could you please elaborate and suggest a fix?

StanislavKlo avatar Jan 30 '25 09:01 StanislavKlo

Hi @pengweiqhca, any updates on this?

StanislavKlo avatar Feb 03 '25 09:02 StanislavKlo

9.9.0 fixed.

pengweiqhca avatar Feb 09 '25 09:02 pengweiqhca

Something is amiss with 9.9.0. For me it breaks running tests in parallel. They do run in parallel in 9.7.0.

rramer-web-com avatar Apr 16 '25 16:04 rramer-web-com

In 10.x with xUnit 3, I can't run in parallel too, there's any updates?

gilmar-sales avatar Apr 28 '25 10:04 gilmar-sales

Something is amiss with 9.9.0. For me it breaks running tests in parallel. They do run in parallel in 9.7.0.

@rramer-web-com Try 9.9.1

pengweiqhca avatar May 07 '25 14:05 pengweiqhca

In 10.x with xUnit 3, I can't run in parallel too, there's any updates?

@gilmar-sales Try 10.4.1

pengweiqhca avatar May 07 '25 14:05 pengweiqhca

In 10.x with xUnit 3, I can't run in parallel too, there's any updates?

@gilmar-sales Try 10.4.1

I've figured out what's going on, my tests are spending more time because it's parallelizing tests inside a collection and others collections doesn't run at the same time.

Consider that 4 test collections has only 3 test cases each, 9 of 12 cores are in idle mode. in the 9.9.1 the tests cases and collections run in parallel, using the 12 cores...

TestProject9-9-1.zip TestProject10-4-1.zip

gilmar-sales avatar May 16 '25 18:05 gilmar-sales

I've created a test case to verify this behaviour: https://github.com/gilmar-sales/Xunit.DependencyInjection/commit/f3248c9b13950410423623cdc480ba83fb3c75e3

gilmar-sales avatar May 16 '25 19:05 gilmar-sales

I created an test case to verify this behaviour: gilmar-sales@f3248c9

You should use Assembly Fixtures or Collection Fixtures.

pengweiqhca avatar May 18 '25 12:05 pengweiqhca

In 10.x with xUnit 3, I can't run in parallel too, there's any updates?

@gilmar-sales Try 10.4.1

I've figured out what's going on, my tests are spending more time because it's parallelizing tests inside a collection and others collections doesn't run at the same time.

Consider that 4 test collections has only 3 test cases each, 9 of 12 cores are in idle mode. in the 9.9.1 the tests cases and collections run in parallel, using the 12 cores...

TestProject9-9-1.zip TestProject10-4-1.zip

TestProject10-4-1.zip is fixed with 10.4.2

pengweiqhca avatar May 18 '25 12:05 pengweiqhca

Tested 10.4.2 and worked to both cases, limiting max parallel threads and full parallelization

gilmar-sales avatar May 19 '25 10:05 gilmar-sales