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.

Server_Tests.cs 1.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Options;
  6. using MQTTnet.Formatter;
  7. using MQTTnet.Tests.Mockups;
  8. namespace MQTTnet.Tests.MQTTv5
  9. {
  10. [TestClass]
  11. public class Server_Tests
  12. {
  13. [TestMethod]
  14. public async Task Will_Message_Send()
  15. {
  16. using (var testEnvironment = new TestEnvironment())
  17. {
  18. var receivedMessagesCount = 0;
  19. await testEnvironment.StartServerAsync();
  20. var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
  21. var clientOptions = new MqttClientOptionsBuilder().WithWillMessage(willMessage).WithProtocolVersion(MqttProtocolVersion.V500);
  22. var c1 = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
  23. c1.UseApplicationMessageReceivedHandler(c => Interlocked.Increment(ref receivedMessagesCount));
  24. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
  25. var c2 = await testEnvironment.ConnectClientAsync(clientOptions);
  26. c2.Dispose(); // Dispose will not send a DISCONNECT pattern first so the will message must be sent.
  27. await Task.Delay(1000);
  28. Assert.AreEqual(1, receivedMessagesCount);
  29. }
  30. }
  31. }
  32. }