You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

167 line
5.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using MQTTnet.Adapter;
  9. using MQTTnet.Client;
  10. using MQTTnet.Diagnostics;
  11. using MQTTnet.Exceptions;
  12. using MQTTnet.Packets;
  13. using MQTTnet.Server;
  14. namespace MQTTnet.Core.Tests
  15. {
  16. [TestClass]
  17. public class MqttClientTests
  18. {
  19. [TestMethod]
  20. public async Task Client_Disconnect_Exception()
  21. {
  22. var factory = new MqttFactory();
  23. var client = factory.CreateMqttClient();
  24. Exception ex = null;
  25. client.Disconnected += (s, e) =>
  26. {
  27. ex = e.Exception;
  28. };
  29. try
  30. {
  31. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("wrong-server").Build());
  32. }
  33. catch
  34. {
  35. }
  36. Assert.IsNotNull(ex);
  37. Assert.IsInstanceOfType(ex, typeof(MqttCommunicationException));
  38. Assert.IsInstanceOfType(ex.InnerException, typeof(SocketException));
  39. }
  40. [TestMethod]
  41. public async Task Client_Publish()
  42. {
  43. var server = new MqttFactory().CreateMqttServer();
  44. try
  45. {
  46. var receivedMessages = new List<MqttApplicationMessage>();
  47. await server.StartAsync(new MqttServerOptions());
  48. var client1 = new MqttFactory().CreateMqttClient();
  49. client1.ApplicationMessageReceived += (_, e) =>
  50. {
  51. lock (receivedMessages)
  52. {
  53. receivedMessages.Add(e.ApplicationMessage);
  54. }
  55. };
  56. await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").Build());
  57. await client1.SubscribeAsync("a");
  58. var client2 = new MqttFactory().CreateMqttClient();
  59. await client2.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").Build());
  60. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithRetainFlag().Build();
  61. await client2.PublishAsync(message);
  62. await Task.Delay(500);
  63. Assert.AreEqual(1, receivedMessages.Count);
  64. Assert.IsFalse(receivedMessages.First().Retain); // Must be false even if set above!
  65. }
  66. finally
  67. {
  68. await server.StopAsync();
  69. }
  70. }
  71. [TestMethod]
  72. public async Task Publish_Special_Content()
  73. {
  74. var factory = new MqttFactory();
  75. var server = factory.CreateMqttServer();
  76. var serverOptions = new MqttServerOptionsBuilder().Build();
  77. var receivedMessages = new List<MqttApplicationMessage>();
  78. var client = factory.CreateMqttClient();
  79. try
  80. {
  81. await server.StartAsync(serverOptions);
  82. client.Connected += async (s, e) =>
  83. {
  84. await client.SubscribeAsync("RCU/P1/H0001/R0003");
  85. var msg = new MqttApplicationMessageBuilder()
  86. .WithPayload("DA|18RS00SC00XI0000RV00R100R200R300R400L100L200L300L400Y100Y200AC0102031800BELK0000BM0000|")
  87. .WithTopic("RCU/P1/H0001/R0003");
  88. await client.PublishAsync(msg.Build());
  89. };
  90. client.ApplicationMessageReceived += (s, e) =>
  91. {
  92. lock (receivedMessages)
  93. {
  94. receivedMessages.Add(e.ApplicationMessage);
  95. }
  96. };
  97. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  98. await Task.Delay(500);
  99. Assert.AreEqual(1, receivedMessages.Count);
  100. Assert.AreEqual("DA|18RS00SC00XI0000RV00R100R200R300R400L100L200L300L400Y100Y200AC0102031800BELK0000BM0000|", receivedMessages.First().ConvertPayloadToString());
  101. }
  102. finally
  103. {
  104. await server.StopAsync();
  105. }
  106. }
  107. #if DEBUG
  108. [TestMethod]
  109. public async Task Client_Cleanup_On_Authentification_Fails()
  110. {
  111. var channel = new TestMqttCommunicationAdapter();
  112. var channel2 = new TestMqttCommunicationAdapter();
  113. channel.Partner = channel2;
  114. channel2.Partner = channel;
  115. Task.Run(async () => {
  116. var connect = await channel2.ReceivePacketAsync(TimeSpan.Zero, CancellationToken.None);
  117. await channel2.SendPacketAsync(new MqttConnAckPacket
  118. {
  119. ConnectReturnCode = Protocol.MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  120. }, CancellationToken.None);
  121. });
  122. var fake = new TestMqttCommunicationAdapterFactory(channel);
  123. var client = new MqttClient(fake, new MqttNetLogger());
  124. try
  125. {
  126. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("any-server").Build());
  127. }
  128. catch (Exception ex)
  129. {
  130. Assert.IsInstanceOfType(ex, typeof(MqttConnectingFailedException));
  131. }
  132. Assert.IsTrue(client._packetReceiverTask == null || client._packetReceiverTask.IsCompleted, "receive loop not completed");
  133. Assert.IsTrue(client._keepAliveMessageSenderTask == null || client._keepAliveMessageSenderTask.IsCompleted, "keepalive loop not completed");
  134. }
  135. #endif
  136. }
  137. }