From 1acc44047798b09d30a58e6054966949d7608f81 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sun, 14 Oct 2018 18:34:24 +0200 Subject: [PATCH 1/7] WIP: Refactoring the AttributeToConfigConverter (just naming) --- .../Bindings/IMqttConfigurationParser.cs | 9 ++++ ...onverter.cs => MqttConfigurationParser.cs} | 42 ++++++++--------- .../MqttTriggerAttributeBindingProvider.cs | 1 - .../Config/IMqttConnectionFactory.cs | 2 +- .../Config/MqttConnectionFactory.cs | 18 +++----- .../Config/MqttExtensionConfigProvider.cs | 3 +- .../Config/MqttWebJobsBuilderExtensions.cs | 2 + .../IRquireMqttConnection.cs | 11 ----- .../MqttAttribute.cs | 9 +--- .../MqttBaseAttribute.cs | 20 ++++++++ .../MqttTriggerAttribute.cs | 10 +--- .../{ => Properties}/AssemblyInfo.cs | 0 .../{ => Properties}/GlobalSuppressions.cs | 0 .../AttributeToConfigConverterTests.cs | 46 +++++++++---------- ...Azure.WebJobs.Extensions.Mqtt.Tests.csproj | 2 +- .../Helpers/Logging/TestLoggerProvider.cs | 3 +- 16 files changed, 87 insertions(+), 91 deletions(-) create mode 100644 src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/IMqttConfigurationParser.cs rename src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/{AttributeToConfigConverter.cs => MqttConfigurationParser.cs} (66%) delete mode 100644 src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/IRquireMqttConnection.cs create mode 100644 src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttBaseAttribute.cs rename src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/{ => Properties}/AssemblyInfo.cs (100%) rename src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/{ => Properties}/GlobalSuppressions.cs (100%) diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/IMqttConfigurationParser.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/IMqttConfigurationParser.cs new file mode 100644 index 0000000..60dc464 --- /dev/null +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/IMqttConfigurationParser.cs @@ -0,0 +1,9 @@ +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config; + +namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings +{ + public interface IMqttConfigurationParser + { + MqttConfiguration Parse(MqttBaseAttribute mqttAttribute); + } +} diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/AttributeToConfigConverter.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs similarity index 66% rename from src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/AttributeToConfigConverter.cs rename to src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs index 3a58cb2..8f786ed 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/AttributeToConfigConverter.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs @@ -1,4 +1,4 @@ -using System; +using System; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; @@ -8,53 +8,51 @@ namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings { /// - /// Converts a to a . + /// Parses a to a . /// - public class AttributeToConfigConverter + public class MqttConfigurationParser : IMqttConfigurationParser { private const string DefaultAppsettingsKeyForConnectionString = "MqttConnection"; - private readonly TimeSpan _detaultReconnectTime = TimeSpan.FromSeconds(5); - private readonly IRquireMqttConnection _mqttTriggerAttribute; + private readonly TimeSpan _defaultReconnectTime = TimeSpan.FromSeconds(5); private readonly INameResolver _nameResolver; private readonly ILogger _logger; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - /// The trigger attribute. /// The name resolver. /// The logger. - public AttributeToConfigConverter(IRquireMqttConnection source, INameResolver nameResolver, ILogger logger) - { - _mqttTriggerAttribute = source; + public MqttConfigurationParser(INameResolver nameResolver, ILogger logger) + { _nameResolver = nameResolver; _logger = logger; } /// - /// Gets the MQTT configuration from this attribute. + /// Gets the MQTT configuration from the given attribute. /// + /// The attribute to parse from. /// /// The configuration. /// - public MqttConfiguration GetMqttConfiguration() + public MqttConfiguration Parse(MqttBaseAttribute mqttAttribute) { - return _mqttTriggerAttribute.MqttConfigCreatorType == null - ? GetConfigurationViaAttributeValues() - : GetConfigurationViaCustomConfigCreator(); + return mqttAttribute.MqttConfigCreatorType == null + ? GetConfigurationViaAttributeValues(mqttAttribute) + : GetConfigurationViaCustomConfigCreator(mqttAttribute); } - private MqttConfiguration GetConfigurationViaAttributeValues() + private MqttConfiguration GetConfigurationViaAttributeValues(MqttBaseAttribute mqttAttribute) { - var name = _mqttTriggerAttribute.ConnectionString ?? DefaultAppsettingsKeyForConnectionString; - var connectionString = _nameResolver.Resolve(_mqttTriggerAttribute.ConnectionString) ?? _nameResolver.Resolve(DefaultAppsettingsKeyForConnectionString); + var name = mqttAttribute.ConnectionString ?? DefaultAppsettingsKeyForConnectionString; + var connectionString = _nameResolver.Resolve(mqttAttribute.ConnectionString) ?? _nameResolver.Resolve(DefaultAppsettingsKeyForConnectionString); var mqttConnectionString = new MqttConnectionString(connectionString); var mqttClientOptions = GetMqttClientOptions(mqttConnectionString); var managedMqttClientOptions = new ManagedMqttClientOptionsBuilder() - .WithAutoReconnectDelay(_detaultReconnectTime) + .WithAutoReconnectDelay(_defaultReconnectTime) .WithClientOptions(mqttClientOptions) .Build(); @@ -82,17 +80,17 @@ private IMqttClientOptions GetMqttClientOptions(MqttConnectionString connectionS return mqttClientOptionsBuilder.Build(); } - private MqttConfiguration GetConfigurationViaCustomConfigCreator() + private MqttConfiguration GetConfigurationViaCustomConfigCreator(MqttBaseAttribute mqttAttribute) { CustomMqttConfig customConfig; try { - var customConfigCreator = (ICreateMqttConfig)Activator.CreateInstance(_mqttTriggerAttribute.MqttConfigCreatorType); + var customConfigCreator = (ICreateMqttConfig)Activator.CreateInstance(mqttAttribute.MqttConfigCreatorType); customConfig = customConfigCreator.Create(_nameResolver, _logger); } catch (Exception ex) { - throw new InvalidCustomConfigCreatorException($"Unexpected exception while getting creating a config via type {_mqttTriggerAttribute.MqttConfigCreatorType.FullName}", ex); + throw new InvalidCustomConfigCreatorException($"Unexpected exception while getting creating a config via type {mqttAttribute.MqttConfigCreatorType.FullName}", ex); } return new MqttConfiguration(customConfig.Name, customConfig.Options); diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs index b0b2d74..b4e60c3 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Messaging; -using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host.Triggers; using Microsoft.Azure.WebJobs.Logging; using Microsoft.Extensions.Logging; diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/IMqttConnectionFactory.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/IMqttConnectionFactory.cs index 40b6f33..982cedc 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/IMqttConnectionFactory.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/IMqttConnectionFactory.cs @@ -7,6 +7,6 @@ public interface IMqttConnectionFactory { Task DisconnectAll(); - MqttConnection GetMqttConnection(IRquireMqttConnection attribute); + MqttConnection GetMqttConnection(MqttBaseAttribute attribute); } } diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs index 5a2415a..ce59ae8 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Listeners; -using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Logging; using Microsoft.Extensions.Logging; @@ -14,26 +13,21 @@ public class MqttConnectionFactory : IMqttConnectionFactory { private readonly ILogger _logger; private readonly IManagedMqttClientFactory _mqttFactory; - private readonly INameResolver _nameResolver; + private readonly IMqttConfigurationParser _mqttConfigurationParser; private readonly ConcurrentDictionary _mqttConnections = new ConcurrentDictionary(); - public MqttConnectionFactory(ILoggerFactory loggerFactory, IManagedMqttClientFactory mqttFactory, INameResolver nameResolver) + public MqttConnectionFactory(ILoggerFactory loggerFactory, IManagedMqttClientFactory mqttFactory, IMqttConfigurationParser mqttConfigurationParser) { _mqttFactory = mqttFactory; - _nameResolver = nameResolver; + _mqttConfigurationParser = mqttConfigurationParser; _logger = loggerFactory.CreateLogger(LogCategories.CreateTriggerCategory("Mqtt")); } - public MqttConnectionFactory() + public MqttConnection GetMqttConnection(MqttBaseAttribute attribute) { - } - - public MqttConnection GetMqttConnection(IRquireMqttConnection attribute) - { - var attributeToConfigConverter = new AttributeToConfigConverter(attribute, _nameResolver, _logger); - var mqttConfiguration = attributeToConfigConverter.GetMqttConfiguration(); - if (_mqttConnections.ContainsKey(mqttConfiguration.Name) && attribute is MqttTriggerAttribute) + var mqttConfiguration = _mqttConfigurationParser.Parse(attribute); + if (_mqttConnections.ContainsKey(mqttConfiguration.Name)) { throw new InvalidOperationException($"Error setting up listener for this attribute. Connectionstring '{mqttConfiguration.Name}' is already used by another Trigger. Connections can only be reused for output bindings. Each trigger needs it own connectionstring"); } diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttExtensionConfigProvider.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttExtensionConfigProvider.cs index 1a6dcc8..35f67cb 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttExtensionConfigProvider.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttExtensionConfigProvider.cs @@ -1,8 +1,7 @@ using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; -using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Description; using Microsoft.Azure.WebJobs.Host.Config; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config { diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttWebJobsBuilderExtensions.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttWebJobsBuilderExtensions.cs index 8e2bc96..52eb0ce 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttWebJobsBuilderExtensions.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttWebJobsBuilderExtensions.cs @@ -1,4 +1,5 @@ using System; +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Listeners; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.DependencyInjection; @@ -25,6 +26,7 @@ public static IWebJobsBuilder AddMqtt(this IWebJobsBuilder builder) builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddSingleton(); + builder.Services.AddTransient(); builder.AddExtension(); return builder; diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/IRquireMqttConnection.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/IRquireMqttConnection.cs deleted file mode 100644 index 1c6a941..0000000 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/IRquireMqttConnection.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt -{ - public interface IRquireMqttConnection - { - string ConnectionString { get; set; } - - Type MqttConfigCreatorType { get; } - } -} diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttAttribute.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttAttribute.cs index 2303c86..7d3b6c9 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttAttribute.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttAttribute.cs @@ -5,19 +5,14 @@ namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt { [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)] [Binding] - public class MqttAttribute : Attribute, IRquireMqttConnection + public class MqttAttribute : MqttBaseAttribute { public MqttAttribute() { } - public MqttAttribute(Type mqttConfigCreatorType) + public MqttAttribute(Type mqttConfigCreatorType) : base(mqttConfigCreatorType) { - MqttConfigCreatorType = mqttConfigCreatorType; } - - public string ConnectionString { get; set; } - - public Type MqttConfigCreatorType { get; } } } diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttBaseAttribute.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttBaseAttribute.cs new file mode 100644 index 0000000..f54f77d --- /dev/null +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttBaseAttribute.cs @@ -0,0 +1,20 @@ +using System; + +namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt +{ + public abstract class MqttBaseAttribute : Attribute + { + protected MqttBaseAttribute() + { + } + + protected MqttBaseAttribute(Type mqttConfigCreatorType) + { + MqttConfigCreatorType = mqttConfigCreatorType; + } + + public string ConnectionString { get; set; } + + public Type MqttConfigCreatorType { get; protected set; } + } +} diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs index 078ee30..bdba71b 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs @@ -5,24 +5,18 @@ namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt { [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)] [Binding] - public class MqttTriggerAttribute : Attribute, IRquireMqttConnection + public class MqttTriggerAttribute : MqttBaseAttribute { public MqttTriggerAttribute(params string[] topics) { Topics = topics; } - public MqttTriggerAttribute(Type mqttConfigCreatorType, params string[] topics) + public MqttTriggerAttribute(Type mqttConfigCreatorType, params string[] topics) : base(mqttConfigCreatorType) { - MqttConfigCreatorType = mqttConfigCreatorType; Topics = topics; } - public Type MqttConfigCreatorType { get; } - public string[] Topics { get; } - - [AppSetting] - public string ConnectionString { get; set; } } } diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/AssemblyInfo.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Properties/AssemblyInfo.cs similarity index 100% rename from src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/AssemblyInfo.cs rename to src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Properties/AssemblyInfo.cs diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/GlobalSuppressions.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Properties/GlobalSuppressions.cs similarity index 100% rename from src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/GlobalSuppressions.cs rename to src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Properties/GlobalSuppressions.cs diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs index 58428bc..fd58947 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs @@ -1,16 +1,16 @@ +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config; +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.Util.Helpers; +using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using Moq; using MQTTnet.Extensions.ManagedClient; -using Xunit; -using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; using System; -using Microsoft.Azure.WebJobs; -using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.Util.Helpers; +using Xunit; namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests { - public class AttributeToConfigConverterTests + public class MqttConfigurationParserTests { private readonly Mock _mockLogger = new Mock(); private readonly MockNameResolver _resolver = new MockNameResolver(); @@ -24,13 +24,12 @@ public void ValidConfigurationIsMappedCorrect() ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId=TestClientId" }; - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act - var result = attributeToConfigConverter.GetMqttConfiguration(); + var result = mqttConfigurationParser.Parse(mqttTriggerAttribute); // Assert - //Assert.Equal(mqttTriggerAttribute.Topics, result.Topics.Select(x => x.Topic)); Assert.Equal("TestClientId", result.Options.ClientOptions.ClientId); } @@ -43,10 +42,10 @@ public void InvalidPortThrowsException() ConnectionString = "Server=ServerName;Port=ByeWorld;Username=UserName;Password=Password" }; - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act & Assert - var ex = Assert.Throws(() => attributeToConfigConverter.GetMqttConfiguration()); + Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); } [Fact] @@ -58,10 +57,10 @@ public void NoClientIdGuidBasedClientIdIsGenerated() ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId=" }; - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act - var result = attributeToConfigConverter.GetMqttConfiguration(); + var result = mqttConfigurationParser.Parse(mqttTriggerAttribute); // Assert Assert.NotNull(result.Options.ClientOptions.ClientId); @@ -77,10 +76,10 @@ public void NoServernameProvidedResultsInException() ConnectionString = "Server=;Port=1883;Username=UserName;Password=Password;ClientId=" }; - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act & Assert - var ex = Assert.Throws(() => attributeToConfigConverter.GetMqttConfiguration()); + Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); } @@ -90,26 +89,25 @@ public void CustomConfigProviderIsInvoked() // Arrange var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(TestMqttConfigProvider)); - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act - var result = attributeToConfigConverter.GetMqttConfiguration(); + var result = mqttConfigurationParser.Parse(mqttTriggerAttribute); // Assert Assert.NotNull(result); - //Assert.Contains(result.Topics.Select(x => x.Topic), (x) => x == "Test"); } private class TestMqttConfigProvider : ICreateMqttConfig { public CustomMqttConfig Create(INameResolver nameResolver, ILogger logger) { - return new TestMqttConfig("",new ManagedMqttClientOptions()); + return new TestMqttConfig("", new ManagedMqttClientOptions()); } } private class TestMqttConfig : CustomMqttConfig - { + { public override IManagedMqttClientOptions Options { get; } public override string Name { get; } @@ -117,7 +115,7 @@ private class TestMqttConfig : CustomMqttConfig public TestMqttConfig(string name, IManagedMqttClientOptions options) { Name = name; - Options = options; + Options = options; } } @@ -127,10 +125,10 @@ public void InvalidCustomConfigCreatorThrowsException() // Arrange var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(string)); - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act & Assert - var ex = Assert.Throws(() => attributeToConfigConverter.GetMqttConfiguration()); + Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); } [Fact] @@ -139,10 +137,10 @@ public void BrokenCustomConfigCreatorThrowsException() // Arrange var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(BrokenTestMqttConfigProvider)); - var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); // Act & Assert - var ex = Assert.Throws(() => attributeToConfigConverter.GetMqttConfiguration()); + Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); } private class BrokenTestMqttConfigProvider : ICreateMqttConfig diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj index e669339..e4c2f29 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj @@ -59,7 +59,7 @@ - + diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/Logging/TestLoggerProvider.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/Logging/TestLoggerProvider.cs index 84a43d3..b2bd17c 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/Logging/TestLoggerProvider.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/Logging/TestLoggerProvider.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; using Microsoft.Extensions.Logging; namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.Helpers.Logging From 6de091b44bcde3f4e4909797977740d1ed83f6e4 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sun, 21 Oct 2018 16:00:29 +0200 Subject: [PATCH 2/7] Fixing the tests --- .../Bindings/MqttConfigurationParser.cs | 7 +-- .../MqttTriggerAttributeBindingProvider.cs | 17 +++++-- .../Config/MqttConnectionFactory.cs | 47 ++++++++++++++----- .../AttributeToConfigConverterTests.cs | 22 +++++---- .../EndToEnd/OutputBindingTests.cs | 4 +- 5 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs index 8f786ed..d1becb5 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs @@ -1,6 +1,7 @@ using System; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config; using Microsoft.Azure.WebJobs; +using Microsoft.Azure.WebJobs.Logging; using Microsoft.Extensions.Logging; using MQTTnet.Client; using MQTTnet.Extensions.ManagedClient; @@ -22,11 +23,11 @@ public class MqttConfigurationParser : IMqttConfigurationParser /// Initializes a new instance of the class. /// /// The name resolver. - /// The logger. - public MqttConfigurationParser(INameResolver nameResolver, ILogger logger) + /// The logger factory. + public MqttConfigurationParser(INameResolver nameResolver, ILoggerFactory loggerFactory) { _nameResolver = nameResolver; - _logger = logger; + _logger = loggerFactory.CreateLogger(LogCategories.CreateTriggerCategory("Mqtt")); } /// diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs index b4e60c3..c482b31 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs @@ -44,12 +44,19 @@ public Task TryCreateAsync(TriggerBindingProviderContext contex } _logger.LogDebug($"Creating binding for parameter '{context.Parameter.Name}'"); + try + { + var mqttTriggerBinding = GetMqttTriggerBinding(context.Parameter, mqttTriggerAttribute); - ITriggerBinding mqttTriggerBinding = GetMqttTriggerBinding(context.Parameter, mqttTriggerAttribute); - - _logger.LogDebug($"Succesfully created binding for parameter '{context.Parameter.Name}'"); + _logger.LogDebug($"Succesfully created binding for parameter '{context.Parameter.Name}'"); - return Task.FromResult(mqttTriggerBinding); + return Task.FromResult(mqttTriggerBinding); + } + catch (Exception ex) + { + _logger.LogError(ex, $"Unhandled exception while binding trigger '{context.Parameter.Name}'"); + throw; + } } private static MqttTriggerAttribute GetMqttTriggerAttribute(ParameterInfo parameter) @@ -69,7 +76,7 @@ private static MqttTriggerAttribute GetMqttTriggerAttribute(ParameterInfo parame return mqttTriggerAttribute; } - private MqttTriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute) + private ITriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute) { TopicFilter[] topics; var mqttConnection = _connectionFactory.GetMqttConnection(mqttTriggerAttribute); diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs index ce59ae8..47b90cb 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs @@ -1,11 +1,11 @@ -using System; -using System.Collections.Concurrent; -using System.Linq; -using System.Threading.Tasks; -using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Listeners; using Microsoft.Azure.WebJobs.Logging; using Microsoft.Extensions.Logging; +using System; +using System.Collections.Concurrent; +using System.Linq; +using System.Threading.Tasks; namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config { @@ -15,7 +15,7 @@ public class MqttConnectionFactory : IMqttConnectionFactory private readonly IManagedMqttClientFactory _mqttFactory; private readonly IMqttConfigurationParser _mqttConfigurationParser; - private readonly ConcurrentDictionary _mqttConnections = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _mqttConnections = new ConcurrentDictionary(); public MqttConnectionFactory(ILoggerFactory loggerFactory, IManagedMqttClientFactory mqttFactory, IMqttConfigurationParser mqttConfigurationParser) { @@ -27,26 +27,47 @@ public MqttConnectionFactory(ILoggerFactory loggerFactory, IManagedMqttClientFac public MqttConnection GetMqttConnection(MqttBaseAttribute attribute) { var mqttConfiguration = _mqttConfigurationParser.Parse(attribute); - if (_mqttConnections.ContainsKey(mqttConfiguration.Name)) + if (_mqttConnections.ContainsKey(mqttConfiguration.Name) && attribute is MqttTriggerAttribute) { - throw new InvalidOperationException($"Error setting up listener for this attribute. Connectionstring '{mqttConfiguration.Name}' is already used by another Trigger. Connections can only be reused for output bindings. Each trigger needs it own connectionstring"); + if (_mqttConnections[mqttConfiguration.Name].UsedByTrigger) + { + throw new InvalidOperationException($"Error setting up listener for this attribute. Connectionstring '{mqttConfiguration.Name}' is already used by another Trigger. Connections can only be reused for output bindings. Each trigger needs it own connectionstring"); + } + else + { + _mqttConnections[mqttConfiguration.Name].UsedByTrigger = true; + } } - var connection = _mqttConnections.GetOrAdd(mqttConfiguration.Name, (c) => new MqttConnection(_mqttFactory, mqttConfiguration, _logger)); - return connection; + var connection = _mqttConnections.GetOrAdd(mqttConfiguration.Name, (c) => new MqttConnectionEntry(new MqttConnection(_mqttFactory, mqttConfiguration, _logger))); + return connection.MqttConnection; } internal bool AllConnectionsConnected() { - return _mqttConnections.All(x => x.Value.ConnectionState == ConnectionState.Connected); + return _mqttConnections.All(x => x.Value.MqttConnection.ConnectionState == ConnectionState.Connected); } public async Task DisconnectAll() { foreach (var connection in _mqttConnections) { - await connection.Value.StopAsync().ConfigureAwait(false); - connection.Value.Dispose(); + await connection.Value.MqttConnection.StopAsync().ConfigureAwait(false); + connection.Value.MqttConnection.Dispose(); + } + } + + private class MqttConnectionEntry + { + public MqttConnectionEntry(MqttConnection mqttConnection) + { + MqttConnection = mqttConnection; + UsedByTrigger = false; } + + public MqttConnection MqttConnection { get; set; } + + public bool UsedByTrigger { get; set; } } + } } diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs index fd58947..633e06b 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/AttributeToConfigConverterTests.cs @@ -12,9 +12,15 @@ namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests { public class MqttConfigurationParserTests { - private readonly Mock _mockLogger = new Mock(); + private readonly Mock _mockLoggerFactory; + private readonly MockNameResolver _resolver = new MockNameResolver(); + public MqttConfigurationParserTests() + { + _mockLoggerFactory = new Mock(); + _mockLoggerFactory.Setup(x => x.CreateLogger(It.IsAny())).Returns(new Mock().Object); + } [Fact] public void ValidConfigurationIsMappedCorrect() { @@ -24,7 +30,7 @@ public void ValidConfigurationIsMappedCorrect() ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId=TestClientId" }; - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act var result = mqttConfigurationParser.Parse(mqttTriggerAttribute); @@ -42,7 +48,7 @@ public void InvalidPortThrowsException() ConnectionString = "Server=ServerName;Port=ByeWorld;Username=UserName;Password=Password" }; - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act & Assert Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); @@ -57,7 +63,7 @@ public void NoClientIdGuidBasedClientIdIsGenerated() ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId=" }; - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act var result = mqttConfigurationParser.Parse(mqttTriggerAttribute); @@ -76,7 +82,7 @@ public void NoServernameProvidedResultsInException() ConnectionString = "Server=;Port=1883;Username=UserName;Password=Password;ClientId=" }; - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act & Assert Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); @@ -89,7 +95,7 @@ public void CustomConfigProviderIsInvoked() // Arrange var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(TestMqttConfigProvider)); - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act var result = mqttConfigurationParser.Parse(mqttTriggerAttribute); @@ -125,7 +131,7 @@ public void InvalidCustomConfigCreatorThrowsException() // Arrange var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(string)); - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act & Assert Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); @@ -137,7 +143,7 @@ public void BrokenCustomConfigCreatorThrowsException() // Arrange var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(BrokenTestMqttConfigProvider)); - var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLogger.Object); + var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object); // Act & Assert Assert.Throws(() => mqttConfigurationParser.Parse(mqttTriggerAttribute)); diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/OutputBindingTests.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/OutputBindingTests.cs index 8f4c2f6..a7ea880 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/OutputBindingTests.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/OutputBindingTests.cs @@ -48,7 +48,7 @@ public static void Testert([Mqtt]out IMqttMessage mqttMessage) [Fact] public async Task TriggerAndOutputReuseConnection() { - var mqttApplicationMessages = new List(); ; + var mqttApplicationMessages = new List(); var counnections = 0; var options = new MqttServerOptionsBuilder() .WithConnectionValidator(x => @@ -69,7 +69,7 @@ public async Task TriggerAndOutputReuseConnection() await mqttServer.PublishAsync(DefaultMessage); - await WaitFor(() => TriggerAndOutputWithSameConnectionTestFunction.CallCount >= 2); + await WaitFor(() => TriggerAndOutputWithSameConnectionTestFunction.CallCount >= 1); } Assert.Equal(1, TriggerAndOutputWithSameConnectionTestFunction.CallCount); From d2abe191f4c425180e483b1b83e0e71ffe4bfe42 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sun, 21 Oct 2018 16:24:19 +0200 Subject: [PATCH 3/7] Updated packages & added support for SourceLink --- ...eOnline.Azure.WebJobs.Extensions.Mqtt.csproj | 14 ++++++++------ src/ExampleFunctions/ExampleFunctions.csproj | 4 ++-- ...e.Azure.WebJobs.Extensions.Mqtt.Tests.csproj | 17 ++++++++++------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj index 2ec78db..9d4bad6 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj @@ -9,8 +9,9 @@ https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt git + true https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt/blob/master/LICENSE - 1.0.0 + 1.1.0 azure functions mqtt webjobs trigger binding function webjob CaseOnline.Azure.WebJobs.Extensions.Mqtt @@ -37,21 +38,22 @@ For more information, please visit https://github.com/keesschollaart81/CaseOnlin - + - + all runtime; build; native; contentfiles; analyzers - + All - + All - + + \ No newline at end of file diff --git a/src/ExampleFunctions/ExampleFunctions.csproj b/src/ExampleFunctions/ExampleFunctions.csproj index 251bea2..b8141ed 100644 --- a/src/ExampleFunctions/ExampleFunctions.csproj +++ b/src/ExampleFunctions/ExampleFunctions.csproj @@ -4,8 +4,8 @@ v2 - - + + diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj index e4c2f29..2bce116 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj @@ -52,14 +52,17 @@ - - - - + + + + - - - + + + all + runtime; build; native; contentfiles; analyzers + + From a809591893910f7d5d1eeceaf69baf76c07499d6 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sun, 21 Oct 2018 16:35:17 +0200 Subject: [PATCH 4/7] TreatWarningsAsErrors --- .../CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj | 6 ++++-- .../Config/MqttConnectionFactory.cs | 11 +++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj index 9d4bad6..a8b3310 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj @@ -9,7 +9,9 @@ https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt git + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb true + true https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt/blob/master/LICENSE 1.1.0 @@ -28,7 +30,7 @@ For more information, please visit https://github.com/keesschollaart81/CaseOnlin https://raw.githubusercontent.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt/master/logo.png true analyzers.ruleset - IOperation + True Full @@ -54,6 +56,6 @@ For more information, please visit https://github.com/keesschollaart81/CaseOnlin - + \ No newline at end of file diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs index 47b90cb..e8b13bd 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs @@ -1,11 +1,11 @@ -using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; -using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Listeners; -using Microsoft.Azure.WebJobs.Logging; -using Microsoft.Extensions.Logging; -using System; +using System; using System.Collections.Concurrent; using System.Linq; using System.Threading.Tasks; +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Bindings; +using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Listeners; +using Microsoft.Azure.WebJobs.Logging; +using Microsoft.Extensions.Logging; namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config { @@ -68,6 +68,5 @@ public MqttConnectionEntry(MqttConnection mqttConnection) public bool UsedByTrigger { get; set; } } - } } From b61240c49523b4f49739ca35f46b01be12bff193 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sat, 3 Nov 2018 13:40:54 +0100 Subject: [PATCH 5/7] Getting the tests to work? --- .../xunit.runner.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/xunit.runner.json b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/xunit.runner.json index af4a997..bf303d6 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/xunit.runner.json +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/xunit.runner.json @@ -1,4 +1,5 @@ { "parallelizeTestCollections": false, - "maxParallelThreads": 1 + "maxParallelThreads": 1, + "parallelizeAssembly": false } \ No newline at end of file From 19e5813f39f8f6d554238c13d0792d3a05207626 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sat, 3 Nov 2018 14:44:01 +0100 Subject: [PATCH 6/7] Updated some dependecies --- .../CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj | 2 +- .../CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj index a8b3310..5eb047f 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj @@ -42,7 +42,7 @@ For more information, please visit https://github.com/keesschollaart81/CaseOnlin - + all runtime; build; native; contentfiles; analyzers diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj index 2bce116..d699d6a 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj @@ -57,8 +57,8 @@ - - + + all runtime; build; native; contentfiles; analyzers From 2f11980b91960fe5cdc9b599a25698c465624424 Mon Sep 17 00:00:00 2001 From: Kees Schollaart Date: Sat, 3 Nov 2018 15:29:46 +0100 Subject: [PATCH 7/7] Fixing connection reuse in tests --- .../Config/MqttConnectionFactory.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs index e8b13bd..feb07b5 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs @@ -54,6 +54,7 @@ public async Task DisconnectAll() await connection.Value.MqttConnection.StopAsync().ConfigureAwait(false); connection.Value.MqttConnection.Dispose(); } + _mqttConnections.Clear(); } private class MqttConnectionEntry