public async Task ICollectorOutputsArePublished() { var mqttApplicationMessages = new List <MqttApplicationMessage>();; using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <ICollectorOutputIsPublishedTestFunction> .RunFor(_testLoggerProvider)) { await mqttClient.SubscribeAsync("test/outtopic"); await mqttClient.SubscribeAsync("test/outtopic2"); mqttClient.OnMessage += (object sender, OnMessageEventArgs e) => mqttApplicationMessages.Add(e.ApplicationMessage); await jobHost.CallAsync(nameof(ICollectorOutputIsPublishedTestFunction.Testert)); await WaitFor(() => ICollectorOutputIsPublishedTestFunction.CallCount >= 1); await WaitFor(() => mqttApplicationMessages.Count > 0); } Assert.Equal(1, ICollectorOutputIsPublishedTestFunction.CallCount); Assert.Equal(2, mqttApplicationMessages.Count); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic"); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic2"); var bodyString = Encoding.UTF8.GetString(mqttApplicationMessages.First().Payload); Assert.Equal("", bodyString); }
public async Task TriggerAndOutputReuseConnection() { var mqttApplicationMessages = new List <MqttApplicationMessage>(); var counnections = 0; var options = new MqttServerOptionsBuilder() .WithConnectionValidator(x => { counnections += (x.ClientId != "IntegrationTest") ? 1 : 0; Debug.WriteLine($"ClientId:{x.ClientId}"); }) .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <TriggerAndOutputWithSameConnectionTestFunction> .RunFor(_testLoggerProvider)) { await mqttClient.SubscribeAsync("test/outtopic"); await mqttClient.SubscribeAsync("test/outtopic2"); await mqttServer.PublishAsync(DefaultMessage); var firstMessage = await mqttClient.WaitForMessage(); if (firstMessage != null) { mqttApplicationMessages.Add(firstMessage); var secondMessage = await mqttClient.WaitForMessage(); if (secondMessage != null) { mqttApplicationMessages.Add(secondMessage); } } await WaitFor(() => TriggerAndOutputWithSameConnectionTestFunction.CallCount >= 1); } Assert.Equal(1, TriggerAndOutputWithSameConnectionTestFunction.CallCount); Assert.Equal(1, counnections); Assert.Equal(2, mqttApplicationMessages.Count); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic"); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic2"); var bodyString = Encoding.UTF8.GetString(mqttApplicationMessages.First().Payload); Assert.Equal("{\"test\":\"message\"}", bodyString); }
public async Task SimpleMessageIsPublished() { MqttApplicationMessage mqttApplicationMessage = null; using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <SimpleOutputIsPublishedTestFunction> .RunFor(_testLoggerProvider)) { await mqttClient.SubscribeAsync("test/topic"); await jobHost.CallAsync(nameof(SimpleOutputIsPublishedTestFunction.Testert)); mqttApplicationMessage = await mqttClient.WaitForMessage(); } Assert.NotNull(mqttApplicationMessage); }
public async Task TriggerAndOutputUseDifferentConnection() { MqttApplicationMessage mqttApplicationMessage = null; var connectionsCountServer1 = 0; var optionsServer1 = new MqttServerOptionsBuilder() .WithDefaultEndpointPort(1337) .WithConnectionValidator(x => { connectionsCountServer1 += (x.ClientId != "IntegrationTest") ? 1 : 0; }) .Build(); var connectionsCountServer2 = 0; var optionsServer2 = new MqttServerOptionsBuilder() .WithConnectionValidator(x => { connectionsCountServer2 += (x.ClientId != "IntegrationTest") ? 1 : 0; }) .Build(); using (var mqttServer1 = await MqttServerHelper.Get(_logger, optionsServer1)) using (var mqttServer2 = await MqttServerHelper.Get(_logger, optionsServer2)) using (var mqttClientForServer2 = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <TriggerAndOutputWithDifferentConnectionTestFunction> .RunFor(_testLoggerProvider)) { await mqttClientForServer2.SubscribeAsync("test/outtopic"); await mqttServer1.PublishAsync(DefaultMessage); mqttApplicationMessage = await mqttClientForServer2.WaitForMessage(); await WaitFor(() => TriggerAndOutputWithDifferentConnectionTestFunction.CallCount >= 1); } Assert.Equal(1, TriggerAndOutputWithDifferentConnectionTestFunction.CallCount); Assert.Equal(1, connectionsCountServer1); Assert.Equal(1, connectionsCountServer2); Assert.NotNull(mqttApplicationMessage); Assert.Equal("test/outtopic", mqttApplicationMessage.Topic); var bodyString = Encoding.UTF8.GetString(mqttApplicationMessage.Payload); Assert.Equal("{\"test\":\"message\"}", bodyString); }
public async Task SimpleMessageIsPublished() { MqttApplicationMessage mqttApplicationMessage = null; using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <SimpleOutputIsPublishedTestFunction> .RunFor(_loggerFactory)) { await mqttClient.SubscribeAsync("test/topic"); mqttClient.OnMessage += (object sender, OnMessageEventArgs e) => mqttApplicationMessage = e.ApplicationMessage; await jobHost.CallAsync(nameof(SimpleOutputIsPublishedTestFunction.Testert)); await WaitFor(() => mqttApplicationMessage != null); } Assert.NotNull(mqttApplicationMessage); }