Parcourir la source

Fix broken unit tests

release/3.x.x
Christian il y a 6 ans
Parent
révision
002e4b8d69
5 fichiers modifiés avec 52 ajouts et 10 suppressions
  1. +9
    -7
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs
  2. +4
    -1
      Frameworks/MQTTnet.NetStandard/Server/MqttClientPendingMessagesQueue.cs
  3. +37
    -0
      Tests/MQTTnet.Core.Tests/MqttClientTests.cs
  4. +1
    -1
      Tests/MQTTnet.Core.Tests/MqttPacketReaderTests.cs
  5. +1
    -1
      Tests/MQTTnet.Core.Tests/MqttPacketSerializerTests.cs

+ 9
- 7
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs Voir le fichier

@@ -24,14 +24,17 @@ namespace MQTTnet.Serializer

public static async Task<MqttPacketHeader> ReadHeaderFromSourceAsync(Stream stream, CancellationToken cancellationToken)
{
byte[] singleByteBuf = new byte[1];
var readCount = await stream.ReadAsync(singleByteBuf, 0, singleByteBuf.Length).ConfigureAwait(false);
// Wait for the next package which starts with the header. At this point there will probably
// some large delay and thus the thread should be put back to the pool (await). So ReadByte()
// is not an option here.
var buffer = new byte[1];
var readCount = await stream.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false);
if (readCount <= 0)
{
return null;
}

var fixedHeader = singleByteBuf[0];
var fixedHeader = buffer[0];
var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4);
var bodyLength = await ReadBodyLengthFromSourceAsync(stream, cancellationToken).ConfigureAwait(false);

@@ -88,8 +91,7 @@ namespace MQTTnet.Serializer
var value = 0;
byte encodedByte;

byte[] singleByteBuf = new byte[1];

var buffer = new byte[1];
var readBytes = new List<byte>();
do
{
@@ -98,13 +100,13 @@ namespace MQTTnet.Serializer
throw new TaskCanceledException();
}

int readCount = await stream.ReadAsync(singleByteBuf, 0, singleByteBuf.Length).ConfigureAwait(false);
var readCount = await stream.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false);
if (readCount <= 0)
{
throw new MqttCommunicationException("Connection closed while reading remaining length data.");
}

encodedByte = singleByteBuf[0];
encodedByte = buffer[0];
readBytes.Add(encodedByte);

value += (byte)(encodedByte & 127) * multiplier;


+ 4
- 1
Frameworks/MQTTnet.NetStandard/Server/MqttClientPendingMessagesQueue.cs Voir le fichier

@@ -43,6 +43,7 @@ namespace MQTTnet.Server

_queue.Enqueue(packet);
_queueWaitSemaphore.Release();

_logger.Trace<MqttClientPendingMessagesQueue>("Enqueued packet (ClientId: {0}).", _session.ClientId);
}

@@ -70,9 +71,11 @@ namespace MQTTnet.Server
try
{
await _queueWaitSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
if (!_queue.TryDequeue(out packet)) {
if (!_queue.TryDequeue(out packet))
{
throw new InvalidOperationException(); // should not happen
}

await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, packet).ConfigureAwait(false);

_logger.Trace<MqttClientPendingMessagesQueue>("Enqueued packet sent (ClientId: {0}).", _session.ClientId);


+ 37
- 0
Tests/MQTTnet.Core.Tests/MqttClientTests.cs Voir le fichier

@@ -0,0 +1,37 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Exceptions;

namespace MQTTnet.Core.Tests
{
[TestClass]
public class MqttClientTests
{

[TestMethod]
public async Task ClientDisconnectException()
{
var factory = new MqttFactory();
var client = factory.CreateMqttClient();

var exceptionIsCorrect = false;
client.Disconnected += (s, e) =>
{
exceptionIsCorrect = e.Exception is MqttCommunicationException c && c.InnerException is SocketException;
};

try
{
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("wrong-server").Build());
}
catch
{
}
Assert.IsTrue(exceptionIsCorrect);
}
}
}

+ 1
- 1
Tests/MQTTnet.Core.Tests/MqttPacketReaderTests.cs Voir le fichier

@@ -12,7 +12,7 @@ namespace MQTTnet.Core.Tests
public void MqttPacketReader_EmptyStream()
{
var memStream = new MemoryStream();
var header = MqttPacketReader.ReadHeaderFromSource(memStream, CancellationToken.None);
var header = MqttPacketReader.ReadHeaderFromSourceAsync(memStream, CancellationToken.None).GetAwaiter().GetResult();

Assert.IsNull(header);
}


+ 1
- 1
Tests/MQTTnet.Core.Tests/MqttPacketSerializerTests.cs Voir le fichier

@@ -405,7 +405,7 @@ namespace MQTTnet.Core.Tests

using (var headerStream = new MemoryStream(Join(buffer1)))
{
var header = MqttPacketReader.ReadHeaderFromSource(headerStream, CancellationToken.None);
var header = MqttPacketReader.ReadHeaderFromSourceAsync(headerStream, CancellationToken.None).GetAwaiter().GetResult();

using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.BodyLength))
{


Chargement…
Annuler
Enregistrer