elsa-core/test/integration/Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.IntegrationTests/SqliteStructuredLogMigrationTests.cs
Sipke Schoorstra 0687b5f9f3
Fix SQLite structured log shell lifecycle (#7461)
* Fix SQLite structured log shell lifecycle

* Address Greptile lifecycle feedback

* Tighten structured log lifecycle guards

* Address structured log background task review
2026-05-18 01:04:04 +02:00

68 lines
2.7 KiB
C#

using Elsa.Common;
using Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.Options;
using Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.Services;
using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.IntegrationTests;
public class SqliteStructuredLogMigrationTests
{
[Fact]
public async Task MigrateAsync_CreatesStructuredLogTableInEmptyDatabase()
{
await using var host = new SqliteStructuredLogTestHost(migrate: false);
await host.Migrator.MigrateAsync();
Assert.True(await host.TableExistsAsync("StructuredLogEvents"));
}
[Fact]
public async Task Startup_DoesNotRunMigrations_WhenOptedOut()
{
await using var host = new SqliteStructuredLogTestHost(options => options.RunMigrationsOnStartup = false, migrate: false);
var startup = host.Services.GetServices<IHostedService>().OfType<SqliteStructuredLogStartupService>().Single();
await startup.StartAsync(CancellationToken.None);
Assert.False(await host.TableExistsAsync("StructuredLogEvents"));
}
[Fact]
public async Task StartupTask_RunsMigrations_WhenHostedServicesAreNotStarted()
{
await using var host = new SqliteStructuredLogTestHost(migrate: false);
using var scope = host.Services.CreateScope();
var startup = scope.ServiceProvider.GetServices<IStartupTask>().OfType<SqliteStructuredLogStartupService>().Single();
await startup.ExecuteAsync(CancellationToken.None);
Assert.True(await host.TableExistsAsync("StructuredLogEvents"));
}
[Fact]
public async Task StartupTask_UsesSameInstance_AsHostedService()
{
await using var host = new SqliteStructuredLogTestHost(migrate: false);
using var scope = host.Services.CreateScope();
var hostedService = host.Services.GetServices<IHostedService>().OfType<SqliteStructuredLogStartupService>().Single();
var startupTask = scope.ServiceProvider.GetServices<IStartupTask>().OfType<SqliteStructuredLogStartupService>().Single();
Assert.Same(hostedService, startupTask);
}
[Fact]
public async Task HostedServices_StartMigrationBeforeWriteBuffer()
{
await using var host = new SqliteStructuredLogTestHost(migrate: false);
var hostedServiceTypes = host.Services.GetServices<IHostedService>().Select(x => x.GetType()).ToList();
Assert.True(
hostedServiceTypes.IndexOf(typeof(SqliteStructuredLogStartupService)) < hostedServiceTypes.IndexOf(typeof(StructuredLogWriteBuffer)),
"SQLite migrations must run before the durable write buffer starts flushing queued logs.");
}
}