using Hangfire; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NSubstitute; using Shouldly; namespace Heimdall.Api.Tests; public sealed class RecurringJobStartupRetryTests { [Fact] public async Task ConfigureRecurringJobs_RetriesDistributedLockContention() { var manager = Substitute.For(); var calls = 0; manager.When(x => x.RemoveIfExists(Arg.Any())).Do(_ => { if (Interlocked.Increment(ref calls) == 1) throw new Hangfire.PostgreSql.PostgreSqlDistributedLockException("synthetic lock contention"); }); using var services = new ServiceCollection() .AddLogging() .AddSingleton(manager) .BuildServiceProvider(); await Program.ConfigureRecurringJobsAsync(services, CancellationToken.None); calls.ShouldBe(2); manager.Received(2).RemoveIfExists("heartbeat-monitor"); } }