Skip to content

Commit

Permalink
Test: Linter Cleanup & Improvements (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored May 21, 2024
1 parent 684385d commit f853676
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
5 changes: 1 addition & 4 deletions Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public async Task RaiseOnFailureToConnectAsync()
var clientOptions = new HiveMQClientOptionsBuilder().WithPort(0).Build();
var client = new HiveMQClient(clientOptions);

await Assert.ThrowsAsync<HiveMQttClientException>(async () =>
{
await client.ConnectAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
await Assert.ThrowsAsync<HiveMQttClientException>(client.ConnectAsync).ConfigureAwait(false);
}

[Fact]
Expand Down
4 changes: 4 additions & 0 deletions Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public async Task Last_Will_With_Properties_Async()
Assert.Equal("last will message", args.PublishMessage.PayloadAsString);
Assert.Equal("application/text", args.PublishMessage.ContentType);
Assert.Equal("response/topic", args.PublishMessage.ResponseTopic);
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
byte[] correlationData = [1, 2, 3, 4, 5];
#pragma warning restore SA1010 // Opening square brackets should be spaced correctly
Assert.Equal(correlationData, args.PublishMessage.CorrelationData);
Assert.Equal(MQTT5PayloadFormatIndicator.UTF8Encoded, args.PublishMessage.PayloadFormatIndicator);
Assert.Equal(100, args.PublishMessage.MessageExpiryInterval);
Expand Down Expand Up @@ -61,7 +63,9 @@ public async Task Last_Will_With_Properties_Async()
options.LastWillAndTestament.QoS = QualityOfService.AtLeastOnceDelivery;
options.LastWillAndTestament.ContentType = "application/text";
options.LastWillAndTestament.ResponseTopic = "response/topic";
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
byte[] correlationData = [1, 2, 3, 4, 5];
#pragma warning restore SA1010 // Opening square brackets should be spaced correctly
options.LastWillAndTestament.CorrelationData = correlationData;
options.LastWillAndTestament.UserProperties.Add("userPropertyKey", "userPropertyValue");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public async Task Last_Will_With_Properties_Async()

var messagesReceived = 0;
var taskLWTReceived = new TaskCompletionSource<bool>();
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
byte[] correlationDataBytes = [1, 2, 3, 4, 5];
#pragma warning restore SA1010 // Opening square brackets should be spaced correctly

// Set the event handler for the message received event
listenerClient.OnMessageReceived += (sender, args) =>
Expand Down
2 changes: 1 addition & 1 deletion Tests/HiveMQtt.Test/HiveMQClient/OptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void WithBadClientMaximumPacketSize()
ClientMaximumPacketSize = -300,
};

var exception = Assert.Throws<HiveMQttClientException>(() => options.Validate());
var exception = Assert.Throws<HiveMQttClientException>(options.Validate);
Assert.Contains("Client Maximum Packet Size must be greater than 0.", exception.Message);

options.ClientMaximumPacketSize = long.MaxValue;
Expand Down
16 changes: 13 additions & 3 deletions Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public async Task ThreeNodeQoS0ChainedPublishesAsync()
var client2MessageCount = 0;

// client 2 will receive the message and republish it to another topic
#pragma warning disable VSTHRD100 // Avoid async void methods
async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
{
client2MessageCount++;
Expand All @@ -210,6 +211,7 @@ async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs even
Assert.NotNull(publishResult);
}
}
#pragma warning restore VSTHRD100 // Avoid async void methods

client2.OnMessageReceived += Client2MessageHandler;

Expand All @@ -218,12 +220,14 @@ async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs even
var client3MessageCount = 0;

// client 3 will receive the final message
#pragma warning disable VSTHRD100 // Avoid async void methods
async void Client3MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 224 in Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
client3MessageCount++;
Assert.NotNull(eventArgs.PublishMessage);
Assert.Equal("Hello World", eventArgs.PublishMessage.PayloadAsString);
}
#pragma warning restore VSTHRD100 // Avoid async void methods

client3.OnMessageReceived += Client3MessageHandler;

Expand Down Expand Up @@ -288,6 +292,7 @@ public async Task ThreeNodeQoS1ChainedPublishesAsync()
var client2MessageCount = 0;

// client 2 will receive the message and republish it to another topic
#pragma warning disable VSTHRD100 // Avoid async void methods
async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
{
client2MessageCount++;
Expand All @@ -298,6 +303,7 @@ async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs even
Assert.Equal(publishResult.QoS1ReasonCode, PubAckReasonCode.Success);
}
}
#pragma warning restore VSTHRD100 // Avoid async void methods

client2.OnMessageReceived += Client2MessageHandler;

Expand All @@ -307,7 +313,7 @@ async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs even
var client3MessageCount = 0;

// client 3 will receive the final message
async void Client3MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
void Client3MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
{
client3MessageCount++;
Assert.NotNull(eventArgs.PublishMessage);
Expand Down Expand Up @@ -377,23 +383,27 @@ public async Task ThreeNodeQoS2ChainedPublishesAsync()
var client2MessageCount = 0;

// client 2 will receive the message and republish it to another topic
#pragma warning disable VSTHRD100 // Avoid async void methods
async void Client2MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
{
client2MessageCount++;
var client = sender as HiveMQClient;
#pragma warning disable CS8602 // Dereference of a possibly null reference.
var publishResult = await client.PublishAsync("HMQ/SecondTopic", eventArgs.PublishMessage.PayloadAsString, QualityOfService.ExactlyOnceDelivery).ConfigureAwait(true);
#pragma warning restore CS8602 // Dereference of a possibly null reference.
Assert.NotNull(publishResult);
Assert.Equal(publishResult.QoS2ReasonCode, PubRecReasonCode.Success);
}
#pragma warning restore VSTHRD100 // Avoid async void methods

client2.OnMessageReceived += Client2MessageHandler;

// client 3 Subscribe to the secondary topic
subscribeResult = await client3.SubscribeAsync("HMQ/SecondTopic", QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);

var client3MessageCount = 0;
// client 3 will receive the final message
async void Client3MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
var client3MessageCount = 0;
void Client3MessageHandler(object? sender, OnMessageReceivedEventArgs eventArgs)
{
client3MessageCount++;
Assert.NotNull(eventArgs.PublishMessage);
Expand Down
10 changes: 2 additions & 8 deletions Tests/HiveMQtt.Test/HiveMQClient/UnsubscribeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public async Task InvalidUnsubscribeStringAsync()
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

// Unsubscribe from a non-existing subscription should throw an exception
await Assert.ThrowsAsync<HiveMQttClientException>(() =>
{
return subClient.UnsubscribeAsync("tests/InvalidUnsubscribeStringAsync");
}).ConfigureAwait(false);
await Assert.ThrowsAsync<HiveMQttClientException>(() => subClient.UnsubscribeAsync("tests/InvalidUnsubscribeStringAsync")).ConfigureAwait(false);

Assert.True(subClient.Subscriptions.Count == 0);

Expand All @@ -63,10 +60,7 @@ public async Task InvalidUnsubscribeSubscriptionAsync()
var subscription = new Subscription(topicFilter);

// Unsubscribe from a non-existing subscription should throw an exception
await Assert.ThrowsAsync<HiveMQttClientException>(() =>
{
return subClient.UnsubscribeAsync(subscription);
}).ConfigureAwait(false);
await Assert.ThrowsAsync<HiveMQttClientException>(() => subClient.UnsubscribeAsync(subscription)).ConfigureAwait(false);

Assert.True(subClient.Subscriptions.Count == 0);

Expand Down
1 change: 0 additions & 1 deletion Tests/HiveMQtt.Test/HiveMQClient/UtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,5 @@ public void MultiLevelWildcardMatch()
// A subscription to “$SYS/#” will receive messages published to topics beginning with “$SYS/”
result = HiveMQClient.MatchTopic("$SYS/#", "$SYS/broker/clients/total");
Assert.True(result);

}
}

0 comments on commit f853676

Please sign in to comment.