Skip to content

Commit

Permalink
Tests: More Reliable LWT Tests (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored Feb 2, 2024
1 parent 5d11a15 commit 11c6013
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
31 changes: 11 additions & 20 deletions Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ namespace HiveMQtt.Test.HiveMQClient;

public class LWTTest
{
[Fact]
public async Task Basic_Last_Will_Async()
{
var options = new HiveMQClientOptions
{
LastWillAndTestament = new LastWillAndTestament("last/will", "last will message"),
};

var client = new HiveMQClient(options);

var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
Assert.True(client.IsConnected());
}

[Fact]
public async Task Last_Will_With_Properties_Async()
{
Expand All @@ -41,7 +26,7 @@ public async Task Last_Will_With_Properties_Async()
{
messagesReceived++;
Assert.Equal(QualityOfService.AtLeastOnceDelivery, args.PublishMessage.QoS);
Assert.Equal("last/will", args.PublishMessage.Topic);
Assert.Equal("last/will2", args.PublishMessage.Topic);
Assert.Equal("last will message", args.PublishMessage.PayloadAsString);
Assert.Equal("application/text", args.PublishMessage.ContentType);
Assert.Equal("response/topic", args.PublishMessage.ResponseTopic);
Expand All @@ -58,20 +43,21 @@ public async Task Last_Will_With_Properties_Async()
taskLWTReceived.SetResult(true);
};

var result = await listenerClient.SubscribeAsync("last/will", QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
var result = await listenerClient.SubscribeAsync("last/will2", QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
Assert.Single(result.Subscriptions);
Assert.Equal(SubAckReasonCode.GrantedQoS1, result.Subscriptions[0].SubscribeReasonCode);
Assert.Equal("last/will", result.Subscriptions[0].TopicFilter.Topic);
Assert.Equal("last/will2", result.Subscriptions[0].TopicFilter.Topic);

// Setup & Connect another client with a LWT
var options = new HiveMQClientOptions
{
LastWillAndTestament = new LastWillAndTestament("last/will", "last will message"),
LastWillAndTestament = new LastWillAndTestament("last/will2", "last will message"),
};

options.LastWillAndTestament.WillDelayInterval = 1;
options.LastWillAndTestament.WillDelayInterval = 5;
options.LastWillAndTestament.PayloadFormatIndicator = 1;
options.LastWillAndTestament.MessageExpiryInterval = 100;
options.LastWillAndTestament.QoS = QualityOfService.AtLeastOnceDelivery;
options.LastWillAndTestament.ContentType = "application/text";
options.LastWillAndTestament.ResponseTopic = "response/topic";
options.LastWillAndTestament.CorrelationData = new byte[] { 1, 2, 3, 4, 5 };
Expand All @@ -82,12 +68,17 @@ public async Task Last_Will_With_Properties_Async()
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
Assert.True(client.IsConnected());

await Task.Delay(5000).ConfigureAwait(false);

// Call DisconnectWithWillMessage. listenerClient should receive the LWT message
var disconnectOptions = new DisconnectOptions { ReasonCode = DisconnectReasonCode.DisconnectWithWillMessage };
var disconnectResult = await client.DisconnectAsync(disconnectOptions).ConfigureAwait(false);

// Wait until the LWT message is received
var taskResult = await taskLWTReceived.Task.WaitAsync(TimeSpan.FromSeconds(25)).ConfigureAwait(false);
Assert.True(taskResult);

Assert.Equal(1, messagesReceived);
await listenerClient.DisconnectAsync().ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task Last_Will_With_Properties_Async()
{
messagesReceived++;
Assert.Equal(QualityOfService.AtLeastOnceDelivery, args.PublishMessage.QoS);
Assert.Equal("last/will", args.PublishMessage.Topic);
Assert.Equal("last/will7", args.PublishMessage.Topic);
Assert.Equal("last will message", args.PublishMessage.PayloadAsString);
Assert.Equal("application/text", args.PublishMessage.ContentType);
Assert.Equal("response/topic", args.PublishMessage.ResponseTopic);
Expand All @@ -64,13 +64,13 @@ public async Task Last_Will_With_Properties_Async()
taskLWTReceived.SetResult(true);
};

var result = await listenerClient.SubscribeAsync("last/will", QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
var result = await listenerClient.SubscribeAsync("last/will7", QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
Assert.Single(result.Subscriptions);
Assert.Equal(SubAckReasonCode.GrantedQoS1, result.Subscriptions[0].SubscribeReasonCode);
Assert.Equal("last/will", result.Subscriptions[0].TopicFilter.Topic);
Assert.Equal("last/will7", result.Subscriptions[0].TopicFilter.Topic);

var lwt = new LastWillAndTestamentBuilder()
.WithTopic("last/will")
.WithTopic("last/will7")
.WithPayload("last will message")
.WithQualityOfServiceLevel(QualityOfService.AtLeastOnceDelivery)
.WithContentType("application/text")
Expand All @@ -79,7 +79,7 @@ public async Task Last_Will_With_Properties_Async()
.WithPayloadFormatIndicator(MQTT5PayloadFormatIndicator.UTF8Encoded)
.WithMessageExpiryInterval(100)
.WithUserProperty("userPropertyKey", "userPropertyValue")
.WithWillDelayInterval(1)
.WithWillDelayInterval(5)
.Build();

// Setup & Connect the client with LWT
Expand All @@ -93,6 +93,8 @@ public async Task Last_Will_With_Properties_Async()
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
Assert.True(client.IsConnected());

await Task.Delay(5000).ConfigureAwait(false);

// Call DisconnectWithWillMessage. listenerClient should receive the LWT message
var disconnectOptions = new DisconnectOptions { ReasonCode = DisconnectReasonCode.DisconnectWithWillMessage };
var disconnectResult = await client.DisconnectAsync(disconnectOptions).ConfigureAwait(false);
Expand Down

0 comments on commit 11c6013

Please sign in to comment.