diff --git a/src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs b/src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs
index 2e94f682c4..f59ba51888 100644
--- a/src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs
+++ b/src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs
@@ -28,6 +28,7 @@ public RabbitMQHealthCheck(RabbitMQHealthCheckOptions options)
///
public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
+ // TODO: cancellationToken unused, see https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/714
try
{
using var model = EnsureConnection().CreateModel();
@@ -52,9 +53,14 @@ private IConnection EnsureConnection()
{
Uri = _options.ConnectionUri,
AutomaticRecoveryEnabled = true,
- UseBackgroundThreadsForIO = true,
+ UseBackgroundThreadsForIO = true
};
+ if (_options.RequestedConnectionTimeout is not null)
+ {
+ ((ConnectionFactory)factory).RequestedConnectionTimeout = _options.RequestedConnectionTimeout.Value;
+ }
+
if (_options.Ssl is not null)
{
((ConnectionFactory)factory).Ssl = _options.Ssl;
diff --git a/src/HealthChecks.Rabbitmq/RabbitMQHealthCheckOptions.cs b/src/HealthChecks.Rabbitmq/RabbitMQHealthCheckOptions.cs
index 802f44c570..d164fe94f3 100644
--- a/src/HealthChecks.Rabbitmq/RabbitMQHealthCheckOptions.cs
+++ b/src/HealthChecks.Rabbitmq/RabbitMQHealthCheckOptions.cs
@@ -34,4 +34,9 @@ public class RabbitMQHealthCheckOptions
/// Must be used in conjunction with the property.
///
public SslOption? Ssl { get; set; }
+
+ ///
+ /// Timeout setting for connection attempts.
+ ///
+ public TimeSpan? RequestedConnectionTimeout { get; set; }
}
diff --git a/test/HealthChecks.RabbitMQ.Tests/Functional/RabbitHealthCheckTests.cs b/test/HealthChecks.RabbitMQ.Tests/Functional/RabbitHealthCheckTests.cs
index 23abf311fe..6c33c26c69 100644
--- a/test/HealthChecks.RabbitMQ.Tests/Functional/RabbitHealthCheckTests.cs
+++ b/test/HealthChecks.RabbitMQ.Tests/Functional/RabbitHealthCheckTests.cs
@@ -272,4 +272,30 @@ public async Task two_rabbitmq_health_check()
response1.StatusCode.ShouldBe(HttpStatusCode.OK);
response2.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
}
+
+ // https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/714
+ [Fact]
+ public async Task should_respect_timeout()
+ {
+ var services = new ServiceCollection();
+
+ services
+ .AddLogging()
+ .AddHealthChecks()
+ .AddRabbitMQ(opt =>
+ {
+ opt.RequestedConnectionTimeout = TimeSpan.FromSeconds(1);
+ opt.ConnectionUri = new Uri($"amqps://user:pwd@invalid-host:5672");
+ },
+ timeout: TimeSpan.FromSeconds(10));
+
+ using var provider = services.BuildServiceProvider();
+ var healthCheckService = provider.GetRequiredService();
+ var start = DateTime.Now;
+ using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
+ var report = await healthCheckService.CheckHealthAsync(cts.Token).ConfigureAwait(false);
+ report.Status.ShouldBe(HealthStatus.Unhealthy);
+ var end = DateTime.Now;
+ (end - start).ShouldBeLessThan(TimeSpan.FromSeconds(10));
+ }
}
diff --git a/test/HealthChecks.RabbitMQ.Tests/HealthChecks.Rabbitmq.approved.txt b/test/HealthChecks.RabbitMQ.Tests/HealthChecks.Rabbitmq.approved.txt
index 7cdc844702..e4e522964f 100644
--- a/test/HealthChecks.RabbitMQ.Tests/HealthChecks.Rabbitmq.approved.txt
+++ b/test/HealthChecks.RabbitMQ.Tests/HealthChecks.Rabbitmq.approved.txt
@@ -11,6 +11,7 @@ namespace HealthChecks.RabbitMQ
public RabbitMQ.Client.IConnection? Connection { get; set; }
public RabbitMQ.Client.IConnectionFactory? ConnectionFactory { get; set; }
public System.Uri? ConnectionUri { get; set; }
+ public System.TimeSpan? RequestedConnectionTimeout { get; set; }
public RabbitMQ.Client.SslOption? Ssl { get; set; }
}
}