The project shows thread-safety issues with WebApplicationFactory<T> (reported here 8629).
According to the documentation, the WebApplicationFactory<T> is used to create the instance of the TestServer and one or more instances of HttpClient that can be used in the test.
This repository presents that while WebApplicationFactory<T> creates one instance of the TestServer when executed from single thread, it is not thread-safe.
To prove it, a sample WebApi project has been created (with dotnet new webapi) and it's Startup class has been extended with a NumberOfCalls counter capturing number of calls to Configure() method as well as small delay emulating more complex startup operation.
A three tests has been made to verify the WebApplicationFactory<T> behaviour.
- WebApplicationFactory_should_initialize_Api_once test verifies that if the factory is used from single thread, the underlying
TestServeris initialized once. This test pass. - WebApplicationFactory_Api_initialization_should_be_thread_safe test verifies if factory initializes only one server instance if it's accessed from multiple threads. This tests fails on my machine, showing that
TestServerwas initialized 8 times (I have 8 cores). - Server_should_be_available test is rather an open question. The
WebApplicationFactory<T>offersServerproperty to access underlyingTestServer. When this property is accessed just after instantiation, it returnsnull. As there is no explicit method onWebApplicationFactory<T>to ensure/enforce test server initialization, it looks like theServerproperty should always return the server instance. The question here is ifServerreturning null is a bug or this is expected behaviour, and if that is expected, then how to properly ensure the test server is initialized?