From f853676c5d38f223bc2e4402dd09087356211ba9 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Tue, 21 May 2024 16:28:53 +0200 Subject: [PATCH] Test: Linter Cleanup & Improvements (#162) --- Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs | 5 +---- Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs | 4 ++++ .../LastWillAndTestamentBuilderTest.cs | 2 ++ Tests/HiveMQtt.Test/HiveMQClient/OptionsTest.cs | 2 +- Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs | 16 +++++++++++++--- .../HiveMQClient/UnsubscribeTest.cs | 10 ++-------- Tests/HiveMQtt.Test/HiveMQClient/UtilTest.cs | 1 - 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs index aee5295d..827e215a 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs @@ -37,10 +37,7 @@ public async Task RaiseOnFailureToConnectAsync() var clientOptions = new HiveMQClientOptionsBuilder().WithPort(0).Build(); var client = new HiveMQClient(clientOptions); - await Assert.ThrowsAsync(async () => - { - await client.ConnectAsync().ConfigureAwait(false); - }).ConfigureAwait(false); + await Assert.ThrowsAsync(client.ConnectAsync).ConfigureAwait(false); } [Fact] diff --git a/Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs index 8ffb03c7..8d7b18b1 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs @@ -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); @@ -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"); diff --git a/Tests/HiveMQtt.Test/HiveMQClient/LastWillAndTestamentBuilderTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/LastWillAndTestamentBuilderTest.cs index 20455816..f21e18a7 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/LastWillAndTestamentBuilderTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/LastWillAndTestamentBuilderTest.cs @@ -41,7 +41,9 @@ public async Task Last_Will_With_Properties_Async() var messagesReceived = 0; var taskLWTReceived = new TaskCompletionSource(); +#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) => diff --git a/Tests/HiveMQtt.Test/HiveMQClient/OptionsTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/OptionsTest.cs index 062c98bf..bad98fc1 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/OptionsTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/OptionsTest.cs @@ -63,7 +63,7 @@ public void WithBadClientMaximumPacketSize() ClientMaximumPacketSize = -300, }; - var exception = Assert.Throws(() => options.Validate()); + var exception = Assert.Throws(options.Validate); Assert.Contains("Client Maximum Packet Size must be greater than 0.", exception.Message); options.ClientMaximumPacketSize = long.MaxValue; diff --git a/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs index 3605a60c..535e32d1 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs @@ -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++; @@ -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; @@ -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) { client3MessageCount++; Assert.NotNull(eventArgs.PublishMessage); Assert.Equal("Hello World", eventArgs.PublishMessage.PayloadAsString); } +#pragma warning restore VSTHRD100 // Avoid async void methods client3.OnMessageReceived += Client3MessageHandler; @@ -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++; @@ -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; @@ -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); @@ -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); diff --git a/Tests/HiveMQtt.Test/HiveMQClient/UnsubscribeTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/UnsubscribeTest.cs index 3358e972..12ecc31d 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/UnsubscribeTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/UnsubscribeTest.cs @@ -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(() => - { - return subClient.UnsubscribeAsync("tests/InvalidUnsubscribeStringAsync"); - }).ConfigureAwait(false); + await Assert.ThrowsAsync(() => subClient.UnsubscribeAsync("tests/InvalidUnsubscribeStringAsync")).ConfigureAwait(false); Assert.True(subClient.Subscriptions.Count == 0); @@ -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(() => - { - return subClient.UnsubscribeAsync(subscription); - }).ConfigureAwait(false); + await Assert.ThrowsAsync(() => subClient.UnsubscribeAsync(subscription)).ConfigureAwait(false); Assert.True(subClient.Subscriptions.Count == 0); diff --git a/Tests/HiveMQtt.Test/HiveMQClient/UtilTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/UtilTest.cs index 348dc23d..d00e0a11 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/UtilTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/UtilTest.cs @@ -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); - } }