Resolve migration routine loggers from the application container

This commit is contained in:
Shadowghost
2026-09-06 07:46:18 +02:00
parent 9c259027df
commit ca90347dc2
6 changed files with 143 additions and 115 deletions
@@ -20,7 +20,6 @@ public class CodeMigrationTests
.RegisterStartupLogger()
.AddSingleton<ApplicationSingleton>()
.AddTransient<MigrationTransient>();
services.AddSingleton(services);
await using var serviceProvider = services.BuildServiceProvider();
var applicationSingleton = serviceProvider.GetRequiredService<ApplicationSingleton>();
@@ -44,6 +43,29 @@ public class CodeMigrationTests
Assert.Same(logger.Topic, performed.Logger.Topic);
}
[Fact]
public async Task Perform_DoesNotLeakTheMigrationTopic()
{
var services = new ServiceCollection()
.AddLogging()
.RegisterStartupLogger()
.AddSingleton<ApplicationSingleton>()
.AddTransient<MigrationTransient>();
await using var serviceProvider = services.BuildServiceProvider();
var logger = new StartupLogger(NullLogger.Instance).BeginGroup($"Test migration");
var migration = new CodeMigration(
typeof(TestMigration),
new JellyfinMigrationAttribute("2026-09-05T10:00:00", nameof(TestMigration)),
null);
await migration.Perform(serviceProvider, logger, CancellationToken.None);
// The topic belongs to the migration that ran, so loggers resolved afterwards must not still write into it.
Assert.Null(serviceProvider.GetRequiredService<IStartupLogger<CodeMigrationTests>>().Topic);
Assert.Null(new StartupLogger(NullLogger.Instance).Topic);
}
private sealed class ApplicationSingleton : IDisposable
{
public bool IsDisposed { get; private set; }
@@ -0,0 +1,54 @@
using Jellyfin.Server.ServerSetupApp;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Jellyfin.Server.Tests.ServerSetupApp;
public class StartupLoggerTests
{
[Fact]
public void BeginAmbientTopic_AttachesNewLoggersToTheTopic()
{
var migration = new StartupLogger(NullLogger.Instance).BeginGroup($"Migration");
using (StartupLogger.BeginAmbientTopic(migration.Topic))
{
Assert.Same(migration.Topic, new StartupLogger(NullLogger.Instance).Topic);
}
}
[Fact]
public void BeginAmbientTopic_RestoresThePreviousTopic()
{
var root = new StartupLogger(NullLogger.Instance);
var outer = root.BeginGroup($"Outer");
var inner = outer.BeginGroup($"Inner");
Assert.Null(new StartupLogger(NullLogger.Instance).Topic);
using (StartupLogger.BeginAmbientTopic(outer.Topic))
{
using (StartupLogger.BeginAmbientTopic(inner.Topic))
{
Assert.Same(inner.Topic, new StartupLogger(NullLogger.Instance).Topic);
}
// Leaving a nested topic has to fall back to the enclosing one, not to the setup UI root.
Assert.Same(outer.Topic, new StartupLogger(NullLogger.Instance).Topic);
}
Assert.Null(new StartupLogger(NullLogger.Instance).Topic);
}
[Fact]
public void BeginGroup_KeepsAnExplicitTopicOverTheAmbientOne()
{
var migration = new StartupLogger(NullLogger.Instance).BeginGroup($"Migration");
var unrelated = new StartupLogger(NullLogger.Instance).BeginGroup($"Unrelated");
using (StartupLogger.BeginAmbientTopic(migration.Topic))
{
Assert.Same(unrelated.Topic, unrelated.With(NullLogger.Instance).Topic);
}
}
}