Using AppHost in multiple integration test files causes problems running concurrently

I have multiple integration test files that each create the servicestack AppHost for the tests that file.

Running a single file of tests works just fine, but when I run multiple test files at the same time, I start getting errors about items in my database that already exist. This is because in each test file, I create an in-memory database and add some users to it in the AppHost container.

So I think what happens is that the first test file adds the users to the database, and at the same time the second file tries to do that same, but must be sharing an instance of AppHost somehow in the background and thus running into this problem.

I’m setting up the integration test like the documentation How to write Unit & Integration tests

What’s you recommendation for running multiple integration test files that each spin up AppHost?

Concurrent tests running their own AppHost instance will hit these kinds of issues, likely due to anything that accesses the AppHost instance via HostContext singleton since they will be all running under the same process, and probably other issues related to running multiple AppHost instances in a single process.

If you absolutely need to run all your integration tests concurrently, I’d suggest isolating to separate processes via running multiple dotnet test commands with filters concurrently or against isolated environments like containers that can be easily spun up and down based on their test requirements.

You will likely only get a significant speed up from the first approach and if your tests are somewhat long running, but it will depend heavily on your specific test cases.

1 Like