mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Add cancellation token to adapter
This commit is contained in:
@@ -11,23 +11,61 @@ namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
|
||||
[TestMethod]
|
||||
public async Task TestTimeoutAfter()
|
||||
public async Task TimeoutAfter()
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(500)).TimeoutAfter(TimeSpan.FromMilliseconds(100));
|
||||
}
|
||||
|
||||
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
|
||||
[TestMethod]
|
||||
public async Task TestTimeoutAfterWithResult()
|
||||
public async Task TimeoutAfterWithResult()
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(500)).ContinueWith(t => 5).TimeoutAfter(TimeSpan.FromMilliseconds(100));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestTimeoutAfterCompleteInTime()
|
||||
public async Task TimeoutAfterCompleteInTime()
|
||||
{
|
||||
var result = await Task.Delay(TimeSpan.FromMilliseconds(100)).ContinueWith(t => 5).TimeoutAfter(TimeSpan.FromMilliseconds(500));
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TimeoutAfterWithInnerException()
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var iis = new int[0];
|
||||
iis[1] = 0;
|
||||
}).TimeoutAfter(TimeSpan.FromSeconds(1));
|
||||
|
||||
Assert.Fail();
|
||||
}
|
||||
catch (MqttCommunicationException e)
|
||||
{
|
||||
Assert.IsTrue(e.InnerException is IndexOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TimeoutAfterWithInnerExceptionWithResult()
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = await Task.Run(() =>
|
||||
{
|
||||
var iis = new int[0];
|
||||
return iis[1];
|
||||
}).TimeoutAfter(TimeSpan.FromSeconds(1));
|
||||
|
||||
Assert.Fail();
|
||||
}
|
||||
catch (MqttCommunicationException e)
|
||||
{
|
||||
Assert.IsTrue(e.InnerException is IndexOutOfRangeException);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Adapter;
|
||||
@@ -442,7 +443,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
using (var headerStream = new MemoryStream(buffer1))
|
||||
{
|
||||
var header = MqttPacketReader.ReadHeaderFromSource(headerStream);
|
||||
var header = MqttPacketReader.ReadHeaderFromSource(headerStream, CancellationToken.None);
|
||||
|
||||
using (var bodyStream = new MemoryStream(buffer1, (int)headerStream.Position, header.BodyLength))
|
||||
{
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace MQTTnet.Core.Tests
|
||||
await Task.Delay(500);
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
|
||||
await c1.Unsubscribe("a");
|
||||
await c1.UnsubscribeAsync("a");
|
||||
await c2.PublishAsync(message);
|
||||
|
||||
await Task.Delay(500);
|
||||
@@ -158,7 +158,7 @@ namespace MQTTnet.Core.Tests
|
||||
await c2.PublishAsync(new MqttApplicationMessage(topic, new byte[0], qualityOfServiceLevel, false));
|
||||
|
||||
await Task.Delay(500);
|
||||
await c1.Unsubscribe(topicFilter);
|
||||
await c1.UnsubscribeAsync(topicFilter);
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace MQTTnet.Core.Tests
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task SendPacketsAsync(TimeSpan timeout, IEnumerable<MqttBasePacket> packets)
|
||||
public Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, IEnumerable<MqttBasePacket> packets)
|
||||
{
|
||||
ThrowIfPartnerIsNull();
|
||||
|
||||
@@ -40,16 +40,11 @@ namespace MQTTnet.Core.Tests
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
|
||||
public Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
||||
{
|
||||
ThrowIfPartnerIsNull();
|
||||
|
||||
return Task.Run(() => _incomingPackets.Take());
|
||||
}
|
||||
|
||||
public IEnumerable<MqttBasePacket> ReceivePackets(CancellationToken cancellationToken)
|
||||
{
|
||||
return _incomingPackets.GetConsumingEnumerable();
|
||||
return Task.Run(() => _incomingPackets.Take(), cancellationToken);
|
||||
}
|
||||
|
||||
private void SendPacketInternal(MqttBasePacket packet)
|
||||
|
||||
Reference in New Issue
Block a user