From a364fc9dfa88591765fe6a8a201b514f1566d1f5 Mon Sep 17 00:00:00 2001
From: Alexander Zabel <a.zabel@reply.de>
Date: Thu, 8 Feb 2024 13:19:19 +0100
Subject: [PATCH 1/2] Add CosmosDbContainerWaitStrategy for better readiness
 check

Introduced the CosmosDbContainerWaitStrategy to boost the readiness check by connecting to a specific URL instead of looking for a log message. The existing wait strategy in the CosmosDbBuilder was modified to use this new strategy, thereby making the readiness check more reliable.
---
 .../CosmosDbBuilder.cs                        |  2 +-
 .../CosmosDbContainerWaitStrategy.cs          | 22 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs

diff --git a/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs b/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs
index b25393ab4..99b8ec0ea 100644
--- a/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs
+++ b/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs
@@ -45,7 +45,7 @@ protected override CosmosDbBuilder Init()
         return base.Init()
             .WithImage(CosmosDbImage)
             .WithPortBinding(CosmosDbPort, true)
-            .WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Started\\r?\\n"));
+            .WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new CosmosDbContainerWaitStrategy()));
     }
 
     /// <inheritdoc />
diff --git a/src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs b/src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs
new file mode 100644
index 000000000..524e0ced7
--- /dev/null
+++ b/src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs
@@ -0,0 +1,22 @@
+namespace Testcontainers.CosmosDb;
+
+public sealed class CosmosDbContainerWaitStrategy : IWaitUntil
+{
+    private const string ReadyProbeUrl = "https://localhost/_explorer/index.html";
+        
+    public async Task<bool> UntilAsync(IContainer container)
+    {
+        try
+        {
+            using var httpClient = ((CosmosDbContainer)container).HttpClient;
+
+            var result = await httpClient.GetAsync(ReadyProbeUrl);
+                
+            return result.IsSuccessStatusCode;
+        }
+        catch (Exception)
+        {
+            return false;
+        }
+    }
+}

From 3d9f0ce67b57bf8e62d0f02f34990dcbd5a48165 Mon Sep 17 00:00:00 2001
From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
Date: Thu, 8 Feb 2024 19:24:54 +0100
Subject: [PATCH 2/2] chore: Move wait strategy to builder class

---
 .../CosmosDbBuilder.cs                        | 31 ++++++++++++++++++-
 .../CosmosDbContainerWaitStrategy.cs          | 22 -------------
 2 files changed, 30 insertions(+), 23 deletions(-)
 delete mode 100644 src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs

diff --git a/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs b/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs
index 99b8ec0ea..d40116625 100644
--- a/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs
+++ b/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs
@@ -45,7 +45,7 @@ protected override CosmosDbBuilder Init()
         return base.Init()
             .WithImage(CosmosDbImage)
             .WithPortBinding(CosmosDbPort, true)
-            .WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new CosmosDbContainerWaitStrategy()));
+            .WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil()));
     }
 
     /// <inheritdoc />
@@ -65,4 +65,33 @@ protected override CosmosDbBuilder Merge(CosmosDbConfiguration oldValue, CosmosD
     {
         return new CosmosDbBuilder(new CosmosDbConfiguration(oldValue, newValue));
     }
+
+    /// <inheritdoc cref="IWaitUntil" />
+    private sealed class WaitUntil : IWaitUntil
+    {
+        /// <inheritdoc />
+        public async Task<bool> UntilAsync(IContainer container)
+        {
+            // CosmosDB's preconfigured HTTP client will redirect the request to the container.
+            const string requestUri = "https://localhost/_explorer/emulator.pem";
+
+            var httpClient = ((CosmosDbContainer)container).HttpClient;
+
+            try
+            {
+                using var httpResponse = await httpClient.GetAsync(requestUri)
+                    .ConfigureAwait(false);
+
+                return httpResponse.IsSuccessStatusCode;
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+            finally
+            {
+                httpClient.Dispose();
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs b/src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs
deleted file mode 100644
index 524e0ced7..000000000
--- a/src/Testcontainers.CosmosDb/CosmosDbContainerWaitStrategy.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-namespace Testcontainers.CosmosDb;
-
-public sealed class CosmosDbContainerWaitStrategy : IWaitUntil
-{
-    private const string ReadyProbeUrl = "https://localhost/_explorer/index.html";
-        
-    public async Task<bool> UntilAsync(IContainer container)
-    {
-        try
-        {
-            using var httpClient = ((CosmosDbContainer)container).HttpClient;
-
-            var result = await httpClient.GetAsync(ReadyProbeUrl);
-                
-            return result.IsSuccessStatusCode;
-        }
-        catch (Exception)
-        {
-            return false;
-        }
-    }
-}