Browse Source

Add unit Tests.

release/3.x.x
Christian Kratky 5 years ago
parent
commit
ab8c7df0b9
1 changed files with 77 additions and 0 deletions
  1. +77
    -0
      Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs

+ 77
- 0
Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs View File

@@ -109,6 +109,83 @@ namespace MQTTnet.Tests
} }
} }


[TestMethod]
public async Task Reconnect_While_Server_Offline()
{
using (var testEnvironment = new TestEnvironment())
{
testEnvironment.IgnoreClientLogErrors = true;

var server = await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();

await Task.Delay(500);
Assert.IsTrue(client.IsConnected);

await server.StopAsync();
await Task.Delay(500);
Assert.IsFalse(client.IsConnected);

for (var i = 0; i < 5; i++)
{
try
{
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
Assert.Fail("Must fail!");
}
catch
{
}
}

await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultEndpointPort(testEnvironment.ServerPort).Build());
await Task.Delay(500);

await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
Assert.IsTrue(client.IsConnected);
}
}

[TestMethod]
public async Task Reconnect_From_Disconnected_Event()
{
using (var testEnvironment = new TestEnvironment())
{
testEnvironment.IgnoreClientLogErrors = true;

var client = testEnvironment.CreateClient();

var tries = 0;
var maxTries = 3;

client.UseDisconnectedHandler(async e =>
{
if (tries >= maxTries)
{
return;
}

Interlocked.Increment(ref tries);

await Task.Delay(100);
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
});

try
{
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
Assert.Fail("Must fail!");
}
catch
{
}
SpinWait.SpinUntil(() => tries >= maxTries, 10000);

Assert.AreEqual(maxTries, tries);
}
}

[TestMethod] [TestMethod]
public async Task PacketIdentifier_In_Publish_Result() public async Task PacketIdentifier_In_Publish_Result()
{ {


Loading…
Cancel
Save