Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DbContext Models are not cached throughout xUnit test run #11333

Closed
gojanpaolo opened this issue Mar 20, 2018 · 3 comments
Closed

DbContext Models are not cached throughout xUnit test run #11333

gojanpaolo opened this issue Mar 20, 2018 · 3 comments

Comments

@gojanpaolo
Copy link

gojanpaolo commented Mar 20, 2018

See #1906 (comment)

Here are the test results that I got.
image

The CacheTestClassXXX classes use a simplified version of the work around described in #1906 (comment).

Notice that only CacheTestClass1 had the overhead of DbContext model creation.
But the TestClassXXX classes all had the overhead.

Steps to reproduce

  1. Create Class Library (.NET Framework 4.6.1) project.
  2. Add Dependencies (Nuget)... see below for list of dependencies
  3. Paste code below..
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using Xunit;

public abstract class TestBase : IDisposable
{
    private readonly SqliteConnection _connection;
    protected readonly DbContextOptions<DbContext> _options;
    public TestBase()
    {
        _connection = new SqliteConnection("DataSource=:memory:");
        _connection.Open();
        _options = new DbContextOptionsBuilder<DbContext>()
            .UseSqlite(_connection)
            .Options;
        using (var context = new DbContext(_options))
        {
            context.Database.EnsureCreated();
        }
    }
    public void Dispose()
    {
        _connection.Close();
    }

    [Fact]
    public void DummyTestMethod() { }
}

public class TestClass1 : TestBase { }

public class TestClass2 : TestBase { }

public class TestClass3 : TestBase { }

[Collection(nameof(DbContextCache))]
public class CacheTestClass1 : TestBase { }

[Collection(nameof(DbContextCache))]
public class CacheTestClass2 : TestBase { }

[Collection(nameof(DbContextCache))]
public class CacheTestClass3 : TestBase { }

[CollectionDefinition(nameof(DbContextCache))]
public class DbContextCache : ICollectionFixture<object> { }

Dependencies

microsoft.entityframeworkcore.sqlite v2.0.2
xunit v2.3.1
xunit.runner.visualstuio v2.3.1

Further technical details

.NET Framework 4.6.1
VS 2017 15.6.1
Windows 7

cc: @ajcvickers

@ajcvickers
Copy link
Member

@gojanpaolo I was able to reproduce this. The issue is that xunit (or the runner) is creating a new app domain for each test collection. This can be good for isolation, but does defeat EF caching. I believe that there are options to turn of this behavior in xunit, so that might be cleaner than what you have above. Also, when running on .NET Core there are no app domains, so xunit will never have this behavior there. (Unless they simulate it with multiple processes or something like that.) Closing as there is no EF actionable issue here.

@smitpatel
Copy link
Member

For more info on how to configure xunit not to use appdomains
https://xunit.github.io/docs/configuring-with-json

@gojanpaolo
Copy link
Author

@ajcvickers I see. Thank you for taking the time to look into this. :)
@smitpatel Awesome! Thanks for the link.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants