Skip to content

Dynamic mqtt port for unit tests#346

Merged
jskeet merged 1 commit intocloudevents:mainfrom
erwinkramer:feature/utest
Apr 27, 2026
Merged

Dynamic mqtt port for unit tests#346
jskeet merged 1 commit intocloudevents:mainfrom
erwinkramer:feature/utest

Conversation

@erwinkramer
Copy link
Copy Markdown
Contributor

@erwinkramer erwinkramer commented Apr 27, 2026

This PR assigns a dynamic port number during mqtt server start and also moves from NewtonsoftJson formatter to SystemTextJson in the mqtt tests.

Should solve port conflicts like https://github.com/cloudevents/sdk-csharp/actions/runs/24733076929/job/72352560560?pr=345#step:4:102. I assume because the .net8 and .net10 tests currently have a race condition to both claim port 52355.

Related to #192.

I verified by starting 2 mqtt servers async, with similar MqttServerOptions setup, that seems to handle it fine:

var options = new MqttServerOptionsBuilder()
    .WithConnectionBacklog(100)
    .WithDefaultEndpointPort(0)
    .WithDefaultEndpoint().Build();

var options2 = new MqttServerOptionsBuilder()
   .WithConnectionBacklog(100)
   .WithDefaultEndpointPort(0)
   .WithDefaultEndpoint().Build();

this.mqttServer = new MqttFactory().CreateMqttServer(options);
this.mqttServer2 = new MqttFactory().CreateMqttServer(options2);
var one = mqttServer.StartAsync();
var two = mqttServer2.StartAsync();

one.GetAwaiter().GetResult();
two.GetAwaiter().GetResult(); // both start fine

Console.WriteLine(options2.DefaultEndpointOptions.Port); 
Console.WriteLine(options.DefaultEndpointOptions.Port); // port differs from server 2

Co-authored-by: Copilot <copilot@github.com>
Signed-off-by: Erwin <erwinkramer@hotmail.com>
@jskeet jskeet merged commit 90e96a6 into cloudevents:main Apr 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants