In my scheduler, implemented with quartz.net v3, i'm trying to test the behaviour of the cancellation token:
....
IScheduler scheduler = await factory.GetScheduler();
....
var tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;
// Start scheduler
await scheduler.Start(ct);
// some sleep
await Task.Delay(TimeSpan.FromSeconds(60));
// communicate cancellation
tokenSource.Cancel();
I have a test Job that runs infinitely and in the Execute
method checks the cancellation token:
public async Task Execute(IJobExecutionContext context)
{
while (true)
{
if (context.CancellationToken.IsCancellationRequested)
{
context.CancellationToken.ThrowIfCancellationRequested();
}
}
}
I would expect that when tokenSource.Cancel()
is fired the job will enter in the if
and throws the Exception. But it doesn't work.