Keep the hosted AI queue polling after transient database failures
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Data.Common;
|
||||
using JobTrackerApi.Data;
|
||||
using JobTrackerApi.Models;
|
||||
using JobTrackerApi.Services;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
@@ -12,6 +14,63 @@ namespace JobTrackerApi.Tests;
|
||||
|
||||
public sealed class AiOperationQueueTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Hosted_worker_survives_transient_claim_failure_and_completes_once()
|
||||
{
|
||||
var interruption = new ClaimInterruption();
|
||||
await using var fixture = await Fixture.CreateAsync(interruption: interruption);
|
||||
await fixture.SeedUserAsync("pro-1", pro: true);
|
||||
await fixture.EnqueueAsync("pro-1", "recover");
|
||||
var configuration = fixture.Provider.GetRequiredService<IConfiguration>();
|
||||
configuration["Workers:AiOperationsEnabled"] = "true";
|
||||
configuration["AiQueue:IdleDelayMs"] = "100";
|
||||
using var hosted = new AiOperationHostedService(fixture.Provider.GetRequiredService<AiOperationWorker>(), configuration,
|
||||
Microsoft.Extensions.Logging.Abstractions.NullLogger<AiOperationHostedService>.Instance);
|
||||
interruption.Armed = true;
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||
try
|
||||
{
|
||||
await hosted.StartAsync(timeout.Token);
|
||||
var finished = await Task.WhenAny(hosted.ExecuteTask!, interruption.PolledAfterRecovery.Task).WaitAsync(timeout.Token);
|
||||
if (finished == hosted.ExecuteTask) await finished;
|
||||
Assert.Same(interruption.PolledAfterRecovery.Task, finished);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await hosted.StopAsync(timeout.Token);
|
||||
}
|
||||
|
||||
await using var scope = fixture.Provider.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<JobTrackerContext>();
|
||||
var operation = await db.UserOperations.IgnoreQueryFilters().AsNoTracking().SingleAsync();
|
||||
Assert.Equal(OperationStatuses.Succeeded, operation.Status);
|
||||
Assert.Equal(1, operation.AttemptCount);
|
||||
Assert.Null(operation.LeaseToken);
|
||||
Assert.Equal("pro-1", fixture.Handler.OwnerUserId);
|
||||
Assert.Single(await db.UserNotifications.IgnoreQueryFilters().ToListAsync());
|
||||
}
|
||||
|
||||
private sealed class ClaimInterruption : DbCommandInterceptor
|
||||
{
|
||||
private int _polls;
|
||||
public bool Armed { get; set; }
|
||||
public TaskCompletionSource PolledAfterRecovery { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
|
||||
DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (Armed && command.CommandText.Contains("UserOperations", StringComparison.Ordinal) &&
|
||||
command.CommandText.Contains("ORDER BY", StringComparison.Ordinal))
|
||||
{
|
||||
var poll = Interlocked.Increment(ref _polls);
|
||||
if (poll == 1) throw new SqliteException("Synthetic busy claim", 5);
|
||||
if (poll == 3) PolledAfterRecovery.TrySetResult();
|
||||
}
|
||||
return new(result);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Admission_is_pro_only_idempotent_bounded_and_contains_no_raw_payload()
|
||||
{
|
||||
@@ -159,7 +218,7 @@ public sealed class AiOperationQueueTests
|
||||
Handler = handler;
|
||||
}
|
||||
|
||||
public static async Task<Fixture> CreateAsync(int perUserCapacity = 10)
|
||||
public static async Task<Fixture> CreateAsync(int perUserCapacity = 10, ClaimInterruption? interruption = null)
|
||||
{
|
||||
var connection = new SqliteConnection("Data Source=:memory:");
|
||||
await connection.OpenAsync();
|
||||
@@ -177,7 +236,11 @@ public sealed class AiOperationQueueTests
|
||||
services.AddHttpContextAccessor();
|
||||
services.AddScoped<CurrentUserService>();
|
||||
services.AddScoped<ICurrentUserService>(provider => provider.GetRequiredService<CurrentUserService>());
|
||||
services.AddDbContext<JobTrackerContext>((_, options) => options.UseSqlite(connection));
|
||||
services.AddDbContext<JobTrackerContext>((_, options) =>
|
||||
{
|
||||
options.UseSqlite(connection);
|
||||
if (interruption is not null) options.AddInterceptors(interruption);
|
||||
});
|
||||
services.AddIdentityCore<ApplicationUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<JobTrackerContext>();
|
||||
services.AddSingleton(TimeProvider.System);
|
||||
services.AddScoped<UserOperationStore>();
|
||||
|
||||
@@ -34,7 +34,8 @@ public sealed class CvProcessingOperationTests
|
||||
var configuration = fixture.Provider.GetRequiredService<IConfiguration>();
|
||||
configuration["Workers:AiOperationsEnabled"] = "false";
|
||||
configuration["Workers:CvDocumentImportsEnabled"] = "true";
|
||||
using var hosted = new AiOperationHostedService(fixture.Provider.GetRequiredService<AiOperationWorker>(), configuration);
|
||||
using var hosted = new AiOperationHostedService(fixture.Provider.GetRequiredService<AiOperationWorker>(), configuration,
|
||||
Microsoft.Extensions.Logging.Abstractions.NullLogger<AiOperationHostedService>.Instance);
|
||||
await hosted.StartAsync(default);
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user