Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public async Task ProcessJob_HandlerFails_MarksJobAsFailed()
using var cts = new CancellationTokenSource();
await processor.StartAsync(cts.Token);
await channel.Writer.WriteAsync(job.Id);

var failed = await WaitForJobAsync(job.Id, j => j.Status == JobStatus.Failed, timeoutMs: 5000);

await Task.Delay(500);
cts.Cancel();
try { await processor.StopAsync(CancellationToken.None); } catch (OperationCanceledException) { }

failed.Status.Should().Be(JobStatus.Failed);
await using var verifyCtx = new AppDbContext(_dbOptions);
var failed = await verifyCtx.JobRecords.FindAsync(job.Id);
failed!.Status.Should().Be(JobStatus.Failed);
failed.ErrorMessage.Should().Contain("Something broke");
}

Expand Down Expand Up @@ -279,25 +279,6 @@ public async Task ProcessJob_MultipleJobs_AllComplete()
}
}

private async Task<JobRecord> WaitForJobAsync(Guid jobId, Func<JobRecord, bool> predicate, int timeoutMs = 3000)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
while (sw.ElapsedMilliseconds < timeoutMs)
{
await using var verifyCtx = new AppDbContext(_dbOptions);
var job = await verifyCtx.JobRecords.FindAsync(jobId);
if (job is not null && predicate(job))
return job;

await Task.Delay(100);
}

await using var finalCtx = new AppDbContext(_dbOptions);
var final = await finalCtx.JobRecords.FindAsync(jobId)
?? throw new InvalidOperationException($"Job {jobId} not found while waiting for state transition.");
return final;
}

private class TestJobHandler : IJobHandler
{
private readonly Action _action;
Expand Down
Loading