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.
 
 
 
 

56 line
1.9 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. namespace MQTTnet.Tests
  12. {
  13. [TestClass]
  14. public class RPC_Tests
  15. {
  16. [TestMethod]
  17. public async Task Execute_Success()
  18. {
  19. using (var testEnvironment = new TestEnvironment())
  20. {
  21. await testEnvironment.StartServerAsync();
  22. var responseSender = await testEnvironment.ConnectClientAsync();
  23. await responseSender.SubscribeAsync("MQTTnet.RPC/+/ping");
  24. responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e =>
  25. {
  26. await responseSender.PublishAsync(e.ApplicationMessage.Topic + "/response", "pong");
  27. });
  28. var requestSender = await testEnvironment.ConnectClientAsync();
  29. var rpcClient = new MqttRpcClient(requestSender);
  30. var response = await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(5), "ping", "", MqttQualityOfServiceLevel.AtMostOnce);
  31. Assert.AreEqual("pong", Encoding.UTF8.GetString(response));
  32. }
  33. }
  34. [TestMethod]
  35. [ExpectedException(typeof(MqttCommunicationTimedOutException))]
  36. public async Task Execute_Timeout()
  37. {
  38. using (var testEnvironment = new TestEnvironment())
  39. {
  40. await testEnvironment.StartServerAsync();
  41. var requestSender = await testEnvironment.ConnectClientAsync();
  42. var rpcClient = new MqttRpcClient(requestSender);
  43. await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(2), "ping", "", MqttQualityOfServiceLevel.AtMostOnce);
  44. }
  45. }
  46. }
  47. }