Ejemplo n.º 1
0
        public async Task ClientCleanupOnAuthentificationFails()
        {
            var channel  = new TestMqttCommunicationAdapter();
            var channel2 = new TestMqttCommunicationAdapter();

            channel.Partner  = channel2;
            channel2.Partner = channel;

            Task.Run(async() => {
                var connect = await channel2.ReceivePacketAsync(TimeSpan.Zero, CancellationToken.None);
                await channel2.SendPacketAsync(new MqttConnAckPacket()
                {
                    ConnectReturnCode = Protocol.MqttConnectReturnCode.ConnectionRefusedNotAuthorized
                }, CancellationToken.None);
            });



            var fake = new TestMqttCommunicationAdapterFactory(channel);

            var client = new MqttClient(fake, new MqttNetLogger());

            try
            {
                await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("any-server").Build());
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(MqttConnectingFailedException));
            }

            Assert.IsTrue(client._packetReceiverTask == null || client._packetReceiverTask.IsCompleted, "receive loop not completed");
            Assert.IsTrue(client._keepAliveMessageSenderTask == null || client._keepAliveMessageSenderTask.IsCompleted, "keepalive loop not completed");
        }
Ejemplo n.º 2
0
        public async Task <IMqttClient> ConnectTestClient(IMqttServer server, string clientId, MqttApplicationMessage willMessage = null)
        {
            var adapterA = new TestMqttCommunicationAdapter();
            var adapterB = new TestMqttCommunicationAdapter();

            adapterA.Partner = adapterB;
            adapterB.Partner = adapterA;

            var client = new MqttClient(
                new TestMqttCommunicationAdapterFactory(adapterA),
                new MqttNetLogger());

            var connected = WaitForClientToConnect(server, clientId);

            FireClientAcceptedEvent(adapterB);

            var options = new MqttClientOptions
            {
                ClientId       = clientId,
                WillMessage    = willMessage,
                ChannelOptions = new MqttClientTcpOptions()
            };

            await client.ConnectAsync(options);

            await connected;

            return(client);
        }
Ejemplo n.º 3
0
        private static MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
        {
            var adapterA = new TestMqttCommunicationAdapter();
            var adapterB = new TestMqttCommunicationAdapter();

            adapterA.Partner = adapterB;
            adapterB.Partner = adapterA;

            var client = new MqttClient(new MqttClientOptions(), adapterA);

            server.InjectClient(clientId, adapterB);
            client.ConnectAsync(willMessage).Wait();
            return(client);
        }