From f60355de6c7fc7c214e0a1b69213303afaaac886 Mon Sep 17 00:00:00 2001 From: Thomas Malowanczyk Date: Wed, 9 Aug 2023 17:00:25 +0200 Subject: [PATCH] refactor: simplify HealthCheck calls in service bus test HealthCheck was always called after setting it up in helper methods so to reduce boiler plate the call to health check is moved intothe helper method. Only a few had a substitute setup inbetween, but these could be moved to earlier in the respective tests --- .../AzureServiceBusQueueHealthCheckTests.cs | 84 ++++--------------- ...ueMessageCountThresholdHealthCheckTests.cs | 63 +++----------- ...eServiceBusSubscriptionHealthCheckTests.cs | 83 ++++-------------- .../AzureServiceBusTopicHealthCheckTests.cs | 65 ++++---------- 4 files changed, 61 insertions(+), 234 deletions(-) diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs index bb77ce6c2b..f0eda88a1e 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs @@ -45,11 +45,7 @@ public azureservicebusqueuehealthcheck_should() public async Task can_create_client_with_connection_string(bool peakMode) { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, peakMode, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -70,21 +66,11 @@ await healthCheck [InlineData(false)] public async Task reuses_existing_client_when_using_same_connection_string_with_different_queue(bool peakMode) { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, connectionString: ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, peakMode, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); - // Second call var otherQueueName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, peakMode, connectionString: ConnectionString); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherQueueName, peakMode, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -124,11 +110,7 @@ await _serviceBusAdministrationClient public async Task can_create_client_with_fully_qualified_endpoint(bool peakMode) { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, peakMode, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -149,21 +131,11 @@ await healthCheck [InlineData(false)] public async Task reuses_existing_client_when_checking_different_queue_in_same_servicebus(bool peakMode) { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, peakMode, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); - // Second call var otherQueueName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, peakMode, fullyQualifiedName: FullyQualifiedName); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherQueueName, peakMode, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -201,11 +173,7 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_checking_healthy_service_through_peek_and_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, true, connectionString: ConnectionString); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, true, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -223,11 +191,7 @@ await _serviceBusReceiver public async Task return_healthy_when_checking_healthy_service_through_peek_and_endpoint() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, true, fullyQualifiedName: FullyQualifiedName); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, true, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -245,16 +209,12 @@ await _serviceBusReceiver public async Task return_unhealthy_when_exception_is_thrown_by_client() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, true, connectionString: ConnectionString); - _serviceBusReceiver .PeekMessageAsync(cancellationToken: tokenSource.Token) .ThrowsAsyncForAnyArgs(new InvalidOperationException()); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, true, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Unhealthy); @@ -272,11 +232,7 @@ await _serviceBusReceiver public async Task return_healthy_when_checking_healthy_service_through_administration_and_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, false, connectionString: ConnectionString); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, false, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -290,11 +246,7 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_checking_healthy_service_through_administration_and_endpoint() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, false, fullyQualifiedName: FullyQualifiedName); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, false, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -308,15 +260,11 @@ await _serviceBusAdministrationClient public async Task return_unhealthy_when_exception_is_thrown_by_administration_client() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, false, connectionString: ConnectionString); - _serviceBusAdministrationClient .GetQueueRuntimePropertiesAsync(QueueName, cancellationToken: tokenSource.Token) .ThrowsAsyncForAnyArgs(new InvalidOperationException()); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, false, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Unhealthy); @@ -326,11 +274,12 @@ await _serviceBusAdministrationClient .ConfigureAwait(false); } - private (AzureServiceBusQueueHealthCheck, HealthCheckContext) CreateQueueHealthCheck( + private Task ExecuteHealthCheckAsync( string queueName, bool peakMode, string? connectionString = null, - string? fullyQualifiedName = null) + string? fullyQualifiedName = null, + CancellationToken cancellationToken = default) { var options = new AzureServiceBusQueueHealthCheckOptions(queueName) { @@ -344,6 +293,7 @@ await _serviceBusAdministrationClient { Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null) }; - return (healthCheck, context); + + return healthCheck.CheckHealthAsync(context, cancellationToken); } } diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs index 5d57c5e78a..8580a3253e 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs @@ -36,11 +36,7 @@ public azureservicebusqueuemessagecountthresholdhealthcheck_should() public async Task can_create_client_with_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -50,21 +46,11 @@ await healthCheck [Fact] public async Task reuses_existing_client_when_using_same_connection_string_with_different_queue() { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); - // Second call var otherQueueName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, connectionString: ConnectionString); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherQueueName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -85,11 +71,7 @@ await _serviceBusAdministrationClient public async Task can_create_client_with_fully_qualified_endpoint() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(QueueName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -99,21 +81,11 @@ await healthCheck [Fact] public async Task reuses_existing_client_when_using_same_fully_qualified_name_with_different_queue() { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, fullyQualifiedName: FullyQualifiedName); + await ExecuteHealthCheckAsync(QueueName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); - - // Second call var otherQueueName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, fullyQualifiedName: FullyQualifiedName); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherQueueName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -134,7 +106,6 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_active_queue_threshold_is_null() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString); var queueProperties = ServiceBusModelFactory.QueueRuntimeProperties(QueueName); var response = Response.FromValue(queueProperties, Substitute.For()); @@ -142,9 +113,7 @@ public async Task return_healthy_when_active_queue_threshold_is_null() .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) .Returns(response); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -173,7 +142,6 @@ public async Task return_expected_health_status_based_on_active_message_threshol DegradedThreshold = degradedThreshold, UnhealthyThreshold = unhealthyThreshold, }; - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString, activeMessagesCountThreshold: messageCountThreshold); var queueProperties = ServiceBusModelFactory.QueueRuntimeProperties(QueueName, activeMessageCount: messageCount); var response = Response.FromValue(queueProperties, Substitute.For()); @@ -181,9 +149,7 @@ public async Task return_expected_health_status_based_on_active_message_threshol .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) .Returns(response); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, connectionString: ConnectionString, activeMessagesCountThreshold: messageCountThreshold, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(expectedHealthStatus); @@ -212,7 +178,6 @@ public async Task return_expected_health_status_based_on_dead_letter_message_thr DegradedThreshold = degradedThreshold, UnhealthyThreshold = unhealthyThreshold, }; - var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString, deadLetterMessagesCountThreshold: messageCountThreshold); var queueProperties = ServiceBusModelFactory.QueueRuntimeProperties(QueueName, deadLetterMessageCount: messageCount); var response = Response.FromValue(queueProperties, Substitute.For()); @@ -220,9 +185,7 @@ public async Task return_expected_health_status_based_on_dead_letter_message_thr .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) .Returns(response); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(QueueName, connectionString: ConnectionString, deadLetterMessagesCountThreshold: messageCountThreshold, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(expectedHealthStatus); @@ -232,12 +195,13 @@ await _serviceBusAdministrationClient .ConfigureAwait(false); } - private (AzureServiceBusQueueMessageCountThresholdHealthCheck, HealthCheckContext) CreateQueueHealthCheck( + private Task ExecuteHealthCheckAsync( string queueName, string? connectionString = null, string? fullyQualifiedName = null, AzureServiceBusQueueMessagesCountThreshold? activeMessagesCountThreshold = null, - AzureServiceBusQueueMessagesCountThreshold? deadLetterMessagesCountThreshold = null) + AzureServiceBusQueueMessagesCountThreshold? deadLetterMessagesCountThreshold = null, + CancellationToken cancellationToken = default) { var options = new AzureServiceBusQueueMessagesCountThresholdHealthCheckOptions(queueName) { @@ -247,13 +211,12 @@ await _serviceBusAdministrationClient ActiveMessages = activeMessagesCountThreshold, DeadLetterMessages = deadLetterMessagesCountThreshold, }; - var healthCheck = new AzureServiceBusQueueMessageCountThresholdHealthCheck(options, _clientProvider); var context = new HealthCheckContext { Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null) }; - return (healthCheck, context); + return healthCheck.CheckHealthAsync(context, cancellationToken); } } diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs index c2a66a72c6..40e5ac87e4 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs @@ -1,4 +1,3 @@ -using System.Threading; using Azure.Core; using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; @@ -48,11 +47,7 @@ public azureservicebussubscriptionhealthcheck_should() public async Task can_create_client_with_connection_string(bool peakMode) { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, peakMode, connectionString: ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, peakMode, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -73,21 +68,11 @@ await healthCheck [InlineData(false)] public async Task reuses_existing_client_when_using_same_connection_string_with_different_topic(bool peakMode) { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, peakMode, connectionString: ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, peakMode, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); - // Second call var otherTopicName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateSubscriptionHealthCheck(otherTopicName, peakMode, connectionString: ConnectionString); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherTopicName, peakMode, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -127,11 +112,7 @@ await _serviceBusAdministrationClient public async Task can_create_client_with_fully_qualified_endpoint(bool peakMode) { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, peakMode, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, peakMode, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -152,21 +133,11 @@ await healthCheck [InlineData(false)] public async Task reuses_existing_client_when_checking_different_topic_in_same_servicebus(bool peakMode) { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, peakMode, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, peakMode, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); - // Second call var otherTopicName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateSubscriptionHealthCheck(otherTopicName, peakMode, fullyQualifiedName: FullyQualifiedName); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherTopicName, peakMode, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); if (peakMode) { @@ -204,11 +175,7 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_checking_healthy_service_through_peek_and_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, true, connectionString: ConnectionString); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, true, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); _serviceBusClient .Received(1) @@ -226,11 +193,7 @@ await _serviceBusReceiver public async Task return_healthy_when_checking_healthy_service_through_peek_and_endpoint() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, true, fullyQualifiedName: FullyQualifiedName); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, true, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); _serviceBusClient .Received(1) @@ -248,15 +211,12 @@ await _serviceBusReceiver public async Task return_unhealthy_when_exception_is_thrown_by_client() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, true, connectionString: ConnectionString); _serviceBusReceiver .PeekMessageAsync(cancellationToken: tokenSource.Token) .ThrowsAsyncForAnyArgs(new InvalidOperationException()); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, true, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); _serviceBusClient .Received(1) @@ -274,11 +234,7 @@ await _serviceBusReceiver public async Task return_healthy_when_checking_healthy_service_through_administration_and_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, false, connectionString: ConnectionString); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, false, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); await _serviceBusAdministrationClient .Received(1) @@ -292,11 +248,7 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_checking_healthy_service_through_administration_and_endpoint() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, false, fullyQualifiedName: FullyQualifiedName); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, false, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); await _serviceBusAdministrationClient .Received(1) @@ -310,15 +262,12 @@ await _serviceBusAdministrationClient public async Task return_unhealthy_when_exception_is_thrown_by_administration_client() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, false, connectionString: ConnectionString); _serviceBusAdministrationClient .GetSubscriptionRuntimePropertiesAsync(TopicName, SubscriptionName, cancellationToken: tokenSource.Token) .ThrowsAsyncForAnyArgs(new InvalidOperationException()); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, false, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); await _serviceBusAdministrationClient .Received(1) @@ -328,11 +277,12 @@ await _serviceBusAdministrationClient actual.Status.ShouldBe(HealthStatus.Unhealthy); } - private (AzureServiceBusSubscriptionHealthCheck, HealthCheckContext) CreateSubscriptionHealthCheck( + private Task ExecuteHealthCheckAsync( string topicName, bool peakMode, string? connectionString = null, - string? fullyQualifiedName = null) + string? fullyQualifiedName = null, + CancellationToken cancellationToken = default) { var options = new AzureServiceBusSubscriptionHealthCheckHealthCheckOptions(topicName, SubscriptionName) { @@ -346,6 +296,7 @@ await _serviceBusAdministrationClient { Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null) }; - return (healthCheck, context); + + return healthCheck.CheckHealthAsync(context, cancellationToken); } } diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs index 56b19b8609..d4ae353044 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs @@ -35,11 +35,7 @@ public azureservicebustopichealthcheck_should() public async Task can_create_client_with_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, connectionString: ConnectionString); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -49,21 +45,11 @@ await healthCheck [Fact] public async Task reuses_existing_client_when_using_same_connection_string_with_different_topic() { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, connectionString: ConnectionString); + await ExecuteHealthCheckAsync(TopicName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); - - // Second call var otherTopicName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateTopicHealthCheck(otherTopicName, connectionString: ConnectionString); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherTopicName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -84,11 +70,7 @@ await _serviceBusAdministrationClient public async Task can_create_client_with_fully_qualified_name() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -98,21 +80,11 @@ await healthCheck [Fact] public async Task reuses_existing_client_when_using_same_fully_qualified_name_with_different_topic() { - // First call using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, fullyQualifiedName: FullyQualifiedName); - - await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(TopicName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); - // Second call var otherTopicName = Guid.NewGuid().ToString(); - var (otherHealthCheck, otherContext) = CreateTopicHealthCheck(otherTopicName, fullyQualifiedName: FullyQualifiedName); - - await otherHealthCheck - .CheckHealthAsync(otherContext, tokenSource.Token) - .ConfigureAwait(false); + await ExecuteHealthCheckAsync(otherTopicName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); _clientProvider .Received(1) @@ -133,11 +105,7 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_only_checking_healthy_service_through_administration_and_connection_string() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, connectionString: ConnectionString); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -155,11 +123,7 @@ await _serviceBusAdministrationClient public async Task return_healthy_when_only_checking_healthy_service_through_administration_and_fully_qualified_name() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, fullyQualifiedName: FullyQualifiedName); - - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, fullyQualifiedName: FullyQualifiedName, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Healthy); @@ -177,15 +141,12 @@ await _serviceBusAdministrationClient public async Task return_unhealthy_when_exception_is_thrown_by_administration_client() { using var tokenSource = new CancellationTokenSource(); - var (healthCheck, context) = CreateTopicHealthCheck(TopicName, connectionString: ConnectionString); _serviceBusAdministrationClient .GetTopicRuntimePropertiesAsync(TopicName, cancellationToken: tokenSource.Token) .ThrowsAsyncForAnyArgs(new InvalidOperationException()); - var actual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) - .ConfigureAwait(false); + var actual = await ExecuteHealthCheckAsync(TopicName, connectionString: ConnectionString, cancellationToken: tokenSource.Token).ConfigureAwait(false); actual.Status.ShouldBe(HealthStatus.Unhealthy); @@ -195,10 +156,11 @@ await _serviceBusAdministrationClient .ConfigureAwait(false); } - private (AzureServiceBusTopicHealthCheck, HealthCheckContext) CreateTopicHealthCheck( + private Task ExecuteHealthCheckAsync( string topicName, string? connectionString = null, - string? fullyQualifiedName = null) + string? fullyQualifiedName = null, + CancellationToken cancellationToken = default) { var options = new AzureServiceBusTopicHealthCheckOptions(topicName) { @@ -211,6 +173,7 @@ await _serviceBusAdministrationClient { Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null) }; - return (healthCheck, context); + + return healthCheck.CheckHealthAsync(context, cancellationToken); } }