25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

60 lines
1.7 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Core.Adapter;
  5. using MQTTnet.Core.Client;
  6. using MQTTnet.Core.Packets;
  7. using MQTTnet.Core.Serializer;
  8. namespace MQTTnet.Core.Tests
  9. {
  10. public class TestMqttCommunicationAdapter : IMqttCommunicationAdapter
  11. {
  12. private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
  13. public TestMqttCommunicationAdapter Partner { get; set; }
  14. public IMqttPacketSerializer PacketSerializer { get; } = new MqttPacketSerializer();
  15. public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
  16. {
  17. await Task.FromResult(0).ConfigureAwait(false);
  18. }
  19. public async Task DisconnectAsync()
  20. {
  21. await Task.FromResult(0).ConfigureAwait(false);
  22. }
  23. public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout)
  24. {
  25. ThrowIfPartnerIsNull();
  26. Partner.SendPacketInternal(packet);
  27. await Task.FromResult(0).ConfigureAwait(false);
  28. }
  29. public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
  30. {
  31. ThrowIfPartnerIsNull();
  32. return await Task.Run(() => _incomingPackets.Take()).ConfigureAwait(false);
  33. }
  34. private void SendPacketInternal(MqttBasePacket packet)
  35. {
  36. if (packet == null) throw new ArgumentNullException(nameof(packet));
  37. _incomingPackets.Add(packet);
  38. }
  39. private void ThrowIfPartnerIsNull()
  40. {
  41. if (Partner == null)
  42. {
  43. throw new InvalidOperationException("Partner is not set.");
  44. }
  45. }
  46. }
  47. }