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.
 
 
 
 

43 lines
1.5 KiB

  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. public TestContext TestContext { get; set; }
  14. [TestMethod]
  15. public async Task Will_Message_Send()
  16. {
  17. using (var testEnvironment = new TestEnvironment(TestContext))
  18. {
  19. var receivedMessagesCount = 0;
  20. await testEnvironment.StartServerAsync();
  21. var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
  22. var clientOptions = new MqttClientOptionsBuilder().WithWillMessage(willMessage).WithProtocolVersion(MqttProtocolVersion.V500);
  23. var c1 = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
  24. c1.UseApplicationMessageReceivedHandler(c => Interlocked.Increment(ref receivedMessagesCount));
  25. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
  26. var c2 = await testEnvironment.ConnectClientAsync(clientOptions);
  27. c2.Dispose(); // Dispose will not send a DISCONNECT pattern first so the will message must be sent.
  28. await Task.Delay(1000);
  29. Assert.AreEqual(1, receivedMessagesCount);
  30. }
  31. }
  32. }
  33. }