mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 11:03:07 +08:00
Refactor code.
This commit is contained in:
@@ -23,10 +23,8 @@ using MQTTnet.Tests.Mockups;
|
||||
namespace MQTTnet.Tests.Client
|
||||
{
|
||||
[TestClass]
|
||||
public class Client_Tests
|
||||
public class Client_Tests : BaseTestClass
|
||||
{
|
||||
public TestContext TestContext { get; set; }
|
||||
|
||||
[TestMethod]
|
||||
public async Task Ensure_Queue_Drain()
|
||||
{
|
||||
@@ -690,7 +688,7 @@ namespace MQTTnet.Tests.Client
|
||||
[TestMethod]
|
||||
public async Task Message_Send_Retry()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
testEnvironment.IgnoreServerLogErrors = true;
|
||||
@@ -914,40 +912,5 @@ namespace MQTTnet.Tests.Client
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(MqttQualityOfServiceLevel.ExactlyOnce)]
|
||||
[DataRow(MqttQualityOfServiceLevel.AtLeastOnce)]
|
||||
public async Task Retry_If_Not_PubAck(MqttQualityOfServiceLevel qos)
|
||||
{
|
||||
long count = 0;
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
var publisher = await testEnvironment.ConnectClient();
|
||||
var subscriber = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId(qos.ToString()));
|
||||
await subscriber.SubscribeAsync("#", qos);
|
||||
|
||||
subscriber.UseApplicationMessageReceivedHandler(c =>
|
||||
{
|
||||
c.AutoAcknowledge = false;
|
||||
++count;
|
||||
Console.WriteLine("process");
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
var pub = publisher.PublishAsync("a", null, qos);
|
||||
|
||||
await Task.Delay(100);
|
||||
await subscriber.DisconnectAsync();
|
||||
await subscriber.ReconnectAsync();
|
||||
await Task.Delay(100);
|
||||
|
||||
var res = await pub;
|
||||
|
||||
Assert.AreEqual(MqttClientPublishReasonCode.Success, res.ReasonCode);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,16 +27,7 @@ namespace MQTTnet.Tests
|
||||
ct => Task.Delay(TimeSpan.FromMilliseconds(500), ct).ContinueWith(t => 5, ct),
|
||||
TimeSpan.FromMilliseconds(100), CancellationToken.None);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TimeoutAfterCompleteInTime()
|
||||
{
|
||||
var result = await MqttTaskTimeout.WaitAsync(
|
||||
ct => Task.Delay(TimeSpan.FromMilliseconds(100), ct).ContinueWith(t => 5, ct),
|
||||
TimeSpan.FromMilliseconds(500), CancellationToken.None);
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TimeoutAfterWithInnerException()
|
||||
{
|
||||
|
||||
@@ -118,7 +118,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
await client.DisconnectAsync();
|
||||
|
||||
Assert.AreEqual(1, result.Items.Count);
|
||||
Assert.AreEqual(MqttClientUnsubscribeResultCode.Success, result.Items[0].ReasonCode);
|
||||
Assert.AreEqual(MqttClientUnsubscribeResultCode.Success, result.Items[0].ResultCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace MQTTnet.Tests.Mockups
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
||||
public Task DisconnectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Formatter.V3;
|
||||
using MQTTnet.Formatter.V5;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
@@ -94,13 +93,11 @@ namespace MQTTnet.Tests
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true,
|
||||
WillMessage = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "My/last/will",
|
||||
Payload = Encoding.UTF8.GetBytes("Good byte."),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Retain = true
|
||||
}
|
||||
WillFlag = true,
|
||||
WillTopic = "My/last/will",
|
||||
WillMessage = Encoding.UTF8.GetBytes("Good byte."),
|
||||
WillQoS = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
WillRetain = true
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
|
||||
@@ -131,13 +128,11 @@ namespace MQTTnet.Tests
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true,
|
||||
WillMessage = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "My/last/will",
|
||||
Payload = Encoding.UTF8.GetBytes("Good byte."),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Retain = true
|
||||
}
|
||||
WillFlag = true,
|
||||
WillTopic = "My/last/will",
|
||||
WillMessage = Encoding.UTF8.GetBytes("Good byte."),
|
||||
WillQoS = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
WillRetain = true
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Publishing;
|
||||
using MQTTnet.Client.Subscribing;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
@@ -165,6 +166,46 @@ namespace MQTTnet.Tests.Server
|
||||
Assert.AreEqual("injected_one", messageReceivedHandler.ReceivedApplicationMessages[0].Topic);
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(MqttQualityOfServiceLevel.ExactlyOnce)]
|
||||
[DataRow(MqttQualityOfServiceLevel.AtLeastOnce)]
|
||||
public async Task Retry_If_Not_PubAck(MqttQualityOfServiceLevel qos)
|
||||
{
|
||||
long count = 0;
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(o => o.WithPersistentSessions());
|
||||
|
||||
var publisher = await testEnvironment.ConnectClient();
|
||||
|
||||
var subscriber = await testEnvironment.ConnectClient(o => o
|
||||
.WithClientId(qos.ToString())
|
||||
.WithCleanSession(false));
|
||||
|
||||
subscriber.UseApplicationMessageReceivedHandler(c =>
|
||||
{
|
||||
c.AutoAcknowledge = false;
|
||||
++count;
|
||||
Console.WriteLine("process");
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
await subscriber.SubscribeAsync("#", qos);
|
||||
|
||||
var pub = publisher.PublishAsync("a", null, qos);
|
||||
|
||||
await Task.Delay(100);
|
||||
await subscriber.DisconnectAsync();
|
||||
await subscriber.ReconnectAsync();
|
||||
await Task.Delay(100);
|
||||
|
||||
var res = await pub;
|
||||
|
||||
Assert.AreEqual(MqttClientPublishReasonCode.Success, res.ReasonCode);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
|
||||
async Task<IMqttClient> TryConnect(TestEnvironment testEnvironment, MqttClientOptionsBuilder options)
|
||||
{
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
if (context.ApplicationMessage.Topic == "not_allowed_topic")
|
||||
{
|
||||
context.AcceptPublish = false;
|
||||
context.ProcessPublish = false;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user