-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ScheduleJob shorthand: Job name should match trigger name by default
ScheduleJob(trigger => trigger.WithIdentity()), without a job configurator, now sets the job identity to match the trigger's (instead of "DEFAULT-[UUID]"). Co-authored-by: Timo van Zijll Langhout <t.zijll@buckaroo.nl>
- Loading branch information
Showing
4 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Quartz.Tests.Unit/Extensions/DependencyInjection/ServiceCollectionExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using NUnit.Framework; | ||
|
||
namespace Quartz.Tests.Unit.Extensions.DependencyInjection | ||
{ | ||
[TestFixture()] | ||
public class ServiceCollectionExtensionsTests | ||
{ | ||
[Test] | ||
public void ScheduleJob_WithJobIdentity_ShouldHonorIt() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
// Go through AddQuartz(), because the IServiceCollectionQuartzConfigurator interface refuses mocking or implementation, due to an internal default-implemented property | ||
services.AddQuartz(quartz => quartz.ScheduleJob<DummyJob>( | ||
trigger => trigger.WithIdentity("TriggerName", "TriggerGroup"), | ||
job => job.WithIdentity("JobName", "JobGroup"))); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var quartzOptions = serviceProvider.GetRequiredService<IOptions<QuartzOptions>>().Value; | ||
|
||
Assert.That(quartzOptions.Triggers, Has.Exactly(1).Items); | ||
Assert.That(quartzOptions.JobDetails, Has.Exactly(1).Items); | ||
|
||
var trigger = quartzOptions.Triggers.Single(); | ||
var job = quartzOptions.JobDetails.Single(); | ||
|
||
// The trigger key should have its own manual configuration | ||
Assert.AreEqual("TriggerName", trigger.Key.Name); | ||
Assert.AreEqual("TriggerGroup", trigger.Key.Group); | ||
|
||
// The job key should have its own manual configuration | ||
Assert.AreEqual("JobName", job.Key.Name); | ||
Assert.AreEqual("JobGroup", job.Key.Group); | ||
|
||
// Also validate that the trigger knows the correct job key | ||
Assert.AreEqual(job.Key.Name, trigger.JobKey.Name); | ||
Assert.AreEqual(job.Key.Group, trigger.JobKey.Group); | ||
} | ||
|
||
[Test] | ||
public void ScheduleJob_WithoutJobIdentityWithoutTriggerIdentity_ShouldCopyFromTriggerIdentity() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
// Go through AddQuartz(), because the IServiceCollectionQuartzConfigurator interface refuses mocking or implementation, due to an internal default-implemented property | ||
services.AddQuartz(quartz => quartz.ScheduleJob<DummyJob>( | ||
trigger => { })); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var quartzOptions = serviceProvider.GetRequiredService<IOptions<QuartzOptions>>().Value; | ||
|
||
Assert.That(quartzOptions.Triggers, Has.Exactly(1).Items); | ||
Assert.That(quartzOptions.JobDetails, Has.Exactly(1).Items); | ||
|
||
var trigger = quartzOptions.Triggers.Single(); | ||
var job = quartzOptions.JobDetails.Single(); | ||
|
||
// The job's key should match the trigger's (auto-generated) key | ||
Assert.AreEqual(trigger.Key.Name, job.Key.Name); | ||
Assert.AreEqual(trigger.Key.Group, job.Key.Group); | ||
|
||
// Also validate that the trigger knows the correct job key | ||
Assert.AreEqual(job.Key.Name, trigger.JobKey.Name); | ||
Assert.AreEqual(job.Key.Group, trigger.JobKey.Group); | ||
} | ||
|
||
[Test] | ||
public void ScheduleJob_WithoutJobIdentityWithTriggerIdentity_ShouldCopyFromTriggerIdentity() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
// Go through AddQuartz(), because the IServiceCollectionQuartzConfigurator interface refuses mocking or implementation, due to an internal default-implemented property | ||
services.AddQuartz(quartz => quartz.ScheduleJob<DummyJob>( | ||
trigger => trigger.WithIdentity("TriggerName", "TriggerGroup"))); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
|
||
var quartzOptions = serviceProvider.GetRequiredService<IOptions<QuartzOptions>>().Value; | ||
|
||
Assert.That(quartzOptions.Triggers, Has.Exactly(1).Items); | ||
Assert.That(quartzOptions.JobDetails, Has.Exactly(1).Items); | ||
|
||
var trigger = quartzOptions.Triggers.Single(); | ||
var job = quartzOptions.JobDetails.Single(); | ||
|
||
// The trigger key should have its own manual configuration | ||
Assert.AreEqual("TriggerName", trigger.Key.Name); | ||
Assert.AreEqual("TriggerGroup", trigger.Key.Group); | ||
|
||
// The job's key should match the trigger's (auto-generated) key | ||
Assert.AreEqual(trigger.Key.Name, job.Key.Name); | ||
Assert.AreEqual(trigger.Key.Group, job.Key.Group); | ||
|
||
// Also validate that the trigger knows the correct job key | ||
Assert.AreEqual(job.Key.Name, trigger.JobKey.Name); | ||
Assert.AreEqual(job.Key.Group, trigger.JobKey.Group); | ||
} | ||
|
||
private sealed class DummyJob : IJob | ||
{ | ||
public Task Execute(IJobExecutionContext context) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters