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.
 
 
 
 

101 lines
3.6 KiB

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Client;
  3. using MQTTnet.Client.Connecting;
  4. using MQTTnet.Client.Disconnecting;
  5. using MQTTnet.Client.ExtendedAuthenticationExchange;
  6. using MQTTnet.Client.Options;
  7. using MQTTnet.Client.Publishing;
  8. using MQTTnet.Client.Receiving;
  9. using MQTTnet.Client.Subscribing;
  10. using MQTTnet.Client.Unsubscribing;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MQTTnet.Tests.Mockups
  14. {
  15. public sealed class TestClientWrapper : IMqttClient
  16. {
  17. public TestClientWrapper(IMqttClient implementation, TestContext testContext)
  18. {
  19. Implementation = implementation;
  20. TestContext = testContext;
  21. }
  22. public IMqttClient Implementation { get; }
  23. public TestContext TestContext { get; }
  24. public bool IsConnected => Implementation.IsConnected;
  25. public IMqttClientOptions Options => Implementation.Options;
  26. public IMqttClientConnectedHandler ConnectedHandler
  27. {
  28. get => Implementation.ConnectedHandler;
  29. set => Implementation.ConnectedHandler = value;
  30. }
  31. public IMqttClientDisconnectedHandler DisconnectedHandler
  32. {
  33. get => Implementation.DisconnectedHandler;
  34. set => Implementation.DisconnectedHandler = value;
  35. }
  36. public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler
  37. {
  38. get => Implementation.ApplicationMessageReceivedHandler;
  39. set => Implementation.ApplicationMessageReceivedHandler = value;
  40. }
  41. public Task<MqttClientConnectResult> ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
  42. {
  43. if (TestContext != null)
  44. {
  45. var clientOptions = (MqttClientOptions)options;
  46. var existingClientId = clientOptions.ClientId;
  47. if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
  48. {
  49. clientOptions.ClientId = TestContext.TestName + existingClientId;
  50. }
  51. }
  52. return Implementation.ConnectAsync(options, cancellationToken);
  53. }
  54. public Task DisconnectAsync(MqttClientDisconnectOptions options, CancellationToken cancellationToken)
  55. {
  56. return Implementation.DisconnectAsync(options, cancellationToken);
  57. }
  58. public void Dispose()
  59. {
  60. Implementation.Dispose();
  61. }
  62. public Task PingAsync(CancellationToken cancellationToken)
  63. {
  64. return Implementation.PingAsync(cancellationToken);
  65. }
  66. public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
  67. {
  68. return Implementation.PublishAsync(applicationMessage, cancellationToken);
  69. }
  70. public Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken)
  71. {
  72. return Implementation.SendExtendedAuthenticationExchangeDataAsync(data, cancellationToken);
  73. }
  74. public Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken)
  75. {
  76. return Implementation.SubscribeAsync(options, cancellationToken);
  77. }
  78. public Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken)
  79. {
  80. return Implementation.UnsubscribeAsync(options, cancellationToken);
  81. }
  82. }
  83. }