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.
 
 
 
 

47 satır
1.6 KiB

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