Browse Source

Add UnitTests.

release/3.x.x
Christian Kratky 5 years ago
parent
commit
cdbdfdec6d
5 changed files with 65 additions and 8 deletions
  1. +1
    -1
      Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs
  2. +2
    -1
      Source/MQTTnet/Server/MqttRetainedMessagesManager.cs
  3. +28
    -1
      Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs
  4. +4
    -3
      Tests/MQTTnet.Core.Tests/MqttTcpChannel_Tests.cs
  5. +30
    -2
      Tests/MQTTnet.Core.Tests/Server_Tests.cs

+ 1
- 1
Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs View File

@@ -216,7 +216,7 @@ namespace MQTTnet.Formatter.V3


return new MqttClientPublishResult return new MqttClientPublishResult
{ {
PacketIdentifier = pubRecPacket.PacketIdentifier,
PacketIdentifier = pubCompPacket.PacketIdentifier,
ReasonCode = MqttClientPublishReasonCode.Success ReasonCode = MqttClientPublishReasonCode.Success
}; };
} }


+ 2
- 1
Source/MQTTnet/Server/MqttRetainedMessagesManager.cs View File

@@ -61,8 +61,9 @@ namespace MQTTnet.Server
using (await _messagesLock.WaitAsync().ConfigureAwait(false)) using (await _messagesLock.WaitAsync().ConfigureAwait(false))
{ {
var saveIsRequired = false; var saveIsRequired = false;
var hasPayload = applicationMessage.Payload?.Any() == true;


if (applicationMessage.Payload?.Any() != true)
if (!hasPayload)
{ {
saveIsRequired = _messages.Remove(applicationMessage.Topic); saveIsRequired = _messages.Remove(applicationMessage.Topic);
_logger.Verbose("Client '{0}' cleared retained message for topic '{1}'.", clientId, applicationMessage.Topic); _logger.Verbose("Client '{0}' cleared retained message for topic '{1}'.", clientId, applicationMessage.Topic);


+ 28
- 1
Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs View File

@@ -11,7 +11,6 @@ using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options; using MQTTnet.Client.Options;
using MQTTnet.Client.Subscribing; using MQTTnet.Client.Subscribing;
using MQTTnet.Exceptions; using MQTTnet.Exceptions;
using MQTTnet.Formatter;
using MQTTnet.Protocol; using MQTTnet.Protocol;
using MQTTnet.Server; using MQTTnet.Server;
using MQTTnet.Tests.Mockups; using MQTTnet.Tests.Mockups;
@@ -21,6 +20,34 @@ namespace MQTTnet.Tests
[TestClass] [TestClass]
public class Client_Tests public class Client_Tests
{ {
[TestMethod]
public async Task PacketIdentifier_In_Publish_Result()
{
using (var testEnvironment = new TestEnvironment())
{
await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();

var result = await client.PublishAsync("a", "a", MqttQualityOfServiceLevel.AtMostOnce);
Assert.AreEqual(null, result.PacketIdentifier);

result = await client.PublishAsync("b", "b", MqttQualityOfServiceLevel.AtMostOnce);
Assert.AreEqual(null, result.PacketIdentifier);

result = await client.PublishAsync("a", "a", MqttQualityOfServiceLevel.AtLeastOnce);
Assert.AreEqual((ushort)1, result.PacketIdentifier);

result = await client.PublishAsync("b", "b", MqttQualityOfServiceLevel.AtLeastOnce);
Assert.AreEqual((ushort)2, result.PacketIdentifier);

result = await client.PublishAsync("a", "a", MqttQualityOfServiceLevel.ExactlyOnce);
Assert.AreEqual((ushort)3, result.PacketIdentifier);

result = await client.PublishAsync("b", "b", MqttQualityOfServiceLevel.ExactlyOnce);
Assert.AreEqual((ushort)4, result.PacketIdentifier);
}
}
[TestMethod] [TestMethod]
public async Task Invalid_Connect_Throws_Exception() public async Task Invalid_Connect_Throws_Exception()
{ {


+ 4
- 3
Tests/MQTTnet.Core.Tests/MqttTcpChannel_Tests.cs View File

@@ -15,11 +15,11 @@ namespace MQTTnet.Tests
public async Task Dispose_Channel_While_Used() public async Task Dispose_Channel_While_Used()
{ {
var ct = new CancellationTokenSource(); var ct = new CancellationTokenSource();
var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


try try
{ {
var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 50000));
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 50001));
serverSocket.Listen(0); serverSocket.Listen(0);


#pragma warning disable 4014 #pragma warning disable 4014
@@ -35,7 +35,7 @@ namespace MQTTnet.Tests
}, ct.Token); }, ct.Token);


var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
await clientSocket.ConnectAsync(IPAddress.Loopback, 50000);
await clientSocket.ConnectAsync(IPAddress.Loopback, 50001);


await Task.Delay(100, ct.Token); await Task.Delay(100, ct.Token);


@@ -68,6 +68,7 @@ namespace MQTTnet.Tests
finally finally
{ {
ct.Cancel(false); ct.Cancel(false);
serverSocket.Dispose();
} }
} }
} }


+ 30
- 2
Tests/MQTTnet.Core.Tests/Server_Tests.cs View File

@@ -540,7 +540,7 @@ namespace MQTTnet.Tests
} }


[TestMethod] [TestMethod]
public async Task Clear_Retained_Message()
public async Task Clear_Retained_Message_With_Empty_Payload()
{ {
using (var testEnvironment = new TestEnvironment()) using (var testEnvironment = new TestEnvironment())
{ {
@@ -566,7 +566,35 @@ namespace MQTTnet.Tests
Assert.AreEqual(0, receivedMessagesCount); Assert.AreEqual(0, receivedMessagesCount);
} }
} }

[TestMethod]
public async Task Clear_Retained_Message_With_Null_Payload()
{
using (var testEnvironment = new TestEnvironment())
{
var receivedMessagesCount = 0;

await testEnvironment.StartServerAsync();

var c1 = await testEnvironment.ConnectClientAsync();

await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload((byte[])null).WithRetainFlag().Build());

await c1.DisconnectAsync();

var c2 = await testEnvironment.ConnectClientAsync();

c2.UseApplicationMessageReceivedHandler(c => Interlocked.Increment(ref receivedMessagesCount));

await Task.Delay(200);
await c2.SubscribeAsync(new TopicFilter { Topic = "retained", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
await Task.Delay(500);

Assert.AreEqual(0, receivedMessagesCount);
}
}

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


Loading…
Cancel
Save