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.
 
 
 
 

186 lines
6.8 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Receiving;
  6. using MQTTnet.Formatter;
  7. using MQTTnet.Protocol;
  8. using MQTTnet.Server;
  9. namespace MQTTnet.Tests.Server
  10. {
  11. [TestClass]
  12. public sealed class Events_Tests : BaseTestClass
  13. {
  14. [TestMethod]
  15. public async Task Fire_Client_Connected_Event()
  16. {
  17. using (var testEnvironment = CreateTestEnvironment())
  18. {
  19. var server = await testEnvironment.StartServer();
  20. MqttServerClientConnectedEventArgs eventArgs = null;
  21. server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e =>
  22. {
  23. eventArgs = e;
  24. });
  25. await testEnvironment.ConnectClient(o => o.WithCredentials("TheUser"));
  26. await LongTestDelay();
  27. Assert.IsNotNull(eventArgs);
  28. Assert.IsTrue(eventArgs.ClientId.StartsWith(nameof(Fire_Client_Connected_Event)));
  29. Assert.IsTrue(eventArgs.Endpoint.Contains("127.0.0.1"));
  30. Assert.AreEqual(MqttProtocolVersion.V311, eventArgs.ProtocolVersion);
  31. Assert.AreEqual("TheUser", eventArgs.UserName);
  32. }
  33. }
  34. [TestMethod]
  35. public async Task Fire_Client_Disconnected_Event()
  36. {
  37. using (var testEnvironment = CreateTestEnvironment())
  38. {
  39. var server = await testEnvironment.StartServer();
  40. MqttServerClientDisconnectedEventArgs eventArgs = null;
  41. server.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(e =>
  42. {
  43. eventArgs = e;
  44. });
  45. var client = await testEnvironment.ConnectClient(o => o.WithCredentials("TheUser"));
  46. await client.DisconnectAsync();
  47. await LongTestDelay();
  48. Assert.IsNotNull(eventArgs);
  49. Assert.IsTrue(eventArgs.ClientId.StartsWith(nameof(Fire_Client_Disconnected_Event)));
  50. Assert.IsTrue(eventArgs.Endpoint.Contains("127.0.0.1"));
  51. Assert.AreEqual(MqttClientDisconnectType.Clean, eventArgs.DisconnectType);
  52. }
  53. }
  54. [TestMethod]
  55. public async Task Fire_Client_Subscribed_Event()
  56. {
  57. using (var testEnvironment = CreateTestEnvironment())
  58. {
  59. var server = await testEnvironment.StartServer();
  60. MqttServerClientSubscribedTopicEventArgs eventArgs = null;
  61. server.ClientSubscribedTopicHandler = new MqttServerClientSubscribedTopicHandlerDelegate(e =>
  62. {
  63. eventArgs = e;
  64. });
  65. var client = await testEnvironment.ConnectClient();
  66. await client.SubscribeAsync("The/Topic", MqttQualityOfServiceLevel.AtLeastOnce);
  67. await LongTestDelay();
  68. Assert.IsNotNull(eventArgs);
  69. Assert.IsTrue(eventArgs.ClientId.StartsWith(nameof(Fire_Client_Subscribed_Event)));
  70. Assert.AreEqual("The/Topic", eventArgs.TopicFilter.Topic);
  71. Assert.AreEqual(MqttQualityOfServiceLevel.AtLeastOnce, eventArgs.TopicFilter.QualityOfServiceLevel);
  72. }
  73. }
  74. [TestMethod]
  75. public async Task Fire_Client_Unsubscribed_Event()
  76. {
  77. using (var testEnvironment = CreateTestEnvironment())
  78. {
  79. var server = await testEnvironment.StartServer();
  80. MqttServerClientUnsubscribedTopicEventArgs eventArgs = null;
  81. server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e =>
  82. {
  83. eventArgs = e;
  84. });
  85. var client = await testEnvironment.ConnectClient();
  86. await client.UnsubscribeAsync("The/Topic");
  87. await LongTestDelay();
  88. Assert.IsNotNull(eventArgs);
  89. Assert.IsTrue(eventArgs.ClientId.StartsWith(nameof(Fire_Client_Unsubscribed_Event)));
  90. Assert.AreEqual("The/Topic", eventArgs.TopicFilter);
  91. }
  92. }
  93. [TestMethod]
  94. public async Task Fire_Application_Message_Received_Event()
  95. {
  96. using (var testEnvironment = CreateTestEnvironment())
  97. {
  98. var server = await testEnvironment.StartServer();
  99. MqttApplicationMessageReceivedEventArgs eventArgs = null;
  100. server.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
  101. {
  102. eventArgs = e;
  103. });
  104. var client = await testEnvironment.ConnectClient();
  105. await client.PublishAsync("The_Topic", "The_Payload");
  106. await LongTestDelay();
  107. Assert.IsNotNull(eventArgs);
  108. Assert.IsTrue(eventArgs.ClientId.StartsWith(nameof(Fire_Application_Message_Received_Event)));
  109. Assert.AreEqual("The_Topic", eventArgs.ApplicationMessage.Topic);
  110. Assert.AreEqual("The_Payload", eventArgs.ApplicationMessage.ConvertPayloadToString());
  111. }
  112. }
  113. [TestMethod]
  114. public async Task Fire_Started_Event()
  115. {
  116. using (var testEnvironment = CreateTestEnvironment())
  117. {
  118. var server = testEnvironment.CreateServer();
  119. EventArgs eventArgs = null;
  120. server.StartedHandler = new MqttServerStartedHandlerDelegate(e =>
  121. {
  122. eventArgs = e;
  123. });
  124. await server.StartAsync(new MqttServerOptionsBuilder().Build());
  125. await LongTestDelay();
  126. Assert.IsNotNull(eventArgs);
  127. }
  128. }
  129. [TestMethod]
  130. public async Task Fire_Stopped_Event()
  131. {
  132. using (var testEnvironment = CreateTestEnvironment())
  133. {
  134. var server = await testEnvironment.StartServer();
  135. EventArgs eventArgs = null;
  136. server.StoppedHandler = new MqttServerStoppedHandlerDelegate(e =>
  137. {
  138. eventArgs = e;
  139. });
  140. await server.StopAsync();
  141. await LongTestDelay();
  142. Assert.IsNotNull(eventArgs);
  143. }
  144. }
  145. }
  146. }