Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions test/CloudNative.CloudEvents.UnitTests/Mqtt/MqttTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.

using CloudNative.CloudEvents.NewtonsoftJson;
using CloudNative.CloudEvents.SystemTextJson;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;
Expand All @@ -17,16 +17,19 @@ namespace CloudNative.CloudEvents.Mqtt.UnitTests;
public class MqttTest : IDisposable
{
private readonly MqttServer mqttServer;
private readonly int mqttServerPort;

public MqttTest()
{
var optionsBuilder = new MqttServerOptionsBuilder()
var options = new MqttServerOptionsBuilder()
.WithConnectionBacklog(100)
.WithDefaultEndpoint()
.WithDefaultEndpointPort(52355);
.WithDefaultEndpointPort(0) // 0 means randomly assigning a port during server startup, prevents conflicts with other tests
.WithDefaultEndpoint().Build();

this.mqttServer = new MqttFactory().CreateMqttServer(optionsBuilder.Build());
this.mqttServer = new MqttFactory().CreateMqttServer(options);
mqttServer.StartAsync().GetAwaiter().GetResult();

this.mqttServerPort = options.DefaultEndpointOptions.Port; // port only gets assigned after the server starts, read it earlier and it stays 0
}

public void Dispose()
Expand All @@ -37,7 +40,6 @@ public void Dispose()
[Fact]
public async Task MqttSendTest()
{

var jsonEventFormatter = new JsonEventFormatter();
var cloudEvent = new CloudEvent
{
Expand All @@ -54,7 +56,7 @@ public async Task MqttSendTest()

var options = new MqttClientOptionsBuilder()
.WithClientId("Client1")
.WithTcpServer("127.0.0.1", 52355)
.WithTcpServer("127.0.0.1", mqttServerPort)
.WithCleanSession()
.Build();

Expand Down