Skip to content

Commit

Permalink
refactor: extend ServiceBus test with additional assert
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TomMalow committed Aug 9, 2023
1 parent 85afba7 commit a1c0565
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -74,29 +74,47 @@ 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);

// Second call
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)
{
_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);
}
}

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

Expand All @@ -135,29 +153,47 @@ 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);

// Second call
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)
{
_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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -54,21 +54,31 @@ 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);

// Second call
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]
Expand All @@ -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);

Expand All @@ -93,21 +103,31 @@ 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);

// Second call
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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading;
using Azure.Core;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
Expand Down Expand Up @@ -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);

Expand All @@ -70,35 +71,53 @@ 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);

// Second call
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)
{
_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);
}
}

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

Expand All @@ -131,35 +150,53 @@ 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);

// Second call
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)
{
_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);
}
}

Expand Down
Loading

0 comments on commit a1c0565

Please sign in to comment.