Bläddra i källkod

Add unit Tests.

release/3.x.x
Christian Kratky 5 år sedan
förälder
incheckning
ab8c7df0b9
1 ändrade filer med 77 tillägg och 0 borttagningar
  1. +77
    -0
      Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs

+ 77
- 0
Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs Visa fil

@@ -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]
public async Task PacketIdentifier_In_Publish_Result()
{


Laddar…
Avbryt
Spara