forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUntilDatabaseIsAvailable.cs
More file actions
40 lines (36 loc) · 1.25 KB
/
UntilDatabaseIsAvailable.cs
File metadata and controls
40 lines (36 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Data.Common;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
internal class UntilDatabaseIsAvailable : IWaitUntil
{
private readonly DbProviderFactory _dbProviderFactory;
public UntilDatabaseIsAvailable(DbProviderFactory dbProviderFactory)
{
_dbProviderFactory = dbProviderFactory;
}
public async Task<bool> UntilAsync(IContainer container)
{
if (container is not IDatabaseContainer dbContainer)
{
throw new NotSupportedException($"The \"UntilDatabaseIsAvailable\" wait strategy is only available on database containers. " +
$"{container.GetType().FullName} does not implement the {nameof(IDatabaseContainer)} interface.");
}
using (var connection = _dbProviderFactory.CreateConnection() ?? throw new InvalidOperationException($"{_dbProviderFactory.GetType().FullName}.CreateConnection() returned null."))
{
connection.ConnectionString = dbContainer.GetConnectionString();
try
{
await connection.OpenAsync();
return true;
}
catch
{
return false;
}
}
}
}
}