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.
 
 
 
 

127 lines
4.6 KiB

  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using MQTTnet.Tests.Mockups;
  6. using MQTTnet.Client;
  7. using MQTTnet.Client.Receiving;
  8. using MQTTnet.Exceptions;
  9. using MQTTnet.Extensions.Rpc;
  10. using MQTTnet.Protocol;
  11. using MQTTnet.Client.Options;
  12. using MQTTnet.Formatter;
  13. using MQTTnet.Extensions.Rpc.Options;
  14. using MQTTnet.Extensions.Rpc.Options.TopicGeneration;
  15. namespace MQTTnet.Tests
  16. {
  17. [TestClass]
  18. public class RPC_Tests
  19. {
  20. public TestContext TestContext { get; set; }
  21. [TestMethod]
  22. public Task Execute_Success_With_QoS_0()
  23. {
  24. return Execute_Success(MqttQualityOfServiceLevel.AtMostOnce, MqttProtocolVersion.V311);
  25. }
  26. [TestMethod]
  27. public Task Execute_Success_With_QoS_1()
  28. {
  29. return Execute_Success(MqttQualityOfServiceLevel.AtLeastOnce, MqttProtocolVersion.V311);
  30. }
  31. [TestMethod]
  32. public Task Execute_Success_With_QoS_2()
  33. {
  34. return Execute_Success(MqttQualityOfServiceLevel.ExactlyOnce, MqttProtocolVersion.V311);
  35. }
  36. [TestMethod]
  37. public Task Execute_Success_With_QoS_0_MQTT_V5()
  38. {
  39. return Execute_Success(MqttQualityOfServiceLevel.AtMostOnce, MqttProtocolVersion.V500);
  40. }
  41. [TestMethod]
  42. public Task Execute_Success_With_QoS_1_MQTT_V5()
  43. {
  44. return Execute_Success(MqttQualityOfServiceLevel.AtLeastOnce, MqttProtocolVersion.V500);
  45. }
  46. [TestMethod]
  47. public Task Execute_Success_With_QoS_2_MQTT_V5()
  48. {
  49. return Execute_Success(MqttQualityOfServiceLevel.ExactlyOnce, MqttProtocolVersion.V500);
  50. }
  51. [TestMethod]
  52. [ExpectedException(typeof(MqttCommunicationTimedOutException))]
  53. public async Task Execute_Timeout()
  54. {
  55. using (var testEnvironment = new TestEnvironment(TestContext))
  56. {
  57. await testEnvironment.StartServerAsync();
  58. var requestSender = await testEnvironment.ConnectClientAsync();
  59. var rpcClient = new MqttRpcClient(requestSender, new MqttRpcClientOptionsBuilder().Build());
  60. await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(2), "ping", "", MqttQualityOfServiceLevel.AtMostOnce);
  61. }
  62. }
  63. [TestMethod]
  64. [ExpectedException(typeof(MqttCommunicationTimedOutException))]
  65. public async Task Execute_With_Custom_Topic_Names()
  66. {
  67. using (var testEnvironment = new TestEnvironment(TestContext))
  68. {
  69. await testEnvironment.StartServerAsync();
  70. var requestSender = await testEnvironment.ConnectClientAsync();
  71. var rpcClient = await testEnvironment.ConnectRpcClientAsync(new MqttRpcClientOptionsBuilder().WithTopicGenerationStrategy(new TestTopicStrategy()) .Build());
  72. await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(2), "ping", "", MqttQualityOfServiceLevel.AtMostOnce);
  73. }
  74. }
  75. async Task Execute_Success(MqttQualityOfServiceLevel qosLevel, MqttProtocolVersion protocolVersion)
  76. {
  77. using (var testEnvironment = new TestEnvironment(TestContext))
  78. {
  79. await testEnvironment.StartServerAsync();
  80. var responseSender = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(protocolVersion));
  81. await responseSender.SubscribeAsync("MQTTnet.RPC/+/ping", qosLevel);
  82. responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e =>
  83. {
  84. await responseSender.PublishAsync(e.ApplicationMessage.Topic + "/response", "pong");
  85. });
  86. var requestSender = await testEnvironment.ConnectClientAsync();
  87. using (var rpcClient = new MqttRpcClient(requestSender, new MqttRpcClientOptionsBuilder().Build()))
  88. {
  89. var response = await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(5), "ping", "", qosLevel);
  90. Assert.AreEqual("pong", Encoding.UTF8.GetString(response));
  91. }
  92. }
  93. }
  94. class TestTopicStrategy : IMqttRpcClientTopicGenerationStrategy
  95. {
  96. public MqttRpcTopicPair CreateRpcTopics(TopicGenerationContext context)
  97. {
  98. return new MqttRpcTopicPair
  99. {
  100. RequestTopic = "a",
  101. ResponseTopic = "b"
  102. };
  103. }
  104. }
  105. }
  106. }