From daa79a9630f8788720e36f6dbc339812671fdb58 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Mon, 4 Nov 2024 16:19:53 +0100 Subject: [PATCH] Ensure parameter handling is coherent --- CHANGELOG.md | 2 + src/NCronJob/Registry/JobRun.cs | 2 +- .../NCronJobIntegrationTests.cs | 49 ++++++++++++++++--- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1011a3b7..61b7200c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ All notable changes to **NCronJob** will be documented in this file. The project ### Changed +- Ensure parameter handling is coherent. Fixed in [#132](https://github.com/NCronJob-Dev/NCronJob/issues/132), by [@nulltoken](https://github.com/nulltoken). + - Prevent `InstantJobRegistry` from altering the content of `JobRegistry`. Fixed in [#131](https://github.com/NCronJob-Dev/NCronJob/issues/131), by [@nulltoken](https://github.com/nulltoken). ## [3.3.4] - 2024-11-03 diff --git a/src/NCronJob/Registry/JobRun.cs b/src/NCronJob/Registry/JobRun.cs index 41fdb43e..829a9665 100644 --- a/src/NCronJob/Registry/JobRun.cs +++ b/src/NCronJob/Registry/JobRun.cs @@ -41,7 +41,7 @@ public static JobRun Create(JobDefinition jobDefinition, object? parameter, Canc { JobRunId = Guid.NewGuid(), JobDefinition = jobDefinition, - Parameter = parameter, + Parameter = parameter ?? jobDefinition.Parameter, CancellationToken = token }; diff --git a/tests/NCronJob.Tests/NCronJobIntegrationTests.cs b/tests/NCronJob.Tests/NCronJobIntegrationTests.cs index 65539ef0..07f608d7 100644 --- a/tests/NCronJob.Tests/NCronJobIntegrationTests.cs +++ b/tests/NCronJob.Tests/NCronJobIntegrationTests.cs @@ -100,30 +100,63 @@ public async Task ExecuteAnInstantJob() } [Fact] - public async Task CronJobShouldPassDownParameter() + public async Task InstantJobShouldInheritInitiallyDefinedParameter() { - ServiceCollection.AddNCronJob(n => n.AddJob(p => p.WithCronExpression("* * * * *").WithParameter("Hello World"))); + ServiceCollection.AddNCronJob( + n => n.AddJob(o => o.WithCronExpression("* * 31 2 *").WithParameter("Hello from AddNCronJob"))); + var provider = CreateServiceProvider(); + await provider.GetRequiredService().StartAsync(CancellationToken); + + provider.GetRequiredService().RunInstantJob(); + + var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken); + content.ShouldBe("Hello from AddNCronJob"); + } + [Fact] + public async Task InstantJobCanOverrideInitiallyDefinedParameter() + { + ServiceCollection.AddNCronJob( + n => n.AddJob(o => o.WithCronExpression("* * 31 2 *").WithParameter("Hello from AddNCronJob"))); + + var provider = CreateServiceProvider(); await provider.GetRequiredService().StartAsync(CancellationToken); - FakeTimer.Advance(TimeSpan.FromMinutes(1)); + provider.GetRequiredService().RunInstantJob("Hello from InstantJob"); var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken); - content.ShouldBe("Hello World"); + content.ShouldBe("Hello from InstantJob"); } [Fact] - public async Task InstantJobShouldGetParameter() + public async Task InstantJobShouldPassDownParameter() { - ServiceCollection.AddNCronJob(n => n.AddJob()); + ServiceCollection.AddNCronJob( + n => n.AddJob()); + + var provider = CreateServiceProvider(); + await provider.GetRequiredService().StartAsync(CancellationToken); + + provider.GetRequiredService().RunInstantJob("Hello from InstantJob"); + + var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken); + content.ShouldBe("Hello from InstantJob"); + } + + [Fact] + public async Task CronJobShouldInheritInitiallyDefinedParameter() + { + ServiceCollection.AddNCronJob( + n => n.AddJob(p => p.WithCronExpression("* * * * *").WithParameter("Hello from AddNCronJob"))); var provider = CreateServiceProvider(); - provider.GetRequiredService().RunInstantJob("Hello World"); await provider.GetRequiredService().StartAsync(CancellationToken); + FakeTimer.Advance(TimeSpan.FromMinutes(1)); + var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken); - content.ShouldBe("Hello World"); + content.ShouldBe("Hello from AddNCronJob"); } [Fact]