Path: redb-route/tests/redb.Route.Tests.Quartz/QuartzRestartTests.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/QuartzRestartTests.cs
----
using redb.Route.Core;
using redb.Route.Quartz;
namespace redb.Route.Tests.Quartz;
///
/// Quartz timers keep firing after Stop() and Start() of the same route context: the stop removes the
/// jobs of the cached endpoints from the scheduler, the start schedules them again.
///
public class QuartzRestartTests
{
private static async Task WaitForAsync(Func condition, string because)
{
var deadline = DateTime.UtcNow.AddSeconds(15);
while (!condition() && DateTime.UtcNow < deadline)
await Task.Delay(50);
condition().Should().BeTrue(because);
}
[Fact]
public async Task Qtimer_fires_after_the_context_is_stopped_and_started_again()
{
var fired = 0;
await using var context = new RouteContext();
context.AddComponent(new QuartzTimerComponent());
context.AddRoutes(r => r.From("qtimer://restart?period=100").Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
await WaitForAsync(() => Volatile.Read(ref fired) >= 1, "qtimer fires");
await context.Stop();
var before = Volatile.Read(ref fired);
await context.Start();
await WaitForAsync(() => Volatile.Read(ref fired) > before, "qtimer fires again after a restart");
}
[Fact]
public async Task Cron_fires_after_the_context_is_stopped_and_started_again()
{
var fired = 0;
await using var context = new RouteContext();
context.AddComponent(new CronComponent());
context.AddRoutes(r => r.From("cron://restart?schedule=0/1 * * * * ?").Process(_ => Interlocked.Increment(ref fired)));
await context.Start();
await WaitForAsync(() => Volatile.Read(ref fired) >= 1, "cron fires");
await context.Stop();
var before = Volatile.Read(ref fired);
await context.Start();
await WaitForAsync(() => Volatile.Read(ref fired) > before, "cron fires again after a restart");
}
}