25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

49 satır
1.7 KiB

  1. #if NETCOREAPP
  2. using System.Buffers;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Formatter;
  5. using MQTTnet.Packets;
  6. namespace MQTTnet.AspNetCore.Tests
  7. {
  8. [TestClass]
  9. public class ReaderExtensionsTest
  10. {
  11. [TestMethod]
  12. public void TestTryDeserialize()
  13. {
  14. var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  15. var buffer = serializer.Encode(new MqttPublishPacket() {Topic = "a", Payload = new byte[5]});
  16. var sequence = new ReadOnlySequence<byte>(buffer.Array, buffer.Offset, buffer.Count);
  17. var part = sequence;
  18. MqttBasePacket packet;
  19. var consumed = part.Start;
  20. var observed = part.Start;
  21. var result = false;
  22. var read = 0;
  23. var reader = new SpanBasedMqttPacketBodyReader();
  24. part = sequence.Slice(sequence.Start, 0); // empty message should fail
  25. result = serializer.TryDecode(reader, part, out packet, out consumed, out observed, out read);
  26. Assert.IsFalse(result);
  27. part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
  28. result = serializer.TryDecode(reader, part, out packet, out consumed, out observed, out read);
  29. Assert.IsFalse(result);
  30. part = sequence.Slice(sequence.Start, 4); // partial body should fail
  31. result = serializer.TryDecode(reader, part, out packet, out consumed, out observed, out read);
  32. Assert.IsFalse(result);
  33. part = sequence; // complete msg should work
  34. result = serializer.TryDecode(reader, part, out packet, out consumed, out observed, out read);
  35. Assert.IsTrue(result);
  36. }
  37. }
  38. }
  39. #endif