public void SetNextRun() { var schedule = new Dictionary <DayOfWeek, List <TimeSpan> >(); schedule.Add(DayOfWeek.Monday, new List <TimeSpan>() { TimeSpan.Parse("12:00"), TimeSpan.Parse("15:00"), TimeSpan.Parse("18:00") }); schedule.Add(DayOfWeek.Wednesday, new List <TimeSpan>() { TimeSpan.Parse("12:00"), TimeSpan.Parse("15:00"), TimeSpan.Parse("18:00") }); schedule.Add(DayOfWeek.Thursday, new List <TimeSpan>() { TimeSpan.Parse("12:00"), TimeSpan.Parse("15:00"), TimeSpan.Parse("18:00") }); var job = new WhenDoJob(); job.Schedule = schedule; job.SetNextRun(DateTimeOffset.Parse("2018-03-08 13:45")); Assert.AreEqual(DateTimeOffset.Parse("2018-03-08 15:00:00"), job.NextRun); job.SetNextRun(DateTimeOffset.Parse("2018-03-08 19:45")); Assert.AreEqual(DateTimeOffset.Parse("2018-03-12 12:00:00"), job.NextRun); job.SetNextRun(DateTimeOffset.Parse("2018-03-09 13:45")); Assert.AreEqual(DateTimeOffset.Parse("2018-03-12 12:00:00"), job.NextRun); job.SetNextRun(DateTimeOffset.Parse("2018-03-15 13:45")); Assert.AreEqual(DateTimeOffset.Parse("2018-03-15 15:00:00"), job.NextRun); }
public void SetNextRunWhenNoScheduledDays() { var schedule = new Dictionary <DayOfWeek, List <TimeSpan> >(); var job = new WhenDoJob(); job.Schedule = schedule; Assert.ThrowsException <ArgumentOutOfRangeException>(() => job.SetNextRun(DateTimeOffset.Parse("2018-03-08 13:45"))); }
public void ShouldOrShouldNotRun() { var job = new WhenDoJob(); job.NextRun = DateTimeOffset.Parse("2018-03-08 16:00:00"); Assert.IsTrue(job.ShouldRun(DateTimeOffset.Parse("2018-03-08 16:04:00"))); Assert.IsFalse(job.ShouldRun(DateTimeOffset.Parse("2018-03-08 15:59:00"))); Assert.IsTrue(job.ShouldRun(DateTimeOffset.Parse("2018-03-08 17:00:00"))); }
public void TestConditionEvaluation() { var dtpMock = new Mock <DateTimeProvider>(); dtpMock.Setup(x => x.CurrentTime).Returns(new TimeSpan(23, 55, 0)); var dtpMock2 = new Mock <IDateTimeProvider>(); dtpMock2.Setup(x => x.CurrentTime).Returns(new TimeSpan(15, 55, 0)); var registry = new Mock <IWhenDoRegistry>(); registry.Setup(x => x.GetExpressionProviderInstance("DateTimeProvider")).Returns(dtpMock.As <IWhenDoExpressionProvider>().Object); var hangfire = new Mock <IBackgroundJobClient>(); var repository = new MemoryJobRepository(); var manager = new WhenDoJobExecutionManager(dtpMock2.Object, registry.Object, MockHelper.CreateLogger <WhenDoJobExecutionManager>(), repository, hangfire.Object); var condition = "@msg.DoubleValue > 15.3 AND @msg.StringValue = \"Livingroom\" AND @dtp.CurrentTime > \"23:00\""; var job = new WhenDoJob(); job.ConditionProviders = new List <ExpressionProviderInfo>() { new ExpressionProviderInfo() { ShortName = "msg", FullName = "TestMessage", ProviderType = typeof(TestMessage) }, new ExpressionProviderInfo() { ShortName = "dtp", FullName = "DateTime", ProviderType = typeof(DateTimeProvider) } }; job.Condition = WhenDoHelpers.ParseExpression <bool>(condition, job.ConditionProviders); var message = new TestMessage() { DoubleValue = 15.0D, IntValue = 20, StringValue = "Livingroom" }; Assert.IsFalse(manager.IsRunnable(job, message)); message = new TestMessage() { DoubleValue = 15.50D, IntValue = 20, StringValue = "Livingroom" }; Assert.IsTrue(manager.IsRunnable(job, message)); message = new TestMessage() { DoubleValue = 15.50D, IntValue = 20, StringValue = "Living room" }; Assert.IsFalse(manager.IsRunnable(job, message)); }
public virtual IWhenDoJob CreateJobFromDefinition(JobDefinition definition) { var providers = GetProviderInfoList(definition.Providers); var job = new WhenDoJob() { Id = definition.Id, Version = definition.Version, Disabled = definition.Disabled, DisabledFrom = definition.DisabledFrom, DisabledTill = definition.DisabledTill, ConditionProviders = providers, Schedule = definition.Schedule.ToWhenDoSchedule(), Type = (definition.Schedule == null) ? JobType.Message : JobType.Scheduled, Condition = WhenDoHelpers.ParseExpression <bool>(definition.When, providers), Commands = definition.Do.Select(x => CreateCommandFromDefinition(x, providers)).ToList() }; return(job); }
public void IsRunnableOutsideDisabledTimeFrame() { var dtpMock = MockHelper.CreateDateTimeProviderMock(); dtpMock.Setup(x => x.CurrentTime).Returns(new TimeSpan(15, 55, 0)); var registry = new Mock <IWhenDoRegistry>(); var hangfire = new Mock <IBackgroundJobClient>(); var repository = new MemoryJobRepository(); var manager = new WhenDoJobExecutionManager(dtpMock.Object, registry.Object, MockHelper.CreateLogger <WhenDoJobExecutionManager>(), repository, hangfire.Object); var job = new WhenDoJob(); job.Condition = WhenDoHelpers.ParseExpression <bool>("true", null); job.DisabledFrom = new TimeSpan(10, 0, 0); job.DisabledTill = new TimeSpan(14, 0, 0); Assert.IsTrue(manager.IsRunnable(job, null)); dtpMock.Verify(x => x.CurrentTime, Times.Once); }
public void IsNotRunnableWhenDisabled() { var dtpMock = MockHelper.CreateDateTimeProviderMock(); dtpMock.Setup(x => x.CurrentTime).Returns(new TimeSpan(11, 55, 0)); var registry = new Mock <IWhenDoRegistry>(); var hangfire = new Mock <IBackgroundJobClient>(); var repository = new MemoryJobRepository(); var manager = new WhenDoJobExecutionManager(dtpMock.Object, registry.Object, MockHelper.CreateLogger <WhenDoJobExecutionManager>(), repository, hangfire.Object); var job = new WhenDoJob(); job.Condition = WhenDoHelpers.ParseExpression <bool>("true", null); job.Disabled = true; Assert.IsFalse(manager.IsRunnable(job, null)); job.Disabled = false; Assert.IsTrue(manager.IsRunnable(job, null)); }
public void SetNextRunShouldBeTomorrow() { var def = new List <ScheduleDefinition>() { new ScheduleDefinition() { Days = new List <string>() { "any" }, TimesOfDay = new List <string>() { "16:00" } } }; var schedule = def.ToWhenDoSchedule(); var job = new WhenDoJob() { Schedule = schedule }; job.SetNextRun(DateTimeOffset.Parse("2018-03-15 19:46")); Assert.AreEqual(DateTimeOffset.Parse("2018-03-16 16:00"), job.NextRun); }