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.
 
 
 
 

94 lines
3.9 KiB

  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Connecting;
  6. using MQTTnet.Client.Disconnecting;
  7. using MQTTnet.Client.ExtendedAuthenticationExchange;
  8. using MQTTnet.Client.Options;
  9. using MQTTnet.Client.Publishing;
  10. using MQTTnet.Client.Receiving;
  11. using MQTTnet.Client.Subscribing;
  12. using MQTTnet.Client.Unsubscribing;
  13. namespace MQTTnet.Tests.Mockups
  14. {
  15. public 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 { get => Implementation.ConnectedHandler; set => Implementation.ConnectedHandler = value; }
  27. public IMqttClientDisconnectedHandler DisconnectedHandler { get => Implementation.DisconnectedHandler; set => Implementation.DisconnectedHandler = value; }
  28. public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get => Implementation.ApplicationMessageReceivedHandler; set => Implementation.ApplicationMessageReceivedHandler = value; }
  29. public Task<MqttClientAuthenticateResult> ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
  30. {
  31. switch (options)
  32. {
  33. case MqttClientOptionsBuilder builder:
  34. {
  35. var existingClientId = builder.Build().ClientId;
  36. if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
  37. {
  38. builder.WithClientId(TestContext.TestName + existingClientId);
  39. }
  40. }
  41. break;
  42. case MqttClientOptions op:
  43. {
  44. var existingClientId = op.ClientId;
  45. if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
  46. {
  47. op.ClientId = TestContext.TestName + existingClientId;
  48. }
  49. }
  50. break;
  51. default:
  52. break;
  53. }
  54. return Implementation.ConnectAsync(options, cancellationToken);
  55. }
  56. public Task DisconnectAsync(MqttClientDisconnectOptions options, CancellationToken cancellationToken)
  57. {
  58. return Implementation.DisconnectAsync(options, cancellationToken);
  59. }
  60. public void Dispose()
  61. {
  62. Implementation.Dispose();
  63. }
  64. public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
  65. {
  66. return Implementation.PublishAsync(applicationMessage, cancellationToken);
  67. }
  68. public Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken)
  69. {
  70. return Implementation.SendExtendedAuthenticationExchangeDataAsync(data, cancellationToken);
  71. }
  72. public Task<Client.Subscribing.MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken)
  73. {
  74. return Implementation.SubscribeAsync(options, cancellationToken);
  75. }
  76. public Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken)
  77. {
  78. return Implementation.UnsubscribeAsync(options, cancellationToken);
  79. }
  80. }
  81. }