Browse Source

Update tests.

release/3.x.x
Christian Kratky 5 years ago
parent
commit
7f08371532
3 changed files with 119 additions and 37 deletions
  1. +55
    -0
      Tests/MQTTnet.Core.Tests/MQTTv5/Feature_Tests.cs
  2. +34
    -0
      Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs
  3. +30
    -37
      Tests/MQTTnet.Core.Tests/RoundtripTime_Tests.cs

+ 55
- 0
Tests/MQTTnet.Core.Tests/MQTTv5/Feature_Tests.cs View File

@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Client.Subscribing;
using MQTTnet.Formatter;
using MQTTnet.Tests.Mockups;

namespace MQTTnet.Tests.MQTTv5
{
[TestClass]
public class Feature_Tests
{
[TestMethod]
public async Task Use_User_Properties()
{
using (var testEnvironment = new TestEnvironment())
{
await testEnvironment.StartServerAsync();

var sender = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
var receiver = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));

var message = new MqttApplicationMessageBuilder()
.WithTopic("A")
.WithUserProperty("x", "1")
.WithUserProperty("y", "2")
.WithUserProperty("z", "3")
.WithUserProperty("z", "4"); // z is here two times to test list of items

await receiver.SubscribeAsync(new MqttClientSubscribeOptions
{
TopicFilters = new List<TopicFilter>
{
new TopicFilter { Topic = "#" }
}
}, CancellationToken.None);

MqttApplicationMessage receivedMessage = null;
receiver.UseApplicationMessageReceivedHandler(e => receivedMessage = e.ApplicationMessage);

await sender.PublishAsync(message.Build(), CancellationToken.None);

await Task.Delay(1000);

Assert.IsNotNull(receivedMessage);
Assert.AreEqual("A", receivedMessage.Topic);
Assert.AreEqual(4, receivedMessage.UserProperties.Count);

}
}
}
}

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

@@ -2,13 +2,16 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using MQTTnet.Client.Subscribing;
using MQTTnet.Exceptions;
using MQTTnet.Formatter;
using MQTTnet.Protocol;
using MQTTnet.Server;
using MQTTnet.Tests.Mockups;
@@ -339,5 +342,36 @@ namespace MQTTnet.Tests
Assert.AreEqual("1", receivedPayload);
}
}

[TestMethod]
public async Task No_Payload()
{
using (var testEnvironment = new TestEnvironment())
{
await testEnvironment.StartServerAsync();

var sender = await testEnvironment.ConnectClientAsync();
var receiver = await testEnvironment.ConnectClientAsync();

var message = new MqttApplicationMessageBuilder()
.WithTopic("A");

await receiver.SubscribeAsync(new MqttClientSubscribeOptions
{
TopicFilters = new List<TopicFilter> { new TopicFilter { Topic = "#" } }
}, CancellationToken.None);

MqttApplicationMessage receivedMessage = null;
receiver.UseApplicationMessageReceivedHandler(e => receivedMessage = e.ApplicationMessage);

await sender.PublishAsync(message.Build(), CancellationToken.None);

await Task.Delay(1000);

Assert.IsNotNull(receivedMessage);
Assert.AreEqual("A", receivedMessage.Topic);
Assert.AreEqual(null, receivedMessage.Payload);
}
}
}
}

+ 30
- 37
Tests/MQTTnet.Core.Tests/RoundtripTime_Tests.cs View File

@@ -4,9 +4,7 @@ using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Client.Receiving;
using MQTTnet.Server;
using MQTTnet.Tests.Mockups;

namespace MQTTnet.Tests
{
@@ -16,42 +14,37 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Round_Trip_Time()
{
var factory = new MqttFactory();
var server = factory.CreateMqttServer();
var receiverClient = factory.CreateMqttClient();
var senderClient = factory.CreateMqttClient();

await server.StartAsync(new MqttServerOptionsBuilder().Build());

await receiverClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
await receiverClient.SubscribeAsync("#");

await senderClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());

TaskCompletionSource<string> response = null;

receiverClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(args =>
using (var testEnvironment = new TestEnvironment())
{
response?.SetResult(args.ApplicationMessage.ConvertPayloadToString());
});

var times = new List<TimeSpan>();
var stopwatch = Stopwatch.StartNew();

await Task.Delay(1000);

for (var i = 0; i < 100; i++)
{
response = new TaskCompletionSource<string>();
await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
response.Task.GetAwaiter().GetResult();

stopwatch.Stop();
times.Add(stopwatch.Elapsed);
stopwatch.Restart();
await testEnvironment.StartServerAsync();
var receiverClient = await testEnvironment.ConnectClientAsync();
var senderClient = await testEnvironment.ConnectClientAsync();

await receiverClient.SubscribeAsync("#");
TaskCompletionSource<string> response = null;

receiverClient.UseApplicationMessageReceivedHandler(e =>
{
response?.SetResult(e.ApplicationMessage.ConvertPayloadToString());
});

var times = new List<TimeSpan>();
var stopwatch = Stopwatch.StartNew();

await Task.Delay(1000);

for (var i = 0; i < 100; i++)
{
response = new TaskCompletionSource<string>();
await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
response.Task.GetAwaiter().GetResult();

stopwatch.Stop();
times.Add(stopwatch.Elapsed);
stopwatch.Restart();
}
}

await server.StopAsync();
}
}
}

Loading…
Cancel
Save