From 506873ed5bb241bfd68ba21c559f7fbc737b8dfd Mon Sep 17 00:00:00 2001 From: Berni Date: Thu, 31 Jan 2019 15:50:01 +0100 Subject: [PATCH] fix logger logId not used in ManagedMqttClient --- .../MqttFactoryExtensions.cs | 4 +- Tests/MQTTnet.Core.Tests/MqttFactoryTests.cs | 79 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 Tests/MQTTnet.Core.Tests/MqttFactoryTests.cs diff --git a/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs b/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs index d2ad020..7f400c5 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs @@ -18,7 +18,7 @@ namespace MQTTnet.Extensions.ManagedClient if (factory == null) throw new ArgumentNullException(nameof(factory)); if (logger == null) throw new ArgumentNullException(nameof(logger)); - return new ManagedMqttClient(factory.CreateMqttClient(), logger.CreateChildLogger()); + return new ManagedMqttClient(factory.CreateMqttClient(logger), logger.CreateChildLogger()); } } -} +} \ No newline at end of file diff --git a/Tests/MQTTnet.Core.Tests/MqttFactoryTests.cs b/Tests/MQTTnet.Core.Tests/MqttFactoryTests.cs new file mode 100644 index 0000000..42f735f --- /dev/null +++ b/Tests/MQTTnet.Core.Tests/MqttFactoryTests.cs @@ -0,0 +1,79 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Diagnostics; +using MQTTnet.Extensions.ManagedClient; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MQTTnet.Tests +{ + [TestClass] + public class MqttFactoryTests + { + [TestMethod] + public async Task Create_Managed_Client_With_Logger() + { + var factory = new MqttFactory(); + + //This test compares + //1. correct logID + string logId = "logId"; + bool invalidLogIdOccured = false; + + //2. if the total log calls are the same for global and local + int globalLogCount = 0; + int localLogCount = 0; + + MqttNetLogger logger = new MqttNetLogger(logId); + + //we have a theoretical bug here if a concurrent test is also logging + var globalLog = new EventHandler((s, e) => + { + if (logId != e.TraceMessage.LogId) + { + invalidLogIdOccured = true; + } + Interlocked.Increment(ref globalLogCount); + }); + + MqttNetGlobalLogger.LogMessagePublished += globalLog; + + logger.LogMessagePublished += (s, e) => + { + if (logId != e.TraceMessage.LogId) + { + invalidLogIdOccured = true; + } + Interlocked.Increment(ref localLogCount); + }; + + var managedClient = factory.CreateManagedMqttClient(logger); + try + { + var clientOptions = new ManagedMqttClientOptionsBuilder(); + + clientOptions.WithClientOptions(o => o.WithTcpServer("this_is_an_invalid_host").WithCommunicationTimeout(TimeSpan.FromSeconds(1))); + + //try connect to get some log entries + await managedClient.StartAsync(clientOptions.Build()); + + //wait at least connect timeout or we have some log messages + var tcs = new TaskCompletionSource(); + managedClient.ConnectingFailed += (s, e) => tcs.TrySetResult(null); + await Task.WhenAny(Task.Delay(managedClient.Options.ClientOptions.CommunicationTimeout), tcs.Task); + } + finally + { + MqttNetGlobalLogger.LogMessagePublished -= globalLog; + + await managedClient.StopAsync(); + } + + Assert.IsFalse(invalidLogIdOccured); + Assert.AreNotEqual(0, globalLogCount); + Assert.AreEqual(globalLogCount, localLogCount); + } + } +} \ No newline at end of file