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.

ReaderExtensionsTest.cs 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Buffers;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using MQTTnet.AspNetCore;
  4. using MQTTnet.Packets;
  5. using MQTTnet.Serializer;
  6. namespace MQTTnet.AspNetCore.Tests
  7. {
  8. [TestClass]
  9. public class ReaderExtensionsTest
  10. {
  11. [TestMethod]
  12. public void TestTryDeserialize()
  13. {
  14. var serializer = new MqttPacketSerializer();
  15. var buffer = serializer.Serialize(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. part = sequence.Slice(sequence.Start, 0); // empty message should fail
  23. result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
  24. Assert.IsFalse(result);
  25. part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
  26. result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
  27. Assert.IsFalse(result);
  28. part = sequence.Slice(sequence.Start, 4); // partial body should fail
  29. result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
  30. Assert.IsFalse(result);
  31. part = sequence; // complete msg should work
  32. result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
  33. Assert.IsTrue(result);
  34. }
  35. }
  36. }