| | | 1 | | namespace Nabs.Tests.DatabaseTests; |
| | | 2 | | |
| | | 3 | | public sealed class DatabaseContainerRunOnce : XunitTestFramework, IDisposable |
| | | 4 | | { |
| | | 5 | | public const string RunOnceFqn = "Nabs.Tests.DatabaseTests.DatabaseContainerRunOnce"; |
| | | 6 | | public const string RunOnceAssemblyName = "Nabs.Tests.DatabaseTests"; |
| | | 7 | | |
| | | 8 | | private readonly MsSqlContainer _container; |
| | | 9 | | |
| | | 10 | | public DatabaseContainerRunOnce(IMessageSink messageSink) |
| | 1 | 11 | | : base(messageSink) |
| | | 12 | | { |
| | 1 | 13 | | DiagnosticMessageSink.OnMessage(new DiagnosticMessage("Container starting ...")); |
| | | 14 | | |
| | 1 | 15 | | _container = new MsSqlBuilder() |
| | 1 | 16 | | .WithImage("mcr.microsoft.com/mssql/server:2019-latest") |
| | 1 | 17 | | .WithName("nabs-test-mssql-db") |
| | 1 | 18 | | .WithPortBinding(14331, 1433) |
| | 1 | 19 | | .WithPassword("Password123") |
| | 1 | 20 | | .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433)) |
| | 1 | 21 | | .Build(); |
| | | 22 | | |
| | 1 | 23 | | StartContainerAndEnsureReady().GetAwaiter().GetResult(); |
| | | 24 | | |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | public new void Dispose() |
| | | 28 | | { |
| | 1 | 29 | | DiagnosticMessageSink.OnMessage(new DiagnosticMessage("Container stopping ...")); |
| | 1 | 30 | | _container.StopAsync().GetAwaiter().GetResult(); |
| | 1 | 31 | | GC.SuppressFinalize(this); |
| | 1 | 32 | | base.Dispose(); |
| | 1 | 33 | | DiagnosticMessageSink.OnMessage(new DiagnosticMessage("Container stopped!")); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | private async Task StartContainerAndEnsureReady() |
| | | 37 | | { |
| | 1 | 38 | | await _container.StartAsync(); |
| | | 39 | | |
| | 1 | 40 | | var retryCount = 1; |
| | 1 | 41 | | var maxRetries = 5; |
| | 1 | 42 | | var delay = TimeSpan.FromSeconds(10); |
| | 1 | 43 | | var isReady = false; |
| | | 44 | | |
| | 1 | 45 | | while (retryCount <= maxRetries && !isReady) |
| | | 46 | | { |
| | | 47 | | try |
| | | 48 | | { |
| | 1 | 49 | | using var connection = new SqlConnection(_container.GetConnectionString()); |
| | 1 | 50 | | await connection.OpenAsync(); |
| | 1 | 51 | | isReady = true; |
| | 1 | 52 | | } |
| | 0 | 53 | | catch |
| | | 54 | | { |
| | 0 | 55 | | DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Connection failed on attempt {retryCount}. Waiti |
| | | 56 | | // If connection fails, wait for a while and then retry |
| | 0 | 57 | | await Task.Delay(delay); |
| | 0 | 58 | | retryCount++; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | if (!isReady) |
| | | 63 | | { |
| | 0 | 64 | | var failureMessage = $"Failed to establish a connection to the SQL Server container after {maxRetries} retri |
| | 0 | 65 | | DiagnosticMessageSink.OnMessage(new DiagnosticMessage(failureMessage)); |
| | 0 | 66 | | throw new InvalidOperationException(failureMessage); |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | DiagnosticMessageSink.OnMessage(new DiagnosticMessage("Container is ready!")); |
| | 1 | 70 | | } |
| | | 71 | | } |