Path: redb-route/tests/redb.Route.Tests.Quartz/QuartzOperatorPauseTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Quartz/QuartzOperatorPauseTests.cs
----
using System.Collections.Specialized;
using Quartz;
using Quartz.Impl;
using redb.Route.Core;
using redb.Route.Quartz;
namespace redb.Route.Tests.Quartz;
///
/// A job an operator paused on the scheduler (a host's panel, CLI or API) stays paused when its route starts again, on
/// the in-memory store: the start replaces the trigger with the route's definition, and the replacement keeps the pause.
/// That holds when the stop removed the job (deleteJob=true, the default) or paused it itself
/// (pauseJob=true): the stop notes the operator's pause on the scheduler, and the next start pauses again. A
/// pause the route set itself at its stop is the route's, and its start lifts it.
///
public class QuartzOperatorPauseTests
{
[Theory]
[InlineData("qtimer", "qtimer://held-timer?period=200&deleteJob=false")]
[InlineData("cron", "cron://held-cron?schedule=0/1 * * * * ?&deleteJob=false")]
public async Task An_operators_pause_survives_a_restart_of_the_route(string scheme, string uri)
{
var scheduler = await NewScheduler();
try
{
var fired = 0;
await using var context = new RouteContext();
context.AddService(typeof(IScheduler), scheduler);
context.AddComponent(scheme == "cron" ? new CronComponent() : new QuartzTimerComponent());
context.AddRoutes(r => r.From(uri).RouteId("held").Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
await WaitForAsync(() => Volatile.Read(ref fired) >= 1, "the job fires before the pause");
var job = new JobKey(scheme == "cron" ? "held-cron" : "held-timer", "redb");
await scheduler.PauseJob(job);
await context.StopRoute("held");
await context.StartRoute("held");
var trigger = (await scheduler.GetTriggersOfJob(job)).Single().Key;
(await scheduler.GetTriggerState(trigger)).Should().Be(TriggerState.Paused,
"the operator paused the job; a restart of its route is not a decision to resume it");
var before = Volatile.Read(ref fired);
await Task.Delay(1500);
Volatile.Read(ref fired).Should().Be(before, "a paused job does not fire");
}
finally
{
await scheduler.Shutdown(waitForJobsToComplete: false);
}
}
[Theory]
[InlineData("qtimer://gone-timer?period=200")]
[InlineData("qtimer://gone-paused?period=200&pauseJob=true&deleteJob=false")]
public async Task An_operators_pause_survives_a_restart_of_the_route_that_removes_or_pauses_its_job_at_the_stop(string uri)
{
// deleteJob=true (the default) removes the job, pause included, at the stop; pauseJob=true pauses it there, and
// its start lifts its own pause. Neither may take the operator's pause with it.
var scheduler = await NewScheduler();
try
{
var fired = 0;
await using var context = new RouteContext();
context.AddService(typeof(IScheduler), scheduler);
context.AddComponent(new QuartzTimerComponent());
context.AddRoutes(r => r.From(uri).RouteId("gone").Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
await WaitForAsync(() => Volatile.Read(ref fired) >= 1, "the job fires before the pause");
var name = new Uri(uri).Host;
await scheduler.PauseJob(new JobKey(name, "redb"));
await context.StopRoute("gone");
await context.StartRoute("gone");
(await scheduler.GetTriggerState(new TriggerKey(name, "redb"))).Should().Be(TriggerState.Paused,
"the operator paused the job; what the route does with it at its stop is not the operator's decision");
var before = Volatile.Read(ref fired);
await Task.Delay(1500);
Volatile.Read(ref fired).Should().Be(before, "a paused job does not fire");
}
finally
{
await scheduler.Shutdown(waitForJobsToComplete: false);
}
}
[Fact]
public async Task A_job_the_operator_resumed_while_its_route_was_stopped_fires_after_the_start()
{
var scheduler = await NewScheduler();
try
{
var fired = 0;
await using var context = new RouteContext();
context.AddService(typeof(IScheduler), scheduler);
context.AddComponent(new QuartzTimerComponent());
context.AddRoutes(r => r.From("qtimer://back?period=200&pauseJob=true&deleteJob=false").RouteId("back")
.Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
var job = new JobKey("back", "redb");
await scheduler.PauseJob(job);
await context.StopRoute("back");
await scheduler.ResumeJob(job);
await context.StartRoute("back");
var before = Volatile.Read(ref fired);
(await scheduler.GetTriggerState(new TriggerKey("back", "redb"))).Should().Be(TriggerState.Normal,
"the operator resumed the job; the pause seen at the stop is no longer theirs");
await WaitForAsync(() => Volatile.Read(ref fired) > before, "the job fires again");
}
finally
{
await scheduler.Shutdown(waitForJobsToComplete: false);
}
}
[Fact]
public async Task A_job_kept_paused_over_a_restart_fires_again_once_the_operator_resumes_it()
{
var scheduler = await NewScheduler();
try
{
var fired = 0;
await using var context = new RouteContext();
context.AddService(typeof(IScheduler), scheduler);
context.AddComponent(new QuartzTimerComponent());
context.AddRoutes(r => r.From("qtimer://resumed?period=200&deleteJob=false").RouteId("resumed")
.Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
var job = new JobKey("resumed", "redb");
await scheduler.PauseJob(job);
await context.StopRoute("resumed");
await context.StartRoute("resumed");
var before = Volatile.Read(ref fired);
await scheduler.ResumeJob(job);
await WaitForAsync(() => Volatile.Read(ref fired) > before, "the resumed job fires on the route's schedule");
}
finally
{
await scheduler.Shutdown(waitForJobsToComplete: false);
}
}
[Fact]
public async Task The_routes_own_pause_at_its_stop_is_lifted_at_its_start()
{
var scheduler = await NewScheduler();
try
{
var fired = 0;
await using var context = new RouteContext();
context.AddService(typeof(IScheduler), scheduler);
context.AddComponent(new QuartzTimerComponent());
context.AddRoutes(r => r.From("qtimer://own-pause?period=200&pauseJob=true&deleteJob=false").RouteId("own")
.Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
var trigger = new TriggerKey("own-pause", "redb");
await context.StopRoute("own");
(await scheduler.GetTriggerState(trigger)).Should().Be(TriggerState.Paused, "pauseJob=true pauses at the stop");
await context.StartRoute("own");
var before = Volatile.Read(ref fired);
(await scheduler.GetTriggerState(trigger)).Should().Be(TriggerState.Normal, "the route lifts its own pause");
await WaitForAsync(() => Volatile.Read(ref fired) > before, "the job fires again");
}
finally
{
await scheduler.Shutdown(waitForJobsToComplete: false);
}
}
private static async Task WaitForAsync(Func condition, string because)
{
var deadline = DateTime.UtcNow.AddSeconds(20);
while (!condition() && DateTime.UtcNow < deadline)
await Task.Delay(50);
condition().Should().BeTrue(because);
}
private static Task NewScheduler()
=> new StdSchedulerFactory(new NameValueCollection
{
["quartz.scheduler.instanceName"] = "redb-route-pause-" + Guid.NewGuid().ToString("N"),
}).GetScheduler();
}