-
-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathUntilDatabaseIsAvailable.cs
More file actions
57 lines (50 loc) · 1.53 KB
/
UntilDatabaseIsAvailable.cs
File metadata and controls
57 lines (50 loc) · 1.53 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Data.Common;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
internal sealed 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 can only be used with database containers. " +
$"The provided container type '{container.GetType().FullName}' does not implement '{nameof(IDatabaseContainer)}'.");
}
var connection = _dbProviderFactory.CreateConnection();
if (connection == null)
{
throw new InvalidOperationException(
$"Failed to create a database connection. The factory '{_dbProviderFactory.GetType().FullName}' returned null from 'CreateConnection()'.");
}
try
{
connection.ConnectionString = dbContainer.GetConnectionString();
await connection.OpenAsync()
.ConfigureAwait(false);
return true;
}
catch
{
return false;
}
finally
{
#if NETSTANDARD2_0
connection.Dispose();
#else
await connection.DisposeAsync()
.ConfigureAwait(false);
#endif
}
}
}
}