From a1c0565358b8b8c871b3dc04d24487ac480468b5 Mon Sep 17 00:00:00 2001 From: Thomas Malowanczyk Date: Wed, 9 Aug 2023 15:59:07 +0200 Subject: [PATCH] refactor: extend ServiceBus test with additional assert Some test was setup incorrectly but were not caught by in any assertion. Added som additional assert to ensure that healthchecks were executing as intended --- .../AzureServiceBusQueueHealthCheckTests.cs | 52 ++++++++++++++--- ...ueMessageCountThresholdHealthCheckTests.cs | 36 +++++++++--- ...eServiceBusSubscriptionHealthCheckTests.cs | 57 +++++++++++++++---- .../AzureServiceBusTopicHealthCheckTests.cs | 36 +++++++++--- 4 files changed, 147 insertions(+), 34 deletions(-) diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs index d510834eff..bb77ce6c2b 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueHealthCheckTests.cs @@ -47,7 +47,7 @@ public async Task can_create_client_with_connection_string(bool peakMode) using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -74,7 +74,7 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, connectionString: ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -82,8 +82,8 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ var otherQueueName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, peakMode, connectionString: ConnectionString); - var otherActual = await otherHealthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); if (peakMode) @@ -91,12 +91,30 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ _clientProvider .Received(1) .CreateClient(ConnectionString); + + _serviceBusClient + .Received(1) + .CreateReceiver(QueueName); + + _serviceBusClient + .Received(1) + .CreateReceiver(otherQueueName); } else { _clientProvider .Received(1) .CreateManagementClient(ConnectionString); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(otherQueueName, tokenSource.Token) + .ConfigureAwait(false); } } @@ -108,7 +126,7 @@ 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); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -135,7 +153,7 @@ public async Task reuses_existing_client_when_checking_different_queue_in_same_s using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, peakMode, fullyQualifiedName: FullyQualifiedName); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -143,8 +161,8 @@ public async Task reuses_existing_client_when_checking_different_queue_in_same_s var otherQueueName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, peakMode, fullyQualifiedName: FullyQualifiedName); - var otherActual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); if (peakMode) @@ -152,12 +170,30 @@ public async Task reuses_existing_client_when_checking_different_queue_in_same_s _clientProvider .Received(1) .CreateClient(FullyQualifiedName, _tokenCredential); + + _serviceBusClient + .Received(1) + .CreateReceiver(QueueName); + + _serviceBusClient + .Received(1) + .CreateReceiver(otherQueueName); } else { _clientProvider .Received(1) .CreateManagementClient(FullyQualifiedName, _tokenCredential); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(otherQueueName, tokenSource.Token) + .ConfigureAwait(false); } } diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs index 032624b72c..5d57c5e78a 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusQueueMessageCountThresholdHealthCheckTests.cs @@ -38,7 +38,7 @@ public async Task can_create_client_with_connection_string() using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -54,7 +54,7 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, connectionString: ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -62,13 +62,23 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ var otherQueueName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, connectionString: ConnectionString); - var otherActual = await healthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); _clientProvider .Received(1) .CreateManagementClient(ConnectionString); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(otherQueueName, tokenSource.Token) + .ConfigureAwait(false); } [Fact] @@ -77,7 +87,7 @@ public async Task can_create_client_with_fully_qualified_endpoint() using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, fullyQualifiedName: FullyQualifiedName); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -93,7 +103,7 @@ public async Task reuses_existing_client_when_using_same_fully_qualified_name_wi using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateQueueHealthCheck(QueueName, fullyQualifiedName: FullyQualifiedName); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -101,13 +111,23 @@ public async Task reuses_existing_client_when_using_same_fully_qualified_name_wi var otherQueueName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateQueueHealthCheck(otherQueueName, fullyQualifiedName: FullyQualifiedName); - var otherActual = await otherHealthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); _clientProvider .Received(1) .CreateManagementClient(FullyQualifiedName, _tokenCredential); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(QueueName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetQueueRuntimePropertiesAsync(otherQueueName, tokenSource.Token) + .ConfigureAwait(false); } [Fact] diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs index c83aa48f3d..c2a66a72c6 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusSubscriptionHealthCheckTests.cs @@ -1,3 +1,4 @@ +using System.Threading; using Azure.Core; using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; @@ -49,7 +50,7 @@ public async Task can_create_client_with_connection_string(bool peakMode) using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateSubscriptionHealthCheck(TopicName, peakMode, connectionString: ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -70,13 +71,13 @@ public async Task can_create_client_with_connection_string(bool peakMode) [Theory] [InlineData(true)] [InlineData(false)] - public async Task reuses_existing_client_when_using_same_connection_string_with_different_queue(bool peakMode) + 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); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -84,8 +85,8 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ var otherTopicName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateSubscriptionHealthCheck(otherTopicName, peakMode, connectionString: ConnectionString); - var otherActual = await otherHealthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); if (peakMode) @@ -93,12 +94,30 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ _clientProvider .Received(1) .CreateClient(ConnectionString); + + _serviceBusClient + .Received(1) + .CreateReceiver(TopicName, SubscriptionName); + + _serviceBusClient + .Received(1) + .CreateReceiver(otherTopicName, SubscriptionName); } else { _clientProvider .Received(1) .CreateManagementClient(ConnectionString); + + await _serviceBusAdministrationClient + .Received(1) + .GetSubscriptionRuntimePropertiesAsync(TopicName, SubscriptionName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetSubscriptionRuntimePropertiesAsync(otherTopicName, SubscriptionName, tokenSource.Token) + .ConfigureAwait(false); } } @@ -110,7 +129,7 @@ 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); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -131,13 +150,13 @@ public async Task can_create_client_with_fully_qualified_endpoint(bool peakMode) [Theory] [InlineData(true)] [InlineData(false)] - public async Task reuses_existing_client_when_checking_different_queue_in_same_servicebus(bool peakMode) + 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); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -145,8 +164,8 @@ public async Task reuses_existing_client_when_checking_different_queue_in_same_s var otherTopicName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateSubscriptionHealthCheck(otherTopicName, peakMode, fullyQualifiedName: FullyQualifiedName); - var otherActual = await otherHealthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); if (peakMode) @@ -154,12 +173,30 @@ public async Task reuses_existing_client_when_checking_different_queue_in_same_s _clientProvider .Received(1) .CreateClient(FullyQualifiedName, _tokenCredential); + + _serviceBusClient + .Received(1) + .CreateReceiver(TopicName, SubscriptionName); + + _serviceBusClient + .Received(1) + .CreateReceiver(otherTopicName, SubscriptionName); } else { _clientProvider .Received(1) .CreateManagementClient(FullyQualifiedName, _tokenCredential); + + await _serviceBusAdministrationClient + .Received(1) + .GetSubscriptionRuntimePropertiesAsync(TopicName, SubscriptionName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetSubscriptionRuntimePropertiesAsync(otherTopicName, SubscriptionName, tokenSource.Token) + .ConfigureAwait(false); } } diff --git a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs index ea8b8e84ae..56b19b8609 100644 --- a/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs +++ b/test/HealthChecks.AzureServiceBus.Tests/AzureServiceBusTopicHealthCheckTests.cs @@ -37,7 +37,7 @@ public async Task can_create_client_with_connection_string() using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateTopicHealthCheck(TopicName, connectionString: ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -53,7 +53,7 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateTopicHealthCheck(TopicName, connectionString: ConnectionString); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -61,13 +61,23 @@ public async Task reuses_existing_client_when_using_same_connection_string_with_ var otherTopicName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateTopicHealthCheck(otherTopicName, connectionString: ConnectionString); - var otherActual = await otherHealthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); _clientProvider .Received(1) .CreateManagementClient(ConnectionString); + + await _serviceBusAdministrationClient + .Received(1) + .GetTopicRuntimePropertiesAsync(TopicName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetTopicRuntimePropertiesAsync(otherTopicName, tokenSource.Token) + .ConfigureAwait(false); } [Fact] @@ -76,7 +86,7 @@ public async Task can_create_client_with_fully_qualified_name() using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateTopicHealthCheck(TopicName, fullyQualifiedName: FullyQualifiedName); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -92,7 +102,7 @@ public async Task reuses_existing_client_when_using_same_fully_qualified_name_wi using var tokenSource = new CancellationTokenSource(); var (healthCheck, context) = CreateTopicHealthCheck(TopicName, fullyQualifiedName: FullyQualifiedName); - var actual = await healthCheck + await healthCheck .CheckHealthAsync(context, tokenSource.Token) .ConfigureAwait(false); @@ -100,13 +110,23 @@ public async Task reuses_existing_client_when_using_same_fully_qualified_name_wi var otherTopicName = Guid.NewGuid().ToString(); var (otherHealthCheck, otherContext) = CreateTopicHealthCheck(otherTopicName, fullyQualifiedName: FullyQualifiedName); - var otherActual = await otherHealthCheck - .CheckHealthAsync(context, tokenSource.Token) + await otherHealthCheck + .CheckHealthAsync(otherContext, tokenSource.Token) .ConfigureAwait(false); _clientProvider .Received(1) .CreateManagementClient(FullyQualifiedName, _tokenCredential); + + await _serviceBusAdministrationClient + .Received(1) + .GetTopicRuntimePropertiesAsync(TopicName, tokenSource.Token) + .ConfigureAwait(false); + + await _serviceBusAdministrationClient + .Received(1) + .GetTopicRuntimePropertiesAsync(otherTopicName, tokenSource.Token) + .ConfigureAwait(false); } [Fact]