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 TestServer is 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 TestServer was initialized 8 times (I have 8 cores).
  • Server_should_be_available test is rather an open question. The WebApplicationFactory<T> offers Server property to access underlying TestServer. When this property is accessed just after instantiation, it returns null. As there is no explicit method on WebApplicationFactory<T> to ensure/enforce test server initialization, it looks like the Server property should always return the server instance. The question here is if Server returning null is a bug or this is expected behaviour, and if that is expected, then how to properly ensure the test server is initialized?