diff --git a/ConsoleTester/Program.cs b/ConsoleTester/Program.cs index 5d42aaf5..e59d3133 100644 --- a/ConsoleTester/Program.cs +++ b/ConsoleTester/Program.cs @@ -11,37 +11,37 @@ class Program static void Main(string[] args) { Console.WriteLine("Which the test you'd like to run (enter the test number):"); - Console.WriteLine("1. DelayFor"); - Console.WriteLine("2. MiscTests (everything else)"); - - byte testNum; - if (byte.TryParse(Console.ReadLine(), out testNum)) - { - // which test to run? - switch (testNum) - { - case 1: // DelayFor - DelayForTest(); - break; - case 2: // MiscTests - MiscTests(); - break; - default: - Console.WriteLine(string.Format("There's not test #{0}", testNum)); - return; - } - } - else - { - MiscTests(); - } + Console.WriteLine("1. DelayFor"); + Console.WriteLine("2. MiscTests (everything else)"); + + byte testNum; + if (byte.TryParse(Console.ReadLine(), out testNum)) + { + // which test to run? + switch (testNum) + { + case 1: // DelayFor + DelayForTest(); + break; + case 2: // MiscTests + MiscTests(); + break; + default: + Console.WriteLine(string.Format("There's not test #{0}", testNum)); + return; + } + } + else + { + MiscTests(); + } Console.ReadKey(); } - static void DelayForTest() - { - Console.WriteLine("Testing DelayFor..."); + static void DelayForTest() + { + Console.WriteLine("Testing DelayFor..."); TaskManager.AddTask(() => Console.WriteLine("ToRunNow() - not delayed: " + DateTime.Now), x => x.ToRunNow()); TaskManager.AddTask(() => Console.WriteLine("ToRunNow() - delayed 2 sec: " + DateTime.Now), x => x.ToRunNow().DelayFor(2).Seconds()); @@ -49,59 +49,59 @@ static void DelayForTest() TaskManager.AddTask(() => Console.WriteLine("ToRunOnceAt() - delayed 2 sec: " + DateTime.Now), x => x.ToRunOnceAt(DateTime.Now).DelayFor(2).Seconds()); TaskManager.AddTask(() => Console.WriteLine("ToRunEvery() - not delayed: " + DateTime.Now), x => x.ToRunEvery(2).Seconds()); TaskManager.AddTask(() => Console.WriteLine("ToRunEvery() - delayed 2 sec: " + DateTime.Now), x => x.ToRunEvery(2).Seconds().DelayFor(2).Seconds()); - - - //TaskManager.AddTask(() => Console.WriteLine("recurring, not delayed: " + DateTime.Now), x => x.ToRunNow().DelayFor(3).Seconds()); - //TaskManager.AddTask(() => Console.WriteLine("Inline task (delayed 5 sec): " + DateTime.Now), x => x.ToRunOnceAt(DateTime.Now).DelayFor(5).Seconds()); - } - - static void MiscTests() - { - TaskManager.TaskFactory = new MyTaskFactory(); - TaskManager.TaskStart += (schedule, e) => Console.WriteLine(schedule.Name + " Started: " + schedule.StartTime); - TaskManager.TaskEnd += (schedule, e) => Console.WriteLine(schedule.Name + " Ended.\n\tStarted: " + schedule.StartTime + "\n\tDuration: " + schedule.Duration + "\n\tNext run: " + schedule.NextRunTime); - - TaskManager.Initialize(new MyRegistry()); - Console.WriteLine("Done initializing..."); - - // try to get the named schedule registered inside MyRegistry - FluentScheduler.Model.Schedule named = TaskManager.GetSchedule("named task"); - if (named != null) - { - // success, execute it manually - named.Execute(); - } - - FluentScheduler.Model.Schedule removable = TaskManager.GetSchedule("removable task"); - if (removable != null) - { - Console.WriteLine("before remove"); - TaskManager.RemoveTask(removable.Name); - Console.WriteLine("after remove"); - } - - FluentScheduler.Model.Schedule longRemovable = TaskManager.GetSchedule("long removable task"); - if (longRemovable != null) - { - Console.WriteLine("before remove long running"); - TaskManager.RemoveTask(longRemovable.Name); - Console.WriteLine("after remove long running"); - } - - //Thread.Sleep(10000); - //TaskManager.Stop(); - - /* TaskManager.AddTask(() => Console.WriteLine("Inline task: " + DateTime.Now), x => x.ToRunEvery(15).Seconds()); - TaskManager.AddTask(() => Console.WriteLine("Inline task (once): " + DateTime.Now), x => x.ToRunOnceAt(DateTime.Now.AddSeconds(5))); - - TaskManager.AddTask(x => x.ToRunNow()); - */ - TaskManager.UnobservedTaskException += TaskManager_UnobservedTaskException; - /* TaskManager.AddTask(() => { - Console.WriteLine("Inline task: " + DateTime.Now); - throw new Exception("Hi"); }, x => x.ToRunNow()); - */ - } + + + //TaskManager.AddTask(() => Console.WriteLine("recurring, not delayed: " + DateTime.Now), x => x.ToRunNow().DelayFor(3).Seconds()); + //TaskManager.AddTask(() => Console.WriteLine("Inline task (delayed 5 sec): " + DateTime.Now), x => x.ToRunOnceAt(DateTime.Now).DelayFor(5).Seconds()); + } + + static void MiscTests() + { + TaskManager.TaskFactory = new MyTaskFactory(); + TaskManager.TaskStart += (schedule, e) => Console.WriteLine(schedule.Name + " Started: " + schedule.StartTime); + TaskManager.TaskEnd += (schedule, e) => Console.WriteLine(schedule.Name + " Ended.\n\tStarted: " + schedule.StartTime + "\n\tDuration: " + schedule.Duration + "\n\tNext run: " + schedule.NextRunTime); + + TaskManager.Initialize(new MyRegistry()); + Console.WriteLine("Done initializing..."); + + // try to get the named schedule registered inside MyRegistry + FluentScheduler.Model.Schedule named = TaskManager.GetSchedule("named task"); + if (named != null) + { + // success, execute it manually + named.Execute(); + } + + FluentScheduler.Model.Schedule removable = TaskManager.GetSchedule("removable task"); + if (removable != null) + { + Console.WriteLine("before remove"); + TaskManager.RemoveTask(removable.Name); + Console.WriteLine("after remove"); + } + + FluentScheduler.Model.Schedule longRemovable = TaskManager.GetSchedule("long removable task"); + if (longRemovable != null) + { + Console.WriteLine("before remove long running"); + TaskManager.RemoveTask(longRemovable.Name); + Console.WriteLine("after remove long running"); + } + + //Thread.Sleep(10000); + //TaskManager.Stop(); + + /* TaskManager.AddTask(() => Console.WriteLine("Inline task: " + DateTime.Now), x => x.ToRunEvery(15).Seconds()); + TaskManager.AddTask(() => Console.WriteLine("Inline task (once): " + DateTime.Now), x => x.ToRunOnceAt(DateTime.Now.AddSeconds(5))); + + TaskManager.AddTask(x => x.ToRunNow()); + */ + TaskManager.UnobservedTaskException += TaskManager_UnobservedTaskException; + /* TaskManager.AddTask(() => { + Console.WriteLine("Inline task: " + DateTime.Now); + throw new Exception("Hi"); }, x => x.ToRunNow()); + */ + } static void TaskManager_UnobservedTaskException(TaskExceptionInformation sender, UnhandledExceptionEventArgs e) { diff --git a/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunEvery_Tests.cs b/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunEvery_Tests.cs index 36835fb6..207ae34d 100644 --- a/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunEvery_Tests.cs +++ b/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunEvery_Tests.cs @@ -5,79 +5,79 @@ namespace FluentScheduler.Tests.ScheduleTests { - [TestFixture] - public class DelayFor_ToRunEvery_Tests - { - [Test] - public void Should_Delay_ToRunEvery_For_2_Seconds() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Seconds").ToRunEvery(10).Seconds().DelayFor(2).Seconds()); - DateTime expectedTime = DateTime.Now.AddSeconds(12); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Seconds").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunEvery_For_2_Minutes() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Minutes").ToRunEvery(10).Seconds().DelayFor(2).Minutes()); - DateTime expectedTime = DateTime.Now.AddSeconds(10).AddMinutes(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Minutes").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunEvery_For_2_Hours() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Hours").ToRunEvery(10).Seconds().DelayFor(2).Hours()); - DateTime expectedTime = DateTime.Now.AddSeconds(10).AddHours(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Hours").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunEvery_For_2_Days() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Days").ToRunEvery(10).Seconds().DelayFor(2).Days()); - DateTime expectedTime = DateTime.Now.AddSeconds(10).AddDays(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Days").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunEvery_For_2_Weeks() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Weeks").ToRunEvery(10).Seconds().DelayFor(2).Weeks()); - DateTime expectedTime = DateTime.Now.AddSeconds(10).AddDays(14); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Weeks").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunEvery_For_2_Months() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Months").ToRunEvery(10).Seconds().DelayFor(2).Months()); - DateTime expectedTime = DateTime.Now.AddSeconds(10).AddMonths(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Months").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunEvery_For_2_Years() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Years").ToRunEvery(10).Seconds().DelayFor(2).Years()); - DateTime expectedTime = DateTime.Now.AddSeconds(10).AddYears(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Years").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - - } + [TestFixture] + public class DelayFor_ToRunEvery_Tests + { + [Test] + public void Should_Delay_ToRunEvery_For_2_Seconds() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Seconds").ToRunEvery(10).Seconds().DelayFor(2).Seconds()); + DateTime expectedTime = DateTime.Now.AddSeconds(12); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Seconds").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunEvery_For_2_Minutes() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Minutes").ToRunEvery(10).Seconds().DelayFor(2).Minutes()); + DateTime expectedTime = DateTime.Now.AddSeconds(10).AddMinutes(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Minutes").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunEvery_For_2_Hours() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Hours").ToRunEvery(10).Seconds().DelayFor(2).Hours()); + DateTime expectedTime = DateTime.Now.AddSeconds(10).AddHours(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Hours").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunEvery_For_2_Days() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Days").ToRunEvery(10).Seconds().DelayFor(2).Days()); + DateTime expectedTime = DateTime.Now.AddSeconds(10).AddDays(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Days").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunEvery_For_2_Weeks() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Weeks").ToRunEvery(10).Seconds().DelayFor(2).Weeks()); + DateTime expectedTime = DateTime.Now.AddSeconds(10).AddDays(14); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Weeks").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunEvery_For_2_Months() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Months").ToRunEvery(10).Seconds().DelayFor(2).Months()); + DateTime expectedTime = DateTime.Now.AddSeconds(10).AddMonths(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Months").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunEvery_For_2_Years() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunEvery_For_2_Years").ToRunEvery(10).Seconds().DelayFor(2).Years()); + DateTime expectedTime = DateTime.Now.AddSeconds(10).AddYears(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunEvery_For_2_Years").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + + } } diff --git a/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunNow_Tests.cs b/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunNow_Tests.cs index 45022727..fe8bf14b 100644 --- a/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunNow_Tests.cs +++ b/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunNow_Tests.cs @@ -5,79 +5,79 @@ namespace FluentScheduler.Tests.ScheduleTests { - [TestFixture] - public class DelayFor_ToRunOnceAt_Tests - { - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Seconds() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Seconds").ToRunOnceAt(DateTime.Now).DelayFor(2).Seconds()); - DateTime expectedTime = DateTime.Now.AddSeconds(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Seconds").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Minutes() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Minutes").ToRunOnceAt(DateTime.Now).DelayFor(2).Minutes()); - DateTime expectedTime = DateTime.Now.AddMinutes(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Minutes").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Hours() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Hours").ToRunOnceAt(DateTime.Now).DelayFor(2).Hours()); - DateTime expectedTime = DateTime.Now.AddHours(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Hours").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Days() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Days").ToRunOnceAt(DateTime.Now).DelayFor(2).Days()); - DateTime expectedTime = DateTime.Now.AddDays(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Days").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Weeks() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Weeks").ToRunOnceAt(DateTime.Now).DelayFor(2).Weeks()); - DateTime expectedTime = DateTime.Now.AddDays(14); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Weeks").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Months() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Months").ToRunOnceAt(DateTime.Now).DelayFor(2).Months()); - DateTime expectedTime = DateTime.Now.AddMonths(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Months").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunOnceAt_For_2_Years() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Years").ToRunOnceAt(DateTime.Now).DelayFor(2).Years()); - DateTime expectedTime = DateTime.Now.AddYears(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Years").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - - } + [TestFixture] + public class DelayFor_ToRunOnceAt_Tests + { + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Seconds() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Seconds").ToRunOnceAt(DateTime.Now).DelayFor(2).Seconds()); + DateTime expectedTime = DateTime.Now.AddSeconds(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Seconds").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Minutes() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Minutes").ToRunOnceAt(DateTime.Now).DelayFor(2).Minutes()); + DateTime expectedTime = DateTime.Now.AddMinutes(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Minutes").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Hours() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Hours").ToRunOnceAt(DateTime.Now).DelayFor(2).Hours()); + DateTime expectedTime = DateTime.Now.AddHours(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Hours").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Days() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Days").ToRunOnceAt(DateTime.Now).DelayFor(2).Days()); + DateTime expectedTime = DateTime.Now.AddDays(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Days").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Weeks() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Weeks").ToRunOnceAt(DateTime.Now).DelayFor(2).Weeks()); + DateTime expectedTime = DateTime.Now.AddDays(14); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Weeks").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Months() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Months").ToRunOnceAt(DateTime.Now).DelayFor(2).Months()); + DateTime expectedTime = DateTime.Now.AddMonths(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Months").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunOnceAt_For_2_Years() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunOnceAt_For_2_Years").ToRunOnceAt(DateTime.Now).DelayFor(2).Years()); + DateTime expectedTime = DateTime.Now.AddYears(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunOnceAt_For_2_Years").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + + } } diff --git a/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunOnceAt_Tests.cs b/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunOnceAt_Tests.cs index aee5874c..0451254f 100644 --- a/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunOnceAt_Tests.cs +++ b/FluentScheduler.Tests/ScheduleTests/DelayFor_ToRunOnceAt_Tests.cs @@ -5,79 +5,79 @@ namespace FluentScheduler.Tests.ScheduleTests { - [TestFixture] - public class DelayFor_ToRunNow_Tests - { - [Test] - public void Should_Delay_ToRunNow_For_2_Seconds() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Seconds").ToRunNow().DelayFor(2).Seconds()); - DateTime expectedTime = DateTime.Now.AddSeconds(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Seconds").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunNow_For_2_Minutes() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Minutes").ToRunNow().DelayFor(2).Minutes()); - DateTime expectedTime = DateTime.Now.AddMinutes(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Minutes").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunNow_For_2_Hours() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Hours").ToRunNow().DelayFor(2).Hours()); - DateTime expectedTime = DateTime.Now.AddHours(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Hours").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunNow_For_2_Days() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Days").ToRunNow().DelayFor(2).Days()); - DateTime expectedTime = DateTime.Now.AddDays(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Days").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunNow_For_2_Weeks() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Weeks").ToRunNow().DelayFor(2).Weeks()); - DateTime expectedTime = DateTime.Now.AddDays(14); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Weeks").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunNow_For_2_Months() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Months").ToRunNow().DelayFor(2).Months()); - DateTime expectedTime = DateTime.Now.AddMonths(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Months").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - [Test] - public void Should_Delay_ToRunNow_For_2_Years() - { - TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Years").ToRunNow().DelayFor(2).Years()); - DateTime expectedTime = DateTime.Now.AddYears(2); - - DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Years").NextRunTime; - - Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); - } - - } + [TestFixture] + public class DelayFor_ToRunNow_Tests + { + [Test] + public void Should_Delay_ToRunNow_For_2_Seconds() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Seconds").ToRunNow().DelayFor(2).Seconds()); + DateTime expectedTime = DateTime.Now.AddSeconds(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Seconds").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunNow_For_2_Minutes() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Minutes").ToRunNow().DelayFor(2).Minutes()); + DateTime expectedTime = DateTime.Now.AddMinutes(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Minutes").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunNow_For_2_Hours() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Hours").ToRunNow().DelayFor(2).Hours()); + DateTime expectedTime = DateTime.Now.AddHours(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Hours").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunNow_For_2_Days() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Days").ToRunNow().DelayFor(2).Days()); + DateTime expectedTime = DateTime.Now.AddDays(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Days").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunNow_For_2_Weeks() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Weeks").ToRunNow().DelayFor(2).Weeks()); + DateTime expectedTime = DateTime.Now.AddDays(14); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Weeks").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunNow_For_2_Months() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Months").ToRunNow().DelayFor(2).Months()); + DateTime expectedTime = DateTime.Now.AddMonths(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Months").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + [Test] + public void Should_Delay_ToRunNow_For_2_Years() + { + TaskManager.AddTask(() => { }, x => x.WithName("Should_Delay_ToRunNow_For_2_Years").ToRunNow().DelayFor(2).Years()); + DateTime expectedTime = DateTime.Now.AddYears(2); + + DateTime actualTime = TaskManager.GetSchedule("Should_Delay_ToRunNow_For_2_Years").NextRunTime; + + Assert.AreEqual(Math.Floor(expectedTime.TimeOfDay.TotalSeconds), Math.Floor(actualTime.TimeOfDay.TotalSeconds)); + } + + } } diff --git a/FluentScheduler/Extensions/DelayForExtensions.cs b/FluentScheduler/Extensions/DelayForExtensions.cs index 623cdb5e..2d05a4e4 100644 --- a/FluentScheduler/Extensions/DelayForExtensions.cs +++ b/FluentScheduler/Extensions/DelayForExtensions.cs @@ -6,68 +6,68 @@ namespace FluentScheduler /// /// Extensions for DelayFor() functionality /// - public static class DelayForExtensions + public static class DelayForExtensions { - private static DelayTimeUnit DelayFor(Schedule schedule, int interval) - { - return new DelayTimeUnit(schedule, interval); - } - /// - /// Delay first execution of the task for the specified time interval. - /// + private static DelayTimeUnit DelayFor(Schedule schedule, int interval) + { + return new DelayTimeUnit(schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// public static DelayTimeUnit DelayFor(this SpecificRunTime runTime, int interval) { - return DelayFor(runTime.Schedule, interval); + return DelayFor(runTime.Schedule, interval); } - /// - /// Delay first execution of the task for the specified time interval. - /// + /// + /// Delay first execution of the task for the specified time interval. + /// public static DelayTimeUnit DelayFor(this SecondUnit timeUnit, int interval) { - return DelayFor(timeUnit.Schedule, interval); + return DelayFor(timeUnit.Schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// + public static DelayTimeUnit DelayFor(this MinuteUnit timeUnit, int interval) + { + return DelayFor(timeUnit.Schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// + public static DelayTimeUnit DelayFor(this HourUnit timeUnit, int interval) + { + return DelayFor(timeUnit.Schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// + public static DelayTimeUnit DelayFor(this DayUnit timeUnit, int interval) + { + return DelayFor(timeUnit.Schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// + public static DelayTimeUnit DelayFor(this WeekUnit timeUnit, int interval) + { + return DelayFor(timeUnit.Schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// + public static DelayTimeUnit DelayFor(this MonthUnit timeUnit, int interval) + { + return DelayFor(timeUnit.Schedule, interval); + } + /// + /// Delay first execution of the task for the specified time interval. + /// + public static DelayTimeUnit DelayFor(this YearUnit timeUnit, int interval) + { + return DelayFor(timeUnit.Schedule, interval); } - /// - /// Delay first execution of the task for the specified time interval. - /// - public static DelayTimeUnit DelayFor(this MinuteUnit timeUnit, int interval) - { - return DelayFor(timeUnit.Schedule, interval); - } - /// - /// Delay first execution of the task for the specified time interval. - /// - public static DelayTimeUnit DelayFor(this HourUnit timeUnit, int interval) - { - return DelayFor(timeUnit.Schedule, interval); - } - /// - /// Delay first execution of the task for the specified time interval. - /// - public static DelayTimeUnit DelayFor(this DayUnit timeUnit, int interval) - { - return DelayFor(timeUnit.Schedule, interval); - } - /// - /// Delay first execution of the task for the specified time interval. - /// - public static DelayTimeUnit DelayFor(this WeekUnit timeUnit, int interval) - { - return DelayFor(timeUnit.Schedule, interval); - } - /// - /// Delay first execution of the task for the specified time interval. - /// - public static DelayTimeUnit DelayFor(this MonthUnit timeUnit, int interval) - { - return DelayFor(timeUnit.Schedule, interval); - } - /// - /// Delay first execution of the task for the specified time interval. - /// - public static DelayTimeUnit DelayFor(this YearUnit timeUnit, int interval) - { - return DelayFor(timeUnit.Schedule, interval); - } - } + } } diff --git a/FluentScheduler/Model/DelayTimeUnit.cs b/FluentScheduler/Model/DelayTimeUnit.cs index 3dcffcc9..34a6e203 100644 --- a/FluentScheduler/Model/DelayTimeUnit.cs +++ b/FluentScheduler/Model/DelayTimeUnit.cs @@ -3,47 +3,49 @@ namespace FluentScheduler.Model { - /// - /// Delayed execution support - each method extends the startup time of the task for the specific interval. - /// - public class DelayTimeUnit - { - internal Schedule Schedule { get; private set; } - internal int Interval { get; private set; } + /// + /// Delayed execution support - each method extends the startup time of the task for the specific interval. + /// + public class DelayTimeUnit + { + internal Schedule Schedule { get; private set; } + internal int Interval { get; private set; } - public DelayTimeUnit(Schedule schedule, int interval) - { - Schedule = schedule; - Interval = interval; - } - public void Seconds() - { - Schedule.DelayRunFor = new TimeSpan(0, 0, 0, Interval, 0); - } + public DelayTimeUnit(Schedule schedule, int interval) + { + Schedule = schedule; + Interval = interval; + } + public void Seconds() + { + Schedule.DelayRunFor = new TimeSpan(0, 0, 0, Interval, 0); + } - public void Minutes() - { - Schedule.DelayRunFor = new TimeSpan(0, 0, Interval, 0, 0); - } - public void Hours() - { - Schedule.DelayRunFor = new TimeSpan(0, Interval, 0, 0, 0); - } - public void Days() - { - Schedule.DelayRunFor = new TimeSpan(Interval, 0, 0, 0, 0); - } - public void Weeks() - { - Schedule.DelayRunFor = new TimeSpan(Interval * 7, 0, 0, 0, 0); - } - public void Months() - { - Schedule.DelayRunFor = DateTime.Now.AddMonths(1).Subtract(DateTime.Now); - } - public void Years() - { - Schedule.DelayRunFor = DateTime.Now.AddYears(1).Subtract(DateTime.Now); - } - } + public void Minutes() + { + Schedule.DelayRunFor = new TimeSpan(0, 0, Interval, 0, 0); + } + public void Hours() + { + Schedule.DelayRunFor = new TimeSpan(0, Interval, 0, 0, 0); + } + public void Days() + { + Schedule.DelayRunFor = new TimeSpan(Interval, 0, 0, 0, 0); + } + public void Weeks() + { + Schedule.DelayRunFor = new TimeSpan(Interval * 7, 0, 0, 0, 0); + } + public void Months() + { + var now = DateTime.Now; + Schedule.DelayRunFor = now.AddMonths(Interval).Subtract(now); + } + public void Years() + { + var now = DateTime.Now; + Schedule.DelayRunFor = now.AddYears(Interval).Subtract(now); + } + } } diff --git a/FluentScheduler/Model/Schedule.cs b/FluentScheduler/Model/Schedule.cs index 33e920b6..b7cf5cf6 100644 --- a/FluentScheduler/Model/Schedule.cs +++ b/FluentScheduler/Model/Schedule.cs @@ -12,25 +12,11 @@ public class Schedule internal Func CalculateNextRun { get; set; } - private TimeSpan _DelayRunFor = TimeSpan.Zero; /// /// The first execution of the task can be delayed by the interval defined here. - /// It will only delay the startup (first execution). + /// It will only delay the startup (first execution). /// - internal TimeSpan DelayRunFor - { - get - { - return _DelayRunFor; - } - set - { - if (value != null) - { - _DelayRunFor = value; - } - } - } + internal TimeSpan DelayRunFor { get; set; } internal ICollection AdditionalSchedules { get; set; } internal Schedule Parent { get; set; } internal int TaskExecutions { get; set; } @@ -160,7 +146,7 @@ public SpecificRunTime ToRunOnceAt(int hours, int minutes) /// public SpecificRunTime ToRunOnceAt(DateTime time) { - CalculateNextRun = x => (this.DelayRunFor > TimeSpan.Zero ? time.Add(this.DelayRunFor) : time); + CalculateNextRun = x => (DelayRunFor > TimeSpan.Zero ? time.Add(DelayRunFor) : time); TaskExecutions = 1; return new SpecificRunTime(this); diff --git a/FluentScheduler/Model/SecondUnit.cs b/FluentScheduler/Model/SecondUnit.cs index 3047527c..249a41fe 100644 --- a/FluentScheduler/Model/SecondUnit.cs +++ b/FluentScheduler/Model/SecondUnit.cs @@ -14,5 +14,5 @@ public SecondUnit(Schedule schedule, int duration) Schedule.CalculateNextRun = x => x.AddSeconds(Duration); } - } + } }