Skip to content

Commit

Permalink
Add AspNetCoreTestEnvironment to test environments.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 6, 2024
1 parent 8a3624d commit 06f03cb
Show file tree
Hide file tree
Showing 44 changed files with 689 additions and 263 deletions.
14 changes: 10 additions & 4 deletions Source/MQTTnet.AspnetCore/Internal/AspNetCoreMqttNetLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ public AspNetCoreMqttNetLogger(

public void Publish(MqttNetLogLevel logLevel, string? source, string? message, object[]? parameters, Exception? exception)
{
var categoryName = $"{_loggerOptions.CategoryNamePrefix}{source}";
var logger = _loggerFactory.CreateLogger(categoryName);
var level = _loggerOptions.LogLevelConverter(logLevel);
logger.Log(level, exception, message, parameters ?? []);
try
{
var categoryName = $"{_loggerOptions.CategoryNamePrefix}{source}";
var logger = _loggerFactory.CreateLogger(categoryName);
var level = _loggerOptions.LogLevelConverter(logLevel);
logger.Log(level, exception, message, parameters ?? []);
}
catch (ObjectDisposedException)
{
}
}
}
}
2 changes: 1 addition & 1 deletion Source/MQTTnet.AspnetCore/Internal/MqttChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public virtual void Dispose()
{
if (!buffer.IsEmpty)
{
if (PacketFormatterAdapter.TryDecode(buffer, out var packet, out consumed, out observed, out var received))
if (PacketFormatterAdapter.TryDecode(buffer,_packetInspector, out var packet, out consumed, out observed, out var received))
{
BytesReceived += received;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static class MqttPacketFormatterAdapterExtensions
public static bool TryDecode(
this MqttPacketFormatterAdapter formatter,
in ReadOnlySequence<byte> input,
MqttPacketInspector? packetInspector,
[MaybeNullWhen(false)] out MqttPacket packet,
out SequencePosition consumed,
out SequencePosition observed,
Expand Down Expand Up @@ -51,6 +52,12 @@ public static bool TryDecode(
var bodySlice = copy.Slice(0, bodyLength);
var bodySegment = GetArraySegment(ref bodySlice);

if (packetInspector != null)
{
packetInspector.FillReceiveBuffer(input.Slice(input.Start, headerLength).ToArray());
packetInspector.FillReceiveBuffer(bodySegment.ToArray());
}

var receivedMqttPacket = new ReceivedMqttPacket(fixedHeader, bodySegment, headerLength + bodyLength);
if (formatter.ProtocolVersion == MqttProtocolVersion.Unknown)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/MQTTnet.Benchmarks/ReaderExtensionsBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task After()
{
if (!buffer.IsEmpty)
{
if (MqttPacketFormatterAdapterExtensions.TryDecode(mqttPacketFormatter, buffer, out var packet, out consumed, out observed, out var received))
if (MqttPacketFormatterAdapterExtensions.TryDecode(mqttPacketFormatter, buffer, null, out var packet, out consumed, out observed, out var received))
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ public void TestTryDeserialize()
var read = 0;

part = sequence.Slice(sequence.Start, 0); // empty message should fail
var result = serializer.TryDecode(part, out _, out consumed, out observed, out read);
var result = serializer.TryDecode(part,null, out _, out consumed, out observed, out read);
Assert.IsFalse(result);

part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
result = serializer.TryDecode(part, out _, out consumed, out observed, out read);
result = serializer.TryDecode(part, null, out _, out consumed, out observed, out read);
Assert.IsFalse(result);

part = sequence.Slice(sequence.Start, 4); // partial body should fail
result = serializer.TryDecode(part, out _, out consumed, out observed, out read);
result = serializer.TryDecode(part, null, out _, out consumed, out observed, out read);
Assert.IsFalse(result);

part = sequence; // complete msg should work
result = serializer.TryDecode(part, out _, out consumed, out observed, out read);
result = serializer.TryDecode(part, null, out _, out consumed, out observed, out read);
Assert.IsTrue(result);
}
}
24 changes: 19 additions & 5 deletions Source/MQTTnet.Tests/BaseTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@
// 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.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Formatter;
using MQTTnet.Tests.Mockups;
using System;
using System.Threading.Tasks;

namespace MQTTnet.Tests
{
public abstract class BaseTestClass
{
public TestContext TestContext { get; set; }

protected TestEnvironment CreateTestEnvironment(MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)

protected TestEnvironmentCollection CreateTestEnvironment(MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
{
var mqttnet = new TestEnvironment(TestContext, protocolVersion);
return new TestEnvironmentCollection(mqttnet);
}

protected TestEnvironmentCollection CreateAspNetCoreTestEnvironment(MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
{
var aspnetcore = new AspNetCoreTestEnvironment(TestContext, protocolVersion);
return new TestEnvironmentCollection(aspnetcore);
}

protected TestEnvironmentCollection CreateMixedTestEnvironment(MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
{
return new TestEnvironment(TestContext, protocolVersion);
var mqttnet = new TestEnvironment(TestContext, protocolVersion);
var aspnetcore = new AspNetCoreTestEnvironment(TestContext, protocolVersion);
return new TestEnvironmentCollection(mqttnet, aspnetcore);
}

protected Task LongTestDelay()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public sealed class LowLevelMqttClient_Tests : BaseTestClass
[TestMethod]
public async Task Authenticate()
{
using (var testEnvironment = CreateTestEnvironment())
using var testEnvironments = CreateMixedTestEnvironment();
foreach (var testEnvironment in testEnvironments)
{
await testEnvironment.StartServer();

Expand All @@ -43,7 +44,8 @@ public async Task Authenticate()
[TestMethod]
public async Task Connect_And_Disconnect()
{
using (var testEnvironment = CreateTestEnvironment())
using var testEnvironments = CreateMixedTestEnvironment();
foreach (var testEnvironment in testEnvironments)
{
await testEnvironment.StartServer();

Expand Down Expand Up @@ -78,7 +80,8 @@ public async Task Connect_To_Wrong_Host()
[TestMethod]
public async Task Loose_Connection()
{
using (var testEnvironment = CreateTestEnvironment())
using var testEnvironments = CreateTestEnvironment();
foreach (var testEnvironment in testEnvironments)
{
testEnvironment.IgnoreServerLogErrors = true;

Expand Down Expand Up @@ -116,7 +119,8 @@ public async Task Loose_Connection()
[TestMethod]
public async Task Maintain_IsConnected_Property()
{
using (var testEnvironment = CreateTestEnvironment())
using var testEnvironments = CreateMixedTestEnvironment();
foreach (var testEnvironment in testEnvironments)
{
testEnvironment.IgnoreServerLogErrors = true;

Expand Down Expand Up @@ -161,7 +165,8 @@ public async Task Maintain_IsConnected_Property()
[TestMethod]
public async Task Subscribe()
{
using (var testEnvironment = CreateTestEnvironment())
using var testEnvironments = CreateMixedTestEnvironment();
foreach (var testEnvironment in testEnvironments)
{
await testEnvironment.StartServer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public async Task ConnectTimeout_Throws_Exception()
[TestMethod]
public async Task Disconnect_Clean()
{
using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
using var testEnvironments = CreateMixedTestEnvironment(MqttProtocolVersion.V500);
foreach (var testEnvironment in testEnvironments)
{
var server = await testEnvironment.StartServer();

Expand Down Expand Up @@ -110,7 +111,8 @@ public async Task Disconnect_Clean()
[TestMethod]
public async Task Disconnect_Clean_With_Custom_Reason()
{
using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
using var testEnvironments = CreateMixedTestEnvironment(MqttProtocolVersion.V500);
foreach (var testEnvironment in testEnvironments)
{
var server = await testEnvironment.StartServer();

Expand Down Expand Up @@ -138,7 +140,8 @@ public async Task Disconnect_Clean_With_Custom_Reason()
[TestMethod]
public async Task Disconnect_Clean_With_User_Properties()
{
using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
using var testEnvironments = CreateMixedTestEnvironment(MqttProtocolVersion.V500);
foreach (var testEnvironment in testEnvironments)
{
var server = await testEnvironment.StartServer();

Expand Down Expand Up @@ -169,7 +172,8 @@ public async Task Disconnect_Clean_With_User_Properties()
[TestMethod]
public async Task No_Unobserved_Exception()
{
using (var testEnvironment = CreateTestEnvironment())
using var testEnvironments = CreateMixedTestEnvironment();
foreach (var testEnvironment in testEnvironments)
{
testEnvironment.IgnoreClientLogErrors = true;

Expand Down Expand Up @@ -201,7 +205,8 @@ public async Task No_Unobserved_Exception()
[TestMethod]
public async Task Return_Non_Success()
{
using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
using var testEnvironments = CreateMixedTestEnvironment(MqttProtocolVersion.V500);
foreach (var testEnvironment in testEnvironments)
{
var server = await testEnvironment.StartServer();

Expand Down
Loading

0 comments on commit 06f03cb

Please sign in to comment.