Skip to content

Commit aadcd60

Browse files
committed
Fix multithreaded test initialisation.
Each test class instantiates its own copy of DatabaseFixture (in parallel); we only need the setup code to execute once.
1 parent a685288 commit aadcd60

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

tests/SideBySide/DatabaseFixture.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,43 @@ public class DatabaseFixture : IDisposable
1111
{
1212
public DatabaseFixture()
1313
{
14-
// increase the number of worker threads to reduce number of spurious failures from threadpool starvation
14+
lock (s_lock)
15+
{
16+
if (!s_isInitialized)
17+
{
18+
// increase the number of worker threads to reduce number of spurious failures from threadpool starvation
1519
#if NETCOREAPP1_1_2
16-
// from https://stackoverflow.com/a/42982698
17-
typeof(ThreadPool).GetMethod("SetMinThreads", BindingFlags.Public | BindingFlags.Static).Invoke(null, new object[] { 64, 64 });
20+
// from https://stackoverflow.com/a/42982698
21+
typeof(ThreadPool).GetMethod("SetMinThreads", BindingFlags.Public | BindingFlags.Static).Invoke(null, new object[] { 64, 64 });
1822
#else
19-
ThreadPool.SetMinThreads(64, 64);
23+
ThreadPool.SetMinThreads(64, 64);
2024
#endif
2125

22-
var csb = AppConfig.CreateConnectionStringBuilder();
23-
var connectionString = csb.ConnectionString;
24-
var database = csb.Database;
25-
csb.Database = "";
26-
using (var db = new MySqlConnection(csb.ConnectionString))
27-
{
28-
db.Open();
29-
using (var cmd = db.CreateCommand())
30-
{
31-
cmd.CommandText = $"create schema if not exists {database};";
32-
cmd.ExecuteNonQuery();
33-
34-
if (!string.IsNullOrEmpty(AppConfig.SecondaryDatabase))
26+
var csb = AppConfig.CreateConnectionStringBuilder();
27+
var database = csb.Database;
28+
csb.Database = "";
29+
using (var db = new MySqlConnection(csb.ConnectionString))
3530
{
36-
cmd.CommandText = $"create schema if not exists {AppConfig.SecondaryDatabase};";
37-
cmd.ExecuteNonQuery();
31+
db.Open();
32+
using (var cmd = db.CreateCommand())
33+
{
34+
cmd.CommandText = $"create schema if not exists {database};";
35+
cmd.ExecuteNonQuery();
36+
37+
if (!string.IsNullOrEmpty(AppConfig.SecondaryDatabase))
38+
{
39+
cmd.CommandText = $"create schema if not exists {AppConfig.SecondaryDatabase};";
40+
cmd.ExecuteNonQuery();
41+
}
42+
}
43+
db.Close();
3844
}
45+
46+
s_isInitialized = true;
3947
}
40-
db.Close();
4148
}
4249

43-
Connection = new MySqlConnection(connectionString);
50+
Connection = new MySqlConnection(AppConfig.ConnectionString);
4451
}
4552

4653
public MySqlConnection Connection { get; }
@@ -57,5 +64,8 @@ protected virtual void Dispose(bool disposing)
5764
Connection.Dispose();
5865
}
5966
}
67+
68+
static object s_lock = new object();
69+
static bool s_isInitialized;
6070
}
6171
}

0 commit comments

Comments
 (0)