Skip to content

Commit

Permalink
AspNetCoreTestEnvironment: Adapt logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 6, 2024
1 parent 83148ca commit 41af5c5
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions Source/MQTTnet.Tests/Mockups/AspNetCoreTestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ public override IMqttClient CreateClient()
{
var services = new ServiceCollection();
var clientBuilder = services.AddMqttClient();
if (EnableLogger)
{
clientBuilder.UseAspNetCoreMqttNetLogger();
}
else
{
clientBuilder.UseMqttNetNullLogger();
}
UseLogger(clientBuilder);

var s = services.BuildServiceProvider();
var client = s.GetRequiredService<IMqttClientFactory>().CreateMqttClient();
Expand Down Expand Up @@ -84,7 +77,8 @@ public override async Task<MqttServer> StartServer(Action<MqttServerOptionsBuild
}

var appBuilder = WebApplication.CreateBuilder();
appBuilder.Services.AddMqttServer(optionsBuilder =>

var serverBuilder = appBuilder.Services.AddMqttServer(optionsBuilder =>
{
optionsBuilder.WithDefaultEndpoint();
optionsBuilder.WithDefaultEndpointPort(ServerPort);
Expand All @@ -93,19 +87,20 @@ public override async Task<MqttServer> StartServer(Action<MqttServerOptionsBuild
{
if (o.DefaultEndpointOptions.Port == 0)
{
o.DefaultEndpointOptions.Port = GetServerPort();
var serverPort = GetServerPort();
o.DefaultEndpointOptions.Port = serverPort;
ServerPort = serverPort;
}
});

UseLogger(serverBuilder);

appBuilder.WebHost.UseKestrel(k => k.ListenMqtt());
appBuilder.Host.ConfigureHostOptions(h => h.ShutdownTimeout = TimeSpan.FromMilliseconds(500d));

_app = appBuilder.Build();

// The OS has chosen the port to we have to properly expose it to the tests.
ServerPort = _app.Services.GetRequiredService<MqttServerOptions>().DefaultEndpointOptions.Port;

await _app.StartAsync();

Server = _app.Services.GetRequiredService<MqttServer>();
return Server;
}
Expand All @@ -126,17 +121,18 @@ public override async Task<MqttServer> StartServer(MqttServerOptionsBuilder opti
optionsBuilder.WithDefaultEndpointPort(ServerPort);
optionsBuilder.WithMaxPendingMessagesPerClient(int.MaxValue);

var options = optionsBuilder.Build();

var appBuilder = WebApplication.CreateBuilder();
appBuilder.Services.AddMqttServer();
appBuilder.Services.AddSingleton(options);
appBuilder.Services.AddSingleton(optionsBuilder.Build());
var serverBuilder = appBuilder.Services.AddMqttServer();

UseLogger(serverBuilder);

appBuilder.WebHost.UseKestrel(k => k.ListenMqtt());
appBuilder.Host.ConfigureHostOptions(h => h.ShutdownTimeout = TimeSpan.FromMilliseconds(500d));

_app = appBuilder.Build();
await _app.StartAsync();

Server = _app.Services.GetRequiredService<MqttServer>();
return Server;
}
Expand Down Expand Up @@ -164,5 +160,17 @@ private static int GetServerPort()
}
return port;
}

private void UseLogger(IMqttBuilder builder)
{
if (EnableLogger)
{
builder.UseAspNetCoreMqttNetLogger();
}
else
{
builder.UseMqttNetNullLogger();
}
}
}
}

0 comments on commit 41af5c5

Please sign in to comment.