Skip to content

Commit d783b51

Browse files
committed
Fixed slow PostgreSQL with host=127.0.0.1
1 parent dffc037 commit d783b51

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

BenchmarkPostgreSql/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Running;
33
using DataLayer.BookApp.EfCode;
4-
using Microsoft.EntityFrameworkCore;
54
using Npgsql;
6-
using Respawn;
75
using Test.Helpers;
86
using TestSupport.EfHelpers;
97

108
public class Program
119
{
12-
private const string _connectionString = "host=localhost;Database=TestSupportBenchmark-TestSupport;Username=postgres;Password=LetMeIn";
10+
1311
private BookContext _context;
1412

1513
[GlobalSetup]
1614
public void Setup()
1715
{
1816
NpgsqlConnection.ClearAllPools();
1917

20-
var builder = new DbContextOptionsBuilder<BookContext>();
21-
builder.UseNpgsql(_connectionString);
22-
_context = new BookContext(builder.Options);
18+
var options = this.CreatePostgreSqlUniqueClassOptions<BookContext>();
19+
_context = new BookContext(options);
2320
_context.Database.EnsureCreated();
2421
}
2522

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ConnectionStrings": {
3-
"PostgreSqlConnection": "host=localhost;Database=TestSupportBenchmark-Test;Username=postgres;Password=LetMeIn"
3+
"PostgreSqlConnection": "host=127.0.0.1;Database=Benchmark-Test;Username=postgres;Password=LetMeIn"
44
}
55
}
66

Test/UnitTests/TestDataLayer/TestEnsureCleanPostgreSql.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,37 @@ public void TestEnsureCleanApplyMigrationOk()
205205
}
206206

207207
[Fact]
208-
public async Task TestRespawnOk()
208+
public async Task TestRespawnWithCheckDbExists()
209209
{
210210
//SETUP
211211
var options = this.CreatePostgreSqlUniqueMethodOptions<BookContext>();
212212
using var context = new BookContext(options);
213213
context.Database.EnsureCreated();
214214

215215
//ATTEMPT
216-
using(new TimeThings(_output))
216+
using (new TimeThings(_output))
217217
await context.EnsureCreatedAndEmptyPostgreSqlAsync();
218218

219219
//VERIFY
220220
context.Books.Count().ShouldEqual(0);
221221
}
222222

223+
[Fact]
224+
public async Task TestRespawnNoCheckDbExists()
225+
{
226+
//SETUP
227+
var options = this.CreatePostgreSqlUniqueMethodOptions<BookContext>();
228+
using var context = new BookContext(options);
229+
context.Database.EnsureCreated();
230+
231+
//ATTEMPT
232+
using (new TimeThings(_output))
233+
await context.EnsureCreatedAndEmptyPostgreSqlAsync(true);
234+
235+
//VERIFY
236+
context.Books.Count().ShouldEqual(0);
237+
}
238+
223239

224240
private int CountTablesInDatabase(DbContext context)
225241
{

Test/UnitTests/TestDataLayer/TestPostgreSqlHelpers.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using DataLayer.BookApp.EfCode;
88
using Microsoft.EntityFrameworkCore;
99
using Npgsql;
10+
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
1011
using Test.Helpers;
1112
using TestSupport.Attributes;
1213
using TestSupport.EfHelpers;
@@ -77,6 +78,24 @@ public void TestEnsureDeletedEnsureCreatedOk()
7778
context.Books.Count().ShouldEqual(0);
7879
}
7980

81+
[Fact]
82+
public void TestEnsureCreatedExistingDbOk()
83+
{
84+
//SETUP
85+
var options = this.CreatePostgreSqlUniqueClassOptions<BookContext>();
86+
using var context = new BookContext(options);
87+
88+
context.Database.EnsureCreated();
89+
90+
//ATTEMPT
91+
using (new TimeThings(_output, "EnsureCreated when database exists"))
92+
{
93+
context.Database.EnsureCreated();
94+
}
95+
96+
//VERIFY
97+
}
98+
8099
[Fact]
81100
public void TestEnsureCleanExistingDatabaseOk()
82101
{

Test/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ConnectionStrings": {
33
"UnitTestConnection": "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True;MultipleActiveResultSets=true",
4-
"PostgreSqlConnection": "host=localhost;Database=Test-Test;Username=postgres;Password=LetMeIn",
4+
"PostgreSqlConnection": "host=127.0.0.1;Database=Test-Test;Username=postgres;Password=LetMeIn",
55
"BookOrderConnection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=EfCore.TestSupport-Test_ComparerBooksAndOrders;Integrated Security=True;MultipleActiveResultSets=True"
66
},
77
"MyInt": 1,

TestSupport/EfHelpers/CleanDatabaseExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ FOR r IN (SELECT nspname FROM pg_namespace WHERE nspname NOT IN ('pg_toast', 'pg
6161
dropPublicSchemaCommand.ExecuteNonQuery();
6262
}
6363

64-
if(setUpSchema)
64+
if (setUpSchema)
6565
databaseFacade.EnsureCreated();
6666
}
6767

0 commit comments

Comments
 (0)