-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ShutdownJob interface for methods executed after scheduler
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace AsyncScheduler | ||
{ | ||
/// <summary> | ||
/// Interface which can be implemented by a Job, | ||
/// so its shutdown method is executed, when scheduler is stopped by CancellationToken. | ||
/// </summary> | ||
public interface IShutdownJob : IJob | ||
{ | ||
|
||
/// <summary> | ||
/// Method is executed on shutdown of Scheduler/Job independent whether this job was started before or not. | ||
/// It is allowed to throw an exception as they are caught. | ||
/// </summary> | ||
/// <returns>message for Log</returns> | ||
Task<string> Shutdown(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AsyncScheduler; | ||
using AsyncScheduler.JobStorage; | ||
using AsyncScheduler.Schedules; | ||
using AsyncSchedulerTest.TestData; | ||
using AsyncSchedulerTest.TestUtils; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace AsyncSchedulerTest | ||
{ | ||
public class ShutdownJobTest | ||
{ | ||
private readonly Scheduler _scheduler; | ||
private readonly ShutdownJob _shutdownJobInstancee; | ||
|
||
public ShutdownJobTest(ITestOutputHelper testOutputHelper) | ||
{ | ||
_shutdownJobInstancee = new ShutdownJob(); | ||
var serviceProvider = new TestActivator(null, _shutdownJobInstancee); | ||
var jobManager = new JobManager(serviceProvider, new XUnitLogger<JobManager>(testOutputHelper), | ||
new InMemoryStorage()); | ||
_scheduler = new Scheduler(serviceProvider, new XUnitLogger<Scheduler>(testOutputHelper), | ||
new UtcSchedulerClock(), jobManager); | ||
} | ||
|
||
[Fact] | ||
public async Task TestShutdown() | ||
{ | ||
_scheduler.JobManager.AddJob<ShutdownJob, ScheduleNever>(); | ||
await RunScheduler(TimeSpan.FromSeconds(1)); | ||
_shutdownJobInstancee.ShutdownCount.Should().Be(1); | ||
} | ||
|
||
private async Task RunScheduler(TimeSpan schedulerTime) | ||
{ | ||
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); | ||
var schedulerTask = _scheduler.Start(cancellationTokenSource.Token); | ||
// ReSharper disable MethodSupportsCancellation | ||
await Task.Delay(schedulerTime).ContinueWith((t) => cancellationTokenSource.Cancel()); | ||
var schedulerFinishTimeout = TimeSpan.FromSeconds(1); | ||
await Task.WhenAny(schedulerTask, Task.Delay(schedulerFinishTimeout)); | ||
cancellationTokenSource.Cancel(); | ||
await schedulerTask; | ||
} | ||
} | ||
} |
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,22 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AsyncScheduler; | ||
|
||
namespace AsyncSchedulerTest.TestData | ||
{ | ||
public class ShutdownJob : IShutdownJob | ||
{ | ||
public int ShutdownCount { get; private set; } = 0; | ||
|
||
public async Task<object> Start(CancellationToken cancellationToken) | ||
{ | ||
return await Task.FromResult("Done"); | ||
} | ||
|
||
public Task<string> Shutdown() | ||
{ | ||
ShutdownCount++; | ||
return Task.FromResult("Shutdown executed"); | ||
} | ||
} | ||
} |
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