Open Closed

xUnit tests not completing after Abp 7 Update. (hanging tests) #4658


User avatar
0
LW created
  • ABP Framework version: v7.0.2
  • UI type: Angular /
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes / no

We have a big problem with our backend unit tests after we migrated from the 5.3.1 version to the most current (7.0.2) version. Now some of the tests are hanging in execution indefinitely when running large batches of tests at once. The same thing happens whether we run the tests from VS or through the "dotnet test" via cmd. From my investigation, this might be due to using synchronization with async-methods.https://github.com/abpframework/abp/issues/2075#issuecomment-561582158. One obvious place where this is done is the common data seeding in the test base.

private static void SeedTestData(ApplicationInitializationContext context)
{
    if (context == null)
    {
    throw new ArgumentNullException(nameof(context));
    }

    AsyncHelper.RunSync(async () =>
    {
    using (var scope = context.ServiceProvider.CreateScope())
    {
    	await scope.ServiceProvider
    		.GetRequiredService<IDataSeeder>()
    		.SeedAsync();
    }
    });
}

For validation, I created a structure that does the seeding through IAsyncLifetime initialization so the seeding is done properly through the asynchronous call stack. That seemed to mitigate the problem alot but there were still some tests that were left hanging. After that, I disabled the parallel test execution. With these changes, I can get larger batches of tests to finish execution. Forcing non-parallel execution is not ideal, however. Have you seen this problem occur previously? And what can be done to fix the issue? I checked that, we don't use synchronous calls for async methods in our own code.


10 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you add the below code to the test module?

    
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        Configure<FeatureManagementOptions>(options =>
        {
            options.SaveStaticFeaturesToDatabase = false;
            options.IsDynamicFeatureStoreEnabled = false;
        });
        
        Configure<PermissionManagementOptions>(options =>
        {
            options.SaveStaticPermissionsToDatabase = false;
            options.IsDynamicPermissionStoreEnabled = false;
        });
    
        context.Services.AddAlwaysDisableUnitOfWorkTransaction();
    }
    
    
  • User Avatar
    0
    LW created

    hi, that seemed to fix the problem.Awesome, Thank you! Can you please explain why that helps, so I can put a comment in our code?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    We will add the above code in the template, you can ignore it.

  • User Avatar
    0
    LW created

    Ok. that " context.Services.AddAlwaysDisableUnitOfWorkTransaction();" actually brought another problem for us. With some of our integration tests, we use test collection to speed up the test execution. We used the transactional unit of work to control the system state, so the tests were not dependent on each other. System state changes were rollbacked after the test. Now that the transactions are disabled, these tests are now interdepended and some query tests fail because another test has affected the database state. I know that Abp recommends to do the test initilization per test, but with larger test sets, that makes the test excecution slower and slower. Is there any way to control the transaction separately per test now? Is there any other good way to speed up the test excecution with larger amount of integration tests?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    This is mainly the limitation of sqlite, which does not support transactions in transactions.

  • User Avatar
    0
    LW created

    Is this a new limitation? This worked fine earlier.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    In fact, there were similar problems before, The sqlite does not support nested transactions.

  • User Avatar
    0
    LW created

    Ok, thanks. Do you have any suggestions on how to handle our current problem? We didn't have nested transactions problem before, but now we do. What caused the change? Why does the nested transactions problem occur during the tests? As far as I know, Abp unit of work system should be able to recognize if there is a transaction open already.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    you can remove AddAlwaysDisableUnitOfWorkTransaction if you need to use a transaction for your own unit test methods.

  • User Avatar
    0
    LW created

    Yes, that seems to work. I got the impression that that was the fix for the hanging tests, but everything seems to work without it. Thank you for your help!

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09