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.

Session_Tests.cs 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Client;
  3. using MQTTnet.Client.Options;
  4. using MQTTnet.Client.Subscribing;
  5. using MQTTnet.Server;
  6. using MQTTnet.Tests.Mockups;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace MQTTnet.Tests
  11. {
  12. [TestClass]
  13. public class Session_Tests
  14. {
  15. public TestContext TestContext { get; set; }
  16. [TestMethod]
  17. public async Task Set_Session_Item()
  18. {
  19. using (var testEnvironment = new TestEnvironment(TestContext))
  20. {
  21. var serverOptions = new MqttServerOptionsBuilder()
  22. .WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
  23. {
  24. // Don't validate anything. Just set some session items.
  25. context.SessionItems["can_subscribe_x"] = true;
  26. context.SessionItems["default_payload"] = "Hello World";
  27. })
  28. .WithSubscriptionInterceptor(delegate (MqttSubscriptionInterceptorContext context)
  29. {
  30. if (context.TopicFilter.Topic == "x")
  31. {
  32. context.AcceptSubscription = context.SessionItems["can_subscribe_x"] as bool? == true;
  33. }
  34. })
  35. .WithApplicationMessageInterceptor(delegate (MqttApplicationMessageInterceptorContext context)
  36. {
  37. context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(context.SessionItems["default_payload"] as string);
  38. });
  39. await testEnvironment.StartServerAsync(serverOptions);
  40. string receivedPayload = null;
  41. var client = await testEnvironment.ConnectClientAsync();
  42. client.UseApplicationMessageReceivedHandler(delegate (MqttApplicationMessageReceivedEventArgs args)
  43. {
  44. receivedPayload = args.ApplicationMessage.ConvertPayloadToString();
  45. });
  46. var subscribeResult = await client.SubscribeAsync("x");
  47. Assert.AreEqual(MqttClientSubscribeResultCode.GrantedQoS0, subscribeResult.Items[0].ResultCode);
  48. var client2 = await testEnvironment.ConnectClientAsync();
  49. await client2.PublishAsync("x");
  50. await Task.Delay(1000);
  51. Assert.AreEqual("Hello World", receivedPayload);
  52. }
  53. }
  54. [TestMethod]
  55. public async Task Get_Session_Items_In_Status()
  56. {
  57. using (var testEnvironment = new TestEnvironment(TestContext))
  58. {
  59. var serverOptions = new MqttServerOptionsBuilder()
  60. .WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
  61. {
  62. // Don't validate anything. Just set some session items.
  63. context.SessionItems["can_subscribe_x"] = true;
  64. context.SessionItems["default_payload"] = "Hello World";
  65. });
  66. await testEnvironment.StartServerAsync(serverOptions);
  67. var client = await testEnvironment.ConnectClientAsync();
  68. var sessionStatus = await testEnvironment.Server.GetSessionStatusAsync();
  69. var session = sessionStatus.First();
  70. Assert.AreEqual(true, session.Items["can_subscribe_x"]);
  71. }
  72. }
  73. [TestMethod]
  74. public async Task Manage_Session_MaxParallel()
  75. {
  76. using (var testEnvironment = new TestEnvironment(TestContext))
  77. {
  78. testEnvironment.IgnoreClientLogErrors = true;
  79. var serverOptions = new MqttServerOptionsBuilder();
  80. await testEnvironment.StartServerAsync(serverOptions);
  81. var options = new MqttClientOptionsBuilder().WithClientId("1");
  82. var clients = await Task.WhenAll(Enumerable.Range(0, 10)
  83. .Select(i => TryConnect(testEnvironment, options)));
  84. var connectedClients = clients.Where(c => c?.IsConnected ?? false).ToList();
  85. Assert.AreEqual(1, connectedClients.Count);
  86. }
  87. }
  88. async Task<IMqttClient> TryConnect(TestEnvironment testEnvironment, MqttClientOptionsBuilder options)
  89. {
  90. try
  91. {
  92. return await testEnvironment.ConnectClientAsync(options);
  93. }
  94. catch (System.Exception)
  95. {
  96. return null;
  97. }
  98. }
  99. }
  100. }