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_TopicAlias_Tests.cs 1.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using MQTTnet.Client;
  6. using MQTTnet.Formatter;
  7. using MQTTnet.Tests.Mockups;
  8. namespace MQTTnet.Tests
  9. {
  10. [TestClass]
  11. public class Server_TopicAlias_Tests
  12. {
  13. [TestMethod]
  14. public async Task Publish_After_Client_Connects()
  15. {
  16. using (var testEnvironment = new TestEnvironment())
  17. {
  18. await testEnvironment.StartServerAsync();
  19. var receivedTopics = new List<string>();
  20. var c1 = await testEnvironment.ConnectClientAsync(options => options.WithProtocolVersion(MqttProtocolVersion.V500));
  21. c1.UseApplicationMessageReceivedHandler(e =>
  22. {
  23. lock (receivedTopics)
  24. {
  25. receivedTopics.Add(e.ApplicationMessage.Topic);
  26. }
  27. });
  28. await c1.SubscribeAsync("#");
  29. var c2 = await testEnvironment.ConnectClientAsync(options => options.WithProtocolVersion(MqttProtocolVersion.V500));
  30. var message = new MqttApplicationMessage
  31. {
  32. Topic = "this_is_the_topic",
  33. TopicAlias = 22
  34. };
  35. await c2.PublishAsync(message);
  36. message.Topic = null;
  37. await c2.PublishAsync(message);
  38. await c2.PublishAsync(message);
  39. await Task.Delay(500);
  40. Assert.AreEqual(3, receivedTopics.Count);
  41. CollectionAssert.AllItemsAreNotNull(receivedTopics);
  42. Assert.IsTrue(receivedTopics.All(t => t.Equals("this_is_the_topic")));
  43. }
  44. }
  45. }
  46. }