Skip to content

Commit 4573844

Browse files
feat: + Wait until container is ready
1 parent 2f07532 commit 4573844

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

Src/NpgsqlBenchmark/Benchmarks/PostgresBenchmark.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DotNet.Testcontainers.Builders;
22
using Npgsql;
3+
using NpgsqlBenchmark.Helpers;
34
using System;
45
using System.Threading.Tasks;
56
using Testcontainers.PostgreSql;
@@ -24,6 +25,8 @@ protected async Task OneTimeSetUp()
2425
.Build();
2526

2627
await _postgre.StartAsync();
28+
await _postgre.WaitContainerStateRunningAsync(TimeSpan.FromMinutes(1));
29+
await _postgre.WaitResponseAsync(TimeSpan.FromMinutes(1));
2730

2831
await using (var masterConnection = new NpgsqlConnection(_postgre.GetConnectionString()))
2932
{
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using DotNet.Testcontainers.Containers;
2+
using Npgsql;
3+
using System;
4+
using System.Diagnostics;
5+
using System.Threading.Tasks;
6+
using Testcontainers.PostgreSql;
7+
8+
namespace NpgsqlBenchmark.Helpers
9+
{
10+
internal static class ContainerHelpers
11+
{
12+
/// <summary>
13+
/// Wait untill container start
14+
/// </summary>
15+
public static async ValueTask WaitContainerStateRunningAsync(this DockerContainer dockerContainer, TimeSpan timeout)
16+
{
17+
var sw = Stopwatch.StartNew();
18+
while (true)
19+
{
20+
if (dockerContainer.State == TestcontainersStates.Running)
21+
{
22+
break;
23+
}
24+
25+
if (sw.Elapsed >= timeout)
26+
{
27+
throw new Exception($"Container start timeout ({timeout}) exceeded, benchmark stopped, current container state is {dockerContainer.State}.");
28+
}
29+
30+
await Task.Delay(100);
31+
}
32+
}
33+
34+
/// <summary>
35+
/// Wait until the container responds to at least one request
36+
/// </summary>
37+
public static async ValueTask WaitResponseAsync(this PostgreSqlContainer container, TimeSpan timeout)
38+
{
39+
var sw = Stopwatch.StartNew();
40+
while (true)
41+
{
42+
if (sw.Elapsed >= timeout)
43+
{
44+
throw new Exception($"Postgres has not responded to any queries in {timeout}. Container state {container.State}");
45+
}
46+
47+
var connectionString = container.GetConnectionString();
48+
try
49+
{
50+
await using (var conn = new NpgsqlConnection(connectionString))
51+
{
52+
await conn.OpenAsync();
53+
await using var command = conn.CreateCommand();
54+
command.CommandText = "SELECT 1";
55+
var result = await command.ExecuteScalarAsync();
56+
if (result is int value && value == 1)
57+
{
58+
break;
59+
}
60+
}
61+
}
62+
catch
63+
{
64+
// игнорим
65+
}
66+
67+
await Task.Delay(100);
68+
}
69+
}
70+
}
71+
}

Src/NpgsqlBenchmark/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using BenchmarkDotNet.Running;
22
using NpgsqlBenchmark.Benchmarks;
3+
using System.Threading.Tasks;
34

45
namespace NpgsqlBenchmark
56
{
67
internal class Program
78
{
8-
static void Main(string[] args)
9+
static async Task Main(string[] args)
910
{
1011
//BenchmarkRunner.Run<ComparePrepareDapper>();
1112
BenchmarkRunner.Run<CompareDapper>();

0 commit comments

Comments
 (0)