Files
Heimdall/tests/Heimdall.Infrastructure.Tests/MaintenanceScheduleTests.cs
T

76 lines
3.4 KiB
C#

using Hangfire;
using Hangfire.Common;
using Heimdall.Application.Services;
using Heimdall.Infrastructure.BackgroundJobs;
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Jobs;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class MaintenanceScheduleTests
{
[Fact]
public void TimeoutSweep_HasMinuteOccurrencesAndDistributedLock()
{
var manager = Substitute.For<IRecurringJobManager>();
JobSetup.ConfigureRecurringJobs(manager);
var call = manager.ReceivedCalls().Single(c => c.GetArguments()[0]?.ToString() == "analysis-job-timeout-sweep");
var cron = call.GetArguments().OfType<string>().Last();
cron.ShouldBe("* * * * *");
var schedule = Cronos.CronExpression.Parse(cron);
var start = new DateTime(2026, 10, 20, 0, 0, 0, DateTimeKind.Utc);
schedule.GetOccurrences(start, start.AddMinutes(3)).Count().ShouldBe(3);
var method = typeof(AnalysisJobDispatcherJob).GetMethod(nameof(AnalysisJobDispatcherJob.ExecuteTimeoutSweepAsync))!;
method.GetCustomAttributes(typeof(DisableConcurrentExecutionAttribute), true).ShouldHaveSingleItem();
}
[Fact]
public async Task Dispatch_DoesNotRunASecondTimeoutSweep()
{
var scheduler = Substitute.For<IAnalysisJobSchedulerService>();
var dispatcher = new AnalysisJobDispatcherJob(Substitute.For<IDbContextFactory<AppDbContext>>(), scheduler,
NullLogger<AnalysisJobDispatcherJob>.Instance);
await dispatcher.ExecuteAsync();
await scheduler.DidNotReceive().GetTimedOutJobsAsync(Arg.Any<CancellationToken>());
}
}
[Collection("PostgresInfra")]
public class SweepLockTests(Heimdall.TestCommon.PostgreSqlContainerFixture postgres)
{
[Fact]
public async Task PostgreSqlLock_ExcludesAnotherWorker_AndIsReleased()
{
var database = "sweep_test_" + Guid.NewGuid().ToString("N");
await using var admin = new Npgsql.NpgsqlConnection(postgres.ConnectionString);
await admin.OpenAsync();
await using (var create = new Npgsql.NpgsqlCommand($"CREATE DATABASE {database}", admin))
await create.ExecuteNonQueryAsync();
var builder = new Npgsql.NpgsqlConnectionStringBuilder(postgres.ConnectionString)
{ Database = database, Pooling = false };
try
{
var storage = new Hangfire.PostgreSql.PostgreSqlStorage(builder.ConnectionString, null,
new Hangfire.PostgreSql.PostgreSqlStorageOptions());
using var firstWorker = storage.GetConnection();
using var secondWorker = storage.GetConnection();
const string resource = "Heimdall.Infrastructure.Jobs.AnalysisJobDispatcherJob.ExecuteTimeoutSweepAsync";
using (firstWorker.AcquireDistributedLock(resource, TimeSpan.FromSeconds(2)))
{
Should.Throw<Hangfire.Storage.DistributedLockTimeoutException>(() =>
secondWorker.AcquireDistributedLock(resource, TimeSpan.Zero));
}
using var next = secondWorker.AcquireDistributedLock(resource, TimeSpan.FromSeconds(2));
}
finally
{
await using var drop = new Npgsql.NpgsqlCommand($"DROP DATABASE {database} WITH (FORCE)", admin);
await drop.ExecuteNonQueryAsync();
}
}
}