Skip to content

Commit

Permalink
Tests: Used named clients (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored Jun 26, 2024
1 parent 386afbf commit 5894e4f
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 61 deletions.
1 change: 0 additions & 1 deletion Tests/HiveMQtt.Test/HiveMQClient/ClientOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class ClientOptionsTest
[Fact]
public void Client_ID()
{
// var clientOptions = new HiveMQClientOptions();
var client = new HiveMQClient();

Assert.NotNull(client.Options.ClientId);
Expand Down
19 changes: 15 additions & 4 deletions Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class ConnectTest
[Fact]
public async Task BasicConnectAndDisconnectAsync()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("BasicConnectAndDisconnectAsync").Build();
var client = new HiveMQClient(options);
Assert.NotNull(client);

var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Expand All @@ -28,22 +29,27 @@ public async Task BasicConnectAndDisconnectAsync()
var disconnectResult = await client.DisconnectAsync(disconnectOptions).ConfigureAwait(false);
Assert.True(disconnectResult);
Assert.False(client.IsConnected());

client.Dispose();
}

[Fact]
public async Task RaiseOnFailureToConnectAsync()
{
// Bad port number
var clientOptions = new HiveMQClientOptionsBuilder().WithPort(0).Build();
var clientOptions = new HiveMQClientOptionsBuilder().WithPort(0).WithClientId("RaiseOnFailureToConnectAsync").Build();
var client = new HiveMQClient(clientOptions);

await Assert.ThrowsAsync<HiveMQttClientException>(client.ConnectAsync).ConfigureAwait(false);

client.Dispose();
}

[Fact]
public async Task TestConnectEventsAsync()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("TestConnectEventsAsync").Build();
var client = new HiveMQClient(options);

// Client Events
client.BeforeConnect += BeforeConnectHandler;
Expand Down Expand Up @@ -85,12 +91,15 @@ public async Task TestConnectEventsAsync()

client.OnConnectSent -= OnConnectSentHandler;
client.OnConnAckReceived -= OnConnAckReceivedHandler;

client.Dispose();
}

[Fact]
public async Task Test_AfterDisconnectEvent_Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("Test_AfterDisconnectEvent_Async").Build();
var client = new HiveMQClient(options);

// Client Events
client.AfterDisconnect += AfterDisconnectHandler;
Expand All @@ -114,6 +123,8 @@ public async Task Test_AfterDisconnectEvent_Async()

// Remove event handlers
client.AfterDisconnect -= AfterDisconnectHandler;

client.Dispose();
}

private static void BeforeConnectHandler(object? sender, BeforeConnectEventArgs eventArgs)
Expand Down
4 changes: 3 additions & 1 deletion Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class LWTTest
public async Task Last_Will_With_Properties_Async()
{
// Setup & Connect a client to listen for LWT
var listenerClient = new HiveMQClient();
var listenerOptions = new HiveMQClientOptionsBuilder().WithClientId("Last_Will_With_Properties_Async-Listener").Build();
var listenerClient = new HiveMQClient(listenerOptions);
var connectResult = await listenerClient.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
Assert.True(listenerClient.IsConnected());
Expand Down Expand Up @@ -54,6 +55,7 @@ public async Task Last_Will_With_Properties_Async()
// Setup & Connect another client with a LWT
var options = new HiveMQClientOptions
{
ClientId = "Last_Will_With_Properties_Async-LWTClient",
LastWillAndTestament = new LastWillAndTestament("last/will2", "last will message"),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public async Task Basic_Last_Will_Async()
public async Task Last_Will_With_Properties_Async()
{
// Setup & Connect a client to listen for LWT
var listenerClient = new HiveMQClient();
var listenerOptions = new HiveMQClientOptionsBuilder().WithClientId("Last_Will_With_Properties_Async-Listener").Build();
var listenerClient = new HiveMQClient(listenerOptions);
var connectResult = await listenerClient.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
Assert.True(listenerClient.IsConnected());
Expand Down Expand Up @@ -86,6 +87,7 @@ public async Task Last_Will_With_Properties_Async()
// Setup & Connect the client with LWT
var options = new HiveMQClientOptions
{
ClientId = "Last_Will_With_Properties_Async-LWTClient",
LastWillAndTestament = lwt,
};

Expand All @@ -103,5 +105,7 @@ public async Task Last_Will_With_Properties_Async()
// Wait until the LWT message is received
var taskResult = await taskLWTReceived.Task.WaitAsync(TimeSpan.FromSeconds(25)).ConfigureAwait(false);
Assert.True(taskResult);

await listenerClient.DisconnectAsync().ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private async Task<int> PublisherClientAsync()
await publishClient.PublishAsync(firstTopic, payload, QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);
}

await publishClient.DisconnectAsync().ConfigureAwait(false);
return batchSize;
}

Expand Down Expand Up @@ -88,6 +89,7 @@ private async Task<int> RelayClientAsync()

// Wait until all messages are relayed
await Task.Delay(5000).ConfigureAwait(false);
await subscribeClient.DisconnectAsync().ConfigureAwait(false);
return relayCount;
}

Expand All @@ -113,6 +115,8 @@ private async Task<int> ReceiverClientAsync()

// Wait for the receiver to receive all messages
await Task.Delay(5000).ConfigureAwait(false);
await receiverClient.DisconnectAsync().ConfigureAwait(false);

return receivedCount;
}
}
25 changes: 20 additions & 5 deletions Tests/HiveMQtt.Test/HiveMQClient/PubSubTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public async Task MostBasicPubSubAsync()
{
var testTopic = "tests/MostBasicPubSubAsync";
var testPayload = new string(/*lang=json,strict*/ "{\"interference\": \"1029384\"}");
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPubSubAsync").Build();
var client = new HiveMQClient(options);
var taskCompletionSource = new TaskCompletionSource<bool>();

var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -47,14 +48,17 @@ public async Task MostBasicPubSubAsync()

var taskResult = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
Assert.True(taskResult);

client.Dispose();
}

[Fact]
public async Task QoS1PubSubAsync()
{
var testTopic = "tests/QoS1PubSubAsync";
var testPayload = new string(/*lang=json,strict*/ "{\"interference\": \"1029384\"}");
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPubSubAsync").Build();
var client = new HiveMQClient(options);
var taskCompletionSource = new TaskCompletionSource<bool>();
var messagesReceived = 0;

Expand Down Expand Up @@ -93,6 +97,8 @@ public async Task QoS1PubSubAsync()

var taskResult = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
Assert.True(taskResult);

client.Dispose();
}

[Fact]
Expand All @@ -101,7 +107,8 @@ public async Task QoS2PubSubAsync()
var testTopic = "tests/QoS2PubSubAsync";
var testPayload = new string(/*lang=json,strict*/ "{\"interference\": \"1029384\"}");

var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("QoS2PubSubAsync").Build();
var client = new HiveMQClient(options);
var taskCompletionSource = new TaskCompletionSource<bool>();
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
Expand Down Expand Up @@ -141,14 +148,17 @@ public async Task QoS2PubSubAsync()

var taskResult = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
Assert.True(taskResult);

client.Dispose();
}

[Fact]
public async Task LargeMessagePubSubAsync()
{
var testTopic = "tests/LargeMessagePubSubAsync";
var testPayload = "1. A delusion starts like any other idea, as an egg. Identical on the outside, perfectly formed. From the shell, you'd never know anything was wrong. It's what's inside that matters. 2. A delusion starts like any other idea, as an egg. Identical on the outside, perfectly formed. From the shell, you'd never know anything was wrong. It's what's inside that matters. 3. A delusion starts like any other idea, as an egg. Identical on the outside, perfectly formed. From the shell, you'd never know anything was wrong. It's what's inside that matters.";
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("LargeMessagePubSubAsync").Build();
var client = new HiveMQClient(options);
var taskCompletionSource = new TaskCompletionSource<bool>();

var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -184,6 +194,8 @@ public async Task LargeMessagePubSubAsync()

var taskResult = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
Assert.True(taskResult);

client.Dispose();
}

[Fact]
Expand All @@ -198,7 +210,8 @@ public async Task OneMBMessagePubSubAsync()
testPayload[i] = 0x05;
}

var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("OneMBMessagePubSubAsync").Build();
var client = new HiveMQClient(options);
var taskCompletionSource = new TaskCompletionSource<bool>();

var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -240,5 +253,7 @@ public async Task OneMBMessagePubSubAsync()

var taskResult = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(15)).ConfigureAwait(false);
Assert.True(taskResult);

client.Dispose();
}
}
40 changes: 32 additions & 8 deletions Tests/HiveMQtt.Test/HiveMQClient/PublishBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class PublishBuilderTest
[Fact]
public async Task MostBasicPublishWithQoS0Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPublishWithQoS0Async").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -32,12 +33,15 @@ public async Task MostBasicPublishWithQoS0Async()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task MostBasicPublishWithQoS1Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPublishWithQoS1Async").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -54,12 +58,15 @@ public async Task MostBasicPublishWithQoS1Async()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task MostBasicPublishWithQoS2Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPublishWithQoS2Async").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -76,12 +83,15 @@ public async Task MostBasicPublishWithQoS2Async()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task MultiPublishWithQoS0Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MultiPublishWithQoS0Async").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -104,12 +114,15 @@ public async Task MultiPublishWithQoS0Async()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task MultiPublishWithQoS1Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MultiPublishWithQoS1Async").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -130,12 +143,15 @@ public async Task MultiPublishWithQoS1Async()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task MultiPublishWithQoS2Async()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("MultiPublishWithQoS2Async").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -155,12 +171,15 @@ public async Task MultiPublishWithQoS2Async()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task PublishWithOptionsAsync()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("PublishWithOptionsAsync").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -180,12 +199,15 @@ public async Task PublishWithOptionsAsync()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}

[Fact]
public async Task PublishPayloadFormatIndicatorAsync()
{
var client = new HiveMQClient();
var options = new HiveMQClientOptionsBuilder().WithClientId("PublishPayloadFormatIndicatorAsync").Build();
var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Expand All @@ -208,5 +230,7 @@ public async Task PublishPayloadFormatIndicatorAsync()

var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

client.Dispose();
}
}
Loading

0 comments on commit 5894e4f

Please sign in to comment.