Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Issues with running tests in parallel



It seems that attempting to run otpimizely solution in parallel using aspnetCore.Mvc.Testing and its webApplicationFactory to run component tests results in exceptions:

System.ArgumentException : An item with the same key has already been added. Key: ConfigureContainer

  Stack Trace: 
Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
Dictionary`2.Add(TKey key, TValue value)
TimeMeters.Start(String key)
ModuleNode.Execute(Action a, String key)
ModuleNode.ConfigureContainer(ServiceConfigurationContext context)
InitializationEngine.ConfigureCurrentModules(Boolean final)
InitializationEngine.ConfigureModules(IEnumerable`1 modules)
ServiceCollectionExtensions.AddCmsHost(IServiceCollection services)
ServiceCollectionExtensions.AddCms(IServiceCollection services)
System.InvalidOperationException : Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.

  Stack Trace: 
Dictionary`2.FindValue(TKey key)
Dictionary`2.TryGetValue(TKey key, TValue& value)
AssemblyScanner.HasEPiServerFrameworkDependency(RuntimeLibrary library, IDictionary`2 allLibraries, IDictionary`2 resolvedAssemblies)
<>c__DisplayClass4_0.<GetScannableAssemblies>b__1(RuntimeLibrary library)
Enumerable.ToArray[TSource](IEnumerable`1 source)

It is random which exception i will get but it seems that there is static list of something somewhere inside.

For now walkaround is to run tests sequentially but this is more a quick hack than a solution.

Edited, Nov 29, 2022 7:39

How's running tests sequentially a quick hack? I think it's a valid solution. 

Nov 29, 2022 9:42

Parallel execution of tests is so baked into test runs that xUnit doesn't even have flag to run tests in series - you walkaround it by putting tests into one collection.

Not being able to run tests in parallel means that your code is probably not thread safe, and it is considered a major red flag.

Nov 30, 2022 7:21

OK that's new to me, I have been not using xUnit for quite a long time, and i assume they shifted to parallel execution in later version.

What you are trying to say, I suppose, is that TimeMeters.Start and AssemblyScanner.HasEPiServerFrameworkDependency are not thread safe. they aren't. however if they should be thread safe or not, is quite arguable. they are meant to be used by initialization process so there should be no multi threading involved.

which kind of tests you are trying to run? Why does it need to initialize the framework? 

Nov 30, 2022 9:59

As i be live i've written i'm using mvc testkit that runs host:

        public async Task PageReturns200(string url)
            var suite = new HttpTestWorker();
            var response = await suite.HttpClient.GetAsync(url);
            Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);

        protected override IHost _host { get; }
        private HttpClient _httpClient;
        public HttpClient HttpClient => _httpClient;

        private HttpTestWorker()
            var factory = new CustomWebApplicationFactory<Program>(Register);
            _httpClient = factory.CreateClient();
            _host = factory.GetHost();

        protected void Register(IServiceCollection services)
        public static void ConfigureBasicMocks(this IServiceCollection services)
        private static void MockDatabase(IServiceCollection services)
            var options = services.Where(x => x.ServiceType.Name.Contains("DbContextOptions")).ToList();

            foreach (var service in options)

            var optionsBuilder = new DbContextOptionsBuilder<KpiDatabaseContext>();


            var optionsBuilder2 = new DbContextOptionsBuilder<EPiServer.Marketing.Testing.Dal.DatabaseContext>();


            var optionsBuilder3 = new DbContextOptionsBuilder();

and from mvc test kit:
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
Edited, Dec 01, 2022 13:15

also currently overriding dbcontextoptions doesn't seem to work as expected

So why i'm doing initialziation? - because i'm running whole host inmemory.

Why i'm running entire host inmemory? - because epi is a minefield of extension methods with service locators in them and other traps like "content area", that makes unit tests as joyful as prostate exam.

Edited, Dec 01, 2022 13:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.