Skip to content

Commit daa79a9

Browse files
nulltokenlinkdotnet
authored andcommitted
Ensure parameter handling is coherent
1 parent 9bbb452 commit daa79a9

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes to **NCronJob** will be documented in this file. The project
88

99
### Changed
1010

11+
- Ensure parameter handling is coherent. Fixed in [#132](https://github.com/NCronJob-Dev/NCronJob/issues/132), by [@nulltoken](https://github.com/nulltoken).
12+
1113
- 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).
1214

1315
## [3.3.4] - 2024-11-03

src/NCronJob/Registry/JobRun.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static JobRun Create(JobDefinition jobDefinition, object? parameter, Canc
4141
{
4242
JobRunId = Guid.NewGuid(),
4343
JobDefinition = jobDefinition,
44-
Parameter = parameter,
44+
Parameter = parameter ?? jobDefinition.Parameter,
4545
CancellationToken = token
4646
};
4747

tests/NCronJob.Tests/NCronJobIntegrationTests.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,63 @@ public async Task ExecuteAnInstantJob()
100100
}
101101

102102
[Fact]
103-
public async Task CronJobShouldPassDownParameter()
103+
public async Task InstantJobShouldInheritInitiallyDefinedParameter()
104104
{
105-
ServiceCollection.AddNCronJob(n => n.AddJob<ParameterJob>(p => p.WithCronExpression("* * * * *").WithParameter("Hello World")));
105+
ServiceCollection.AddNCronJob(
106+
n => n.AddJob<ParameterJob>(o => o.WithCronExpression("* * 31 2 *").WithParameter("Hello from AddNCronJob")));
107+
106108
var provider = CreateServiceProvider();
109+
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);
110+
111+
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>();
112+
113+
var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken);
114+
content.ShouldBe("Hello from AddNCronJob");
115+
}
107116

117+
[Fact]
118+
public async Task InstantJobCanOverrideInitiallyDefinedParameter()
119+
{
120+
ServiceCollection.AddNCronJob(
121+
n => n.AddJob<ParameterJob>(o => o.WithCronExpression("* * 31 2 *").WithParameter("Hello from AddNCronJob")));
122+
123+
var provider = CreateServiceProvider();
108124
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);
109125

110-
FakeTimer.Advance(TimeSpan.FromMinutes(1));
126+
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>("Hello from InstantJob");
111127

112128
var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken);
113-
content.ShouldBe("Hello World");
129+
content.ShouldBe("Hello from InstantJob");
114130
}
115131

116132
[Fact]
117-
public async Task InstantJobShouldGetParameter()
133+
public async Task InstantJobShouldPassDownParameter()
118134
{
119-
ServiceCollection.AddNCronJob(n => n.AddJob<ParameterJob>());
135+
ServiceCollection.AddNCronJob(
136+
n => n.AddJob<ParameterJob>());
137+
138+
var provider = CreateServiceProvider();
139+
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);
140+
141+
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>("Hello from InstantJob");
142+
143+
var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken);
144+
content.ShouldBe("Hello from InstantJob");
145+
}
146+
147+
[Fact]
148+
public async Task CronJobShouldInheritInitiallyDefinedParameter()
149+
{
150+
ServiceCollection.AddNCronJob(
151+
n => n.AddJob<ParameterJob>(p => p.WithCronExpression("* * * * *").WithParameter("Hello from AddNCronJob")));
120152
var provider = CreateServiceProvider();
121-
provider.GetRequiredService<IInstantJobRegistry>().RunInstantJob<ParameterJob>("Hello World");
122153

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

156+
FakeTimer.Advance(TimeSpan.FromMinutes(1));
157+
125158
var content = await CommunicationChannel.Reader.ReadAsync(CancellationToken);
126-
content.ShouldBe("Hello World");
159+
content.ShouldBe("Hello from AddNCronJob");
127160
}
128161

129162
[Fact]

0 commit comments

Comments
 (0)