Skip to content

Commit

Permalink
Ensure parameter handling is coherent
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken authored and linkdotnet committed Nov 4, 2024
1 parent 9bbb452 commit daa79a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/NCronJob/Registry/JobRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
49 changes: 41 additions & 8 deletions tests/NCronJob.Tests/NCronJobIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,63 @@ public async Task ExecuteAnInstantJob()
}

[Fact]
public async Task CronJobShouldPassDownParameter()
public async Task InstantJobShouldInheritInitiallyDefinedParameter()
{
ServiceCollection.AddNCronJob(n => n.AddJob<ParameterJob>(p => p.WithCronExpression("* * * * *").WithParameter("Hello World")));
ServiceCollection.AddNCronJob(
n => n.AddJob<ParameterJob>(o => o.WithCronExpression("* * 31 2 *").WithParameter("Hello from AddNCronJob")));

var provider = CreateServiceProvider();
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);

provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>();

var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken);
content.ShouldBe("Hello from AddNCronJob");
}

[Fact]
public async Task InstantJobCanOverrideInitiallyDefinedParameter()
{
ServiceCollection.AddNCronJob(
n => n.AddJob<ParameterJob>(o => o.WithCronExpression("* * 31 2 *").WithParameter("Hello from AddNCronJob")));

var provider = CreateServiceProvider();
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);

FakeTimer.Advance(TimeSpan.FromMinutes(1));
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>("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<ParameterJob>());
ServiceCollection.AddNCronJob(
n => n.AddJob<ParameterJob>());

var provider = CreateServiceProvider();
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);

provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>("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<ParameterJob>(p => p.WithCronExpression("* * * * *").WithParameter("Hello from AddNCronJob")));
var provider = CreateServiceProvider();
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>("Hello World");

await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);

FakeTimer.Advance(TimeSpan.FromMinutes(1));

var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken);
content.ShouldBe("Hello World");
content.ShouldBe("Hello from AddNCronJob");
}

[Fact]
Expand Down

0 comments on commit daa79a9

Please sign in to comment.