|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +using Bet.Extensions.Testing.Logging; |
| 6 | + |
| 7 | +using CronScheduler.Extensions.Internal; |
| 8 | +using CronScheduler.Extensions.Scheduler; |
| 9 | + |
| 10 | +using Microsoft.Extensions.Configuration; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.Extensions.Hosting; |
| 13 | +using Microsoft.Extensions.Logging; |
| 14 | + |
| 15 | +using Xunit; |
| 16 | +using Xunit.Abstractions; |
| 17 | + |
| 18 | +namespace CronScheduler.UnitTest |
| 19 | +{ |
| 20 | + public class SchedulerRegistrationTests |
| 21 | + { |
| 22 | + private ITestOutputHelper _output; |
| 23 | + |
| 24 | + public SchedulerRegistrationTests(ITestOutputHelper output) |
| 25 | + { |
| 26 | + _output = output ?? throw new ArgumentNullException(nameof(output)); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task Successfully_Register_Two_Jobs_With_The_Same_Type() |
| 31 | + { |
| 32 | + var configuration = new ConfigurationBuilder().AddInMemoryCollection().Build(); |
| 33 | + |
| 34 | + var services = new ServiceCollection(); |
| 35 | + |
| 36 | + services.AddSingleton<IConfiguration>(configuration); |
| 37 | + |
| 38 | + services.AddLogging(builder => |
| 39 | + { |
| 40 | + builder.AddDebug(); |
| 41 | + builder.AddXunit(_output, LogLevel.Debug); |
| 42 | + }); |
| 43 | + |
| 44 | + services.AddScheduler(); |
| 45 | + services.AddHostedService<SchedulerHostedService>(); |
| 46 | + |
| 47 | + var sp = services.BuildServiceProvider(); |
| 48 | + var schedulerRegistration = sp.GetRequiredService<ISchedulerRegistration>(); |
| 49 | + var loggerFactory = sp.GetRequiredService<ILoggerFactory>(); |
| 50 | + |
| 51 | + var options1 = new CustomTestJobOptions |
| 52 | + { |
| 53 | + JobName = "Job1", |
| 54 | + CronSchedule = "0/2 * * * * *", |
| 55 | + RunImmediately = false, |
| 56 | + DisplayText = "Every 2 seconds." |
| 57 | + }; |
| 58 | + |
| 59 | + var options2 = new CustomTestJobOptions |
| 60 | + { |
| 61 | + JobName = "Job2", |
| 62 | + CronSchedule = "0/4 * * * * *", |
| 63 | + RunImmediately = false, |
| 64 | + DisplayText = "Every 4 seconds." |
| 65 | + }; |
| 66 | + |
| 67 | + schedulerRegistration.AddOrUpdate(new CustomTestJob(options1, loggerFactory.CreateLogger<CustomTestJob>()), options1); |
| 68 | + schedulerRegistration.AddOrUpdate(new CustomTestJob(options2, loggerFactory.CreateLogger<CustomTestJob>()), options2); |
| 69 | + |
| 70 | + var backgroundService = sp.GetService<IHostedService>() as SchedulerHostedService; |
| 71 | + await backgroundService.StartAsync(CancellationToken.None); |
| 72 | + |
| 73 | + await Task.Delay(TimeSpan.FromSeconds(15)); |
| 74 | + |
| 75 | + await backgroundService.StopAsync(CancellationToken.None); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments