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

Correctly set pool size when using AddPooledDbContextFactory #23983

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,11 @@ public static IServiceCollection AddPooledDbContextFactory<TContext>(
{
Check.NotNull(optionsAction, nameof(optionsAction));

return AddPooledDbContextFactory<TContext>(serviceCollection, (_, ob) => optionsAction(ob));
var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23889", out var enabled) && enabled;

return useOldBehavior
? AddPooledDbContextFactory<TContext>(serviceCollection, (_, ob) => optionsAction(ob))
: AddPooledDbContextFactory<TContext>(serviceCollection, (_, ob) => optionsAction(ob), poolSize);
}

/// <summary>
Expand Down
134 changes: 131 additions & 3 deletions test/EFCore.SqlServer.FunctionalTests/DbContextPoolingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand All @@ -26,7 +27,41 @@ namespace Microsoft.EntityFrameworkCore
{
public class DbContextPoolingTest : IClassFixture<NorthwindQuerySqlServerFixture<NoopModelCustomizer>>
{
private static IServiceProvider BuildServiceProvider<TContextService, TContext>(int poolSize = 32)
private static IServiceProvider BuildServiceProvider<TContextService, TContext>()
where TContextService : class
where TContext : DbContext, TContextService
=> new ServiceCollection()
.AddDbContextPool<TContextService, TContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).AddDbContextPool<ISecondContext, SecondContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).BuildServiceProvider();

private static IServiceProvider BuildServiceProvider<TContext>()
where TContext : DbContext
=> new ServiceCollection()
.AddDbContextPool<TContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).AddDbContextPool<SecondContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).BuildServiceProvider();

private static IServiceProvider BuildServiceProviderWithFactory<TContext>()
where TContext : DbContext
=> new ServiceCollection()
.AddPooledDbContextFactory<TContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).AddDbContextPool<SecondContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).BuildServiceProvider();

private static IServiceProvider BuildServiceProvider<TContextService, TContext>(int poolSize)
where TContextService : class
where TContext : DbContext, TContextService
=> new ServiceCollection()
Expand All @@ -41,7 +76,7 @@ private static IServiceProvider BuildServiceProvider<TContextService, TContext>(
.EnableServiceProviderCaching(false),
poolSize).BuildServiceProvider();

private static IServiceProvider BuildServiceProvider<TContext>(int poolSize = 32)
private static IServiceProvider BuildServiceProvider<TContext>(int poolSize)
where TContext : DbContext
=> new ServiceCollection()
.AddDbContextPool<TContext>(
Expand All @@ -56,7 +91,7 @@ private static IServiceProvider BuildServiceProvider<TContext>(int poolSize = 32
poolSize)
.BuildServiceProvider();

private static IServiceProvider BuildServiceProviderWithFactory<TContext>(int poolSize = 32)
private static IServiceProvider BuildServiceProviderWithFactory<TContext>(int poolSize)
where TContext : DbContext
=> new ServiceCollection()
.AddPooledDbContextFactory<TContext>(
Expand Down Expand Up @@ -150,13 +185,105 @@ public void Invalid_pool_size()
Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProvider<PooledContext>(poolSize: -1));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProvider<IPooledContext, PooledContext>(poolSize: 0));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProvider<IPooledContext, PooledContext>(poolSize: -1));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProviderWithFactory<PooledContext>(poolSize: 0));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProviderWithFactory<PooledContext>(poolSize: -1));
}

[ConditionalFact]
public void Validate_pool_size()
{
var serviceProvider = BuildServiceProvider<PooledContext>(poolSize: 64);

using var scope = serviceProvider.CreateScope();

Assert.Equal(
64,
scope.ServiceProvider
.GetRequiredService<PooledContext>()
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_service_interface()
{
var serviceProvider = BuildServiceProvider<IPooledContext, PooledContext>(poolSize: 64);

using var scope = serviceProvider.CreateScope();

Assert.Equal(
64,
((DbContext)scope.ServiceProvider
.GetRequiredService<IPooledContext>())
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_factory()
{
var serviceProvider = BuildServiceProviderWithFactory<PooledContext>(poolSize: 64);

using var context = serviceProvider.GetRequiredService<IDbContextFactory<PooledContext>>().CreateDbContext();

Assert.Equal(
64,
context.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_default()
{
var serviceProvider = BuildServiceProvider<PooledContext>();

using var scope = serviceProvider.CreateScope();

Assert.Equal(
128,
scope.ServiceProvider
.GetRequiredService<PooledContext>()
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_service_interface_default()
{
var serviceProvider = BuildServiceProvider<IPooledContext, PooledContext>();

using var scope = serviceProvider.CreateScope();

Assert.Equal(
128,
((DbContext)scope.ServiceProvider
.GetRequiredService<IPooledContext>())
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_factory_default()
{
var serviceProvider = BuildServiceProviderWithFactory<PooledContext>();

using var context = serviceProvider.GetRequiredService<IDbContextFactory<PooledContext>>().CreateDbContext();

Assert.Equal(
128,
context.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalTheory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -619,6 +746,7 @@ private object GetContextEventField(DbContext context, string eventName)
=> typeof(DbContext)
.GetField(eventName, BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(context);

private bool _changeTracker_OnTracked;

private void ChangeTracker_OnTracked(object sender, EntityTrackedEventArgs e)
Expand Down