Skip to content

Commit

Permalink
refactor: simplify HealthCheck calls in service bus test
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TomMalow committed Aug 9, 2023
1 parent a1c0565 commit f60355d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -326,11 +274,12 @@ await _serviceBusAdministrationClient
.ConfigureAwait(false);
}

private (AzureServiceBusQueueHealthCheck, HealthCheckContext) CreateQueueHealthCheck(
private Task<HealthCheckResult> ExecuteHealthCheckAsync(
string queueName,
bool peakMode,
string? connectionString = null,
string? fullyQualifiedName = null)
string? fullyQualifiedName = null,
CancellationToken cancellationToken = default)
{
var options = new AzureServiceBusQueueHealthCheckOptions(queueName)
{
Expand All @@ -344,6 +293,7 @@ await _serviceBusAdministrationClient
{
Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null)
};
return (healthCheck, context);

return healthCheck.CheckHealthAsync(context, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -134,17 +106,14 @@ 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<Response>());

_serviceBusAdministrationClient
.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);

Expand Down Expand Up @@ -173,17 +142,14 @@ 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<Response>());

_serviceBusAdministrationClient
.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);

Expand Down Expand Up @@ -212,17 +178,14 @@ 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<Response>());

_serviceBusAdministrationClient
.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);

Expand All @@ -232,12 +195,13 @@ await _serviceBusAdministrationClient
.ConfigureAwait(false);
}

private (AzureServiceBusQueueMessageCountThresholdHealthCheck, HealthCheckContext) CreateQueueHealthCheck(
private Task<HealthCheckResult> 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)
{
Expand All @@ -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);
}
}
Loading

0 comments on commit f60355d

Please sign in to comment.