mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-11-04 13:33:02 +08:00
v4.0.0 (#1357)
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Diagnostics;
|
||||
|
||||
namespace MQTTnet.Tests.Diagnostics
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class Logger_Tests : BaseTestClass
|
||||
{
|
||||
[TestMethod]
|
||||
public void Log_Without_Source()
|
||||
{
|
||||
var logger = new MqttNetEventLogger();
|
||||
|
||||
MqttNetLogMessage logMessage = null;
|
||||
logger.LogMessagePublished += (s, e) => { logMessage = e.LogMessage; };
|
||||
|
||||
logger.Publish(MqttNetLogLevel.Info, "SOURCE", "MESSAGE", new object[] { "ABC" }, new InvalidOperationException());
|
||||
|
||||
Assert.AreEqual(MqttNetLogLevel.Info, logMessage.Level);
|
||||
Assert.AreEqual("SOURCE", logMessage.Source);
|
||||
Assert.AreEqual("MESSAGE", logMessage.Message);
|
||||
Assert.AreEqual("InvalidOperationException", logMessage.Exception.GetType().Name);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Root_Log_Messages()
|
||||
{
|
||||
var logger = new MqttNetEventLogger();
|
||||
var childLogger = logger.WithSource("Source1");
|
||||
|
||||
var logMessagesCount = 0;
|
||||
|
||||
logger.LogMessagePublished += (s, e) => { logMessagesCount++; };
|
||||
|
||||
childLogger.Verbose("Verbose");
|
||||
childLogger.Info("Info");
|
||||
childLogger.Warning((Exception)null, "Warning");
|
||||
childLogger.Error(null, "Error");
|
||||
|
||||
Assert.AreEqual(4, logMessagesCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Use_Custom_Log_Id()
|
||||
{
|
||||
var logger = new MqttNetEventLogger("logId");
|
||||
var childLogger = logger.WithSource("Source1");
|
||||
|
||||
logger.LogMessagePublished += (s, e) =>
|
||||
{
|
||||
Assert.AreEqual("logId", e.LogMessage.LogId);
|
||||
Assert.AreEqual("Source1", e.LogMessage.Source);
|
||||
};
|
||||
|
||||
childLogger.Verbose("Verbose");
|
||||
childLogger.Info("Info");
|
||||
childLogger.Warning((Exception)null, "Warning");
|
||||
childLogger.Error(null, "Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Implementations;
|
||||
|
||||
namespace MQTTnet.Tests.Diagnostics
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class PacketInspection_Tests : BaseTestClass
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task Inspect_Client_Packets()
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
using (var mqttClient = testEnvironment.CreateClient())
|
||||
{
|
||||
var mqttClientOptions = testEnvironment.Factory.CreateClientOptionsBuilder()
|
||||
.WithClientId("CLIENT_ID") // Must be fixed.
|
||||
.WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
|
||||
.Build();
|
||||
|
||||
var packets = new List<string>();
|
||||
|
||||
mqttClient.InspectPackage += eventArgs =>
|
||||
{
|
||||
packets.Add(eventArgs.Direction + ":" + Convert.ToBase64String(eventArgs.Buffer));
|
||||
return PlatformAbstractionLayer.CompletedTask;
|
||||
};
|
||||
|
||||
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
|
||||
|
||||
Assert.AreEqual(2, packets.Count);
|
||||
Assert.AreEqual("Outbound:ECwABE1RVFQEAgAPACBJbnNwZWN0X0NsaWVudF9QYWNrZXRzX0NMSUVOVF9JRA==", packets[0]); // CONNECT
|
||||
Assert.AreEqual("Inbound:IAIAAA==", packets[1]); // CONNACK
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Diagnostics;
|
||||
|
||||
namespace MQTTnet.Tests.Diagnostics
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class SourceLogger_Tests : BaseTestClass
|
||||
{
|
||||
[TestMethod]
|
||||
public void Log_With_Source()
|
||||
{
|
||||
MqttNetLogMessage logMessage = null;
|
||||
|
||||
var logger = new MqttNetEventLogger();
|
||||
logger.LogMessagePublished += (s, e) =>
|
||||
{
|
||||
logMessage = e.LogMessage;
|
||||
};
|
||||
|
||||
var sourceLogger = logger.WithSource("The_Source");
|
||||
sourceLogger.Info("MESSAGE", (object)null, (object)null);
|
||||
|
||||
Assert.AreEqual("The_Source", logMessage.Source);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user