xUnit InMemoryDatabase Test failure: InvalidOperationException

687 Views Asked by At

I run Unit-Tests on TFS in a CI environment, today it failed with the following exception:

Error:
  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.
Stacktrace:
    at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
    at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
    at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.GetDisplayName(Type type)
    at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FindEntityType(Type type)
    at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.EntityEqualityRewritingExpressionVisitor.RewriteEntityEquality(ExpressionType nodeType, Expression left, Expression right)
    at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.EntityEqualityRewritingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
    at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
    at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
    at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
    at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.EntityEqualityRewritingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
    at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
    at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
    at Remotion.Linq.Clauses.WhereClause.TransformExpressions(Func`2 transformation)
    at Remotion.Linq.QueryModel.TransformExpressions(Func`2 transformation)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryOptimizer.Optimize(QueryCompilationContext queryCompilationContext, QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()
    at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
    at System.Linq.Queryable.Any[TSource](IQueryable`1 source)
    at ModelNamespace.SomeRepository.TestMethod()
    at TestNamespace.SomeRepositoryTest.TestMethod_Test()

After that I started the same build on the same commit again and it succeeded. Odd isn't it?

How the Tests are built up

To set up InMemoryDatabases, we use the following helper Class:

public class DbContextTestHelper
{
    public static DbContextOptions<CustomContext> PrepareData(Action<CustomContext> createData)
    {
        var options = CreateDbContextOptions();
        SaveData(options, createData);
        return options;
    }

    private static void SaveData(DbContextOptions<CustomContext> options, Action<CustomContext> createData)
    {
        // Insert seed data into the database using one instance of the context
        using (var context = new CustomContext(options))
        {
            createData.Invoke(context);
            context.SaveChanges();
        }
    }

    private static DbContextOptions<CustomContext> CreateDbContextOptions()
    {
        return new DbContextOptionsBuilder<CustomContext>()
            .UseInMemoryDatabase(Guid.NewGuid()
                .ToString()) //Database with same name gets reused, so let's isolate the tests from each other...
            .Options;
    }
}

So now we have many Tests we build up like this:

public class SomeRepositoryTest
{
    [Fact]
    public async Task TestMethod_Test()
    {
        var options = DbContextTestHelper.PrepareData(context =>
        {
            // do some initialisation
        });
        // Use a clean instance of the context to run the test
        using (var context = new CustomContext(options))
        {
            var testee = new SomeRepository(context);
            await testee.TestMethod(); // InvalidOperationException from above
            // Assert something
        }
    }
}

Of course there are many cases where we create those DbContextOptions<CustomContext> and naturally they run parallel because of the built-in functionality that xUnit provides (yes of course not in the same class but for many test classes).

My Question

As far as I can tell there are few possibilities of why this can sometimes cause an exception:

  • the GUID's generated to ensure different instances of InMemoryDatabases are duplicates. However I'd rather not believe this.
  • there is a bug in the InMememory Database System
  • We are using InMemoryDatabases wrong

Do you know what's going on?

Edit 1:

As Requested, here's the TestMethod() where System.Linq.Queryable.Any[TSource](IQueryable'1 source) gets called.

    public void TestMethod(SomeObject someObject, List<DateTime> dates, bool someOption)
    {
        var res = _dbContext.SomeSet.Where(o =>
            o.SomeObject.Equals(someObject) && dates.Contains(o.DateTime) && o.SomeOption == someOption);
        if (res.Any()) // at System.Linq.Queryable.Any[TSource](IQueryable`1 source) 
        {
            _dbContext.SomeSet.RemoveRange(res);
        }
    }
0

There are 0 best solutions below