示例#1
0
        public async Task ConnectAsync_CalledFromMultipleSenders_ClientConnectedOnce()
        {
            var sender1          = new object();
            var sender2          = new object();
            var mqttClientConfig = new MqttClientConfig();

            var mqttClient = Substitute.For <IMqttClient>();

            mqttClient.ConnectAsync(Arg.Any <IMqttClientOptions>(), Arg.Any <CancellationToken>())
            .ReturnsForAnyArgs(_ => Task.FromResult(new MqttClientAuthenticateResult()));

            var clientWrapper = new MqttClientWrapper(
                mqttClient,
                mqttClientConfig,
                Substitute.For <IBrokerCallbacksInvoker>(),
                Substitute.For <ISilverbackLogger>());

            var task1 = clientWrapper.ConnectAsync(sender1);
            var task2 = clientWrapper.ConnectAsync(sender2);

            task2.Should().BeSameAs(task1);

            await task1;

            await mqttClient.Received(1).ConnectAsync(
                Arg.Any <IMqttClientOptions>(),
                Arg.Any <CancellationToken>());

            await mqttClient.Received(1).ConnectAsync(
                mqttClientConfig.GetMqttClientOptions(),
                Arg.Any <CancellationToken>());
        }
        DisconnectAsync_CalledFromMultipleSenders_ClientDisconnectedWhenAllSendersDisconnect()
        {
            var sender1          = new object();
            var sender2          = new object();
            var mqttClientConfig = new MqttClientConfig();

            var mqttClient = Substitute.For <IMqttClient>();

            mqttClient.ConnectAsync(Arg.Any <IMqttClientOptions>(), Arg.Any <CancellationToken>())
            .ReturnsForAnyArgs(_ => Task.FromResult(new MqttClientAuthenticateResult()));

            var clientWrapper = new MqttClientWrapper(
                mqttClient,
                mqttClientConfig,
                Substitute.For <IBrokerCallbacksInvoker>(),
                Substitute.For <ISilverbackLogger>());

            await clientWrapper.ConnectAsync(sender1);

            await clientWrapper.ConnectAsync(sender2);

            mqttClient.IsConnected.Returns(true);

            await clientWrapper.DisconnectAsync(sender1);

            await mqttClient.Received(0).DisconnectAsync(
                Arg.Any <MqttClientDisconnectOptions>(),
                Arg.Any <CancellationToken>());

            await clientWrapper.DisconnectAsync(sender2);

            await mqttClient.Received(1).DisconnectAsync(
                Arg.Any <MqttClientDisconnectOptions>(),
                Arg.Any <CancellationToken>());
        }
        public void GetClient_ProducersWithSameClientConfig_SameClientReturned()
        {
            var config = new MqttClientConfig
            {
                ChannelOptions = new MqttClientTcpOptions
                {
                    Server = "mqtt-server"
                }
            };

            var producer1 = (MqttProducer)_broker.GetProducer(
                new MqttProducerEndpoint("some-topic")
            {
                Configuration = config
            });
            var producer2 = (MqttProducer)_broker.GetProducer(
                new MqttProducerEndpoint("some-topic")
            {
                Configuration = config
            });

            var factory = new MqttClientsCache(
                new MqttNetClientFactory(Substitute.For <IMqttNetLogger>()),
                Substitute.For <IBrokerCallbacksInvoker>(),
                _logger);
            var client1 = factory.GetClient(producer1);
            var client2 = factory.GetClient(producer2);

            client1.Should().NotBeNull();
            client2.Should().NotBeNull();
            client2.Should().BeSameAs(client1);
        }
示例#4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="MqttOutboundEndpointRouter{TMessage}" /> class.
 /// </summary>
 /// <param name="routerFunction">
 ///     The <see cref="DictionaryOutboundRouter{TMessage,TEndpoint}.RouterFunction" />.
 /// </param>
 /// <param name="endpointBuilderActions">
 ///     The <see cref="IReadOnlyDictionary{TKey,TValue}" /> containing the key of each endpoint and the
 ///     <see cref="Action{T}" /> to be invoked to build them.
 /// </param>
 /// <param name="clientConfig">
 ///     The <see cref="MqttClientConfig" />.
 /// </param>
 public MqttOutboundEndpointRouter(
     RouterFunction routerFunction,
     IReadOnlyDictionary <string, Action <IMqttProducerEndpointBuilder> > endpointBuilderActions,
     MqttClientConfig clientConfig)
     : base(routerFunction, BuildEndpointsDictionary(endpointBuilderActions, clientConfig))
 {
 }
示例#5
0
 private static Dictionary <string, MqttProducerEndpoint> BuildEndpointsDictionary(
     IReadOnlyDictionary <string, Action <IMqttProducerEndpointBuilder> > endpointBuilderActions,
     MqttClientConfig clientConfig)
 {
     return(Check.NotNull(endpointBuilderActions, nameof(endpointBuilderActions))
            .ToDictionary(
                pair => pair.Key,
                pair => BuildEndpoint(pair.Value, clientConfig)));
 }
示例#6
0
        private static MqttProducerEndpoint BuildEndpoint(
            Action <IMqttProducerEndpointBuilder> builderAction,
            MqttClientConfig clientConfig)
        {
            var builder = new MqttProducerEndpointBuilder(clientConfig);

            builderAction.Invoke(builder);

            return(builder.Build());
        }
示例#7
0
        public void Default_ProtocolVersionV500Set()
        {
            var config = new MqttClientConfig();

            config.ProtocolVersion.Should().Be(MqttProtocolVersion.V500);
        }
示例#8
0
 public Task OnClientDisconnectingAsync(MqttClientConfig config) =>
 _publisher.PublishAsync(new TestEventOne());
示例#9
0
 public Task OnClientDisconnectingAsync(MqttClientConfig config)
 {
     CallsCount++;
     return(Task.CompletedTask);
 }
示例#10
0
 public TransactionsExporter(IMqttClient mqttClient, IOptions <MqttClientConfig> config, ILogger logger)
 {
     _config     = config.Value;
     _mqttClient = mqttClient;
     _logger     = logger;
 }