public void Should_Remove_LongRunning_Task_But_Keep_Running()
        {
            var name = "longrunning";
            var schedule = new Schedule(() => { Thread.Sleep(100); });
            schedule.WithName(name).ToRunNow().AndEvery(2).Seconds();
            schedule.Execute();

            Assert.IsTrue(TaskManager.RunningSchedules.Any(task => task.Name == name));
            TaskManager.RemoveTask(name);
            Assert.IsNull(TaskManager.GetSchedule(name));
            Assert.IsTrue(TaskManager.RunningSchedules.Any(task => task.Name == name));

            Thread.Sleep(2042); // wait until a second run would normally be executed
            Assert.IsFalse(TaskManager.RunningSchedules.Any(task => task.Name == name));
        }
Beispiel #2
0
        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<MyInlineTask>(x => x.ToRunNow());
             */
            TaskManager.UnobservedTaskException += TaskManager_UnobservedTaskException;

            /*			TaskManager.AddTask(() => {
             *                                                                              Console.WriteLine("Inline task: " + DateTime.Now);
             *                              throw new Exception("Hi"); }, x => x.ToRunNow());
             */
        }
Beispiel #3
0
        public void Should_Be_Able_To_Schedule_Multiple_ITasks()
        {
            var task1 = new Mock<ITask>();
            var task2 = new Mock<ITask>();
            task1.Setup(m => m.Execute());
            task2.Setup(m => m.Execute());
            var schedule = new Schedule(task1.Object).AndThen(task2.Object);
            schedule.Execute();

            while (TaskManager.RunningSchedules.Any())
            {
                Thread.Sleep(1);
            }
            task1.Verify(m => m.Execute(), Times.Once());
            task2.Verify(m => m.Execute(), Times.Once());
        }
Beispiel #4
0
        public void Should_Be_Able_To_Schedule_Multiple_Simple_Methods()
        {
            // Arrange
            var task1 = new Mock<ITask>();
            var task2 = new Mock<ITask>();
            task1.Setup(m => m.Execute());
            task2.Setup(m => m.Execute());

            // Act
            var schedule = new Schedule(() => task1.Object.Execute()).AndThen(() => task2.Object.Execute());
            schedule.Execute();
            while (TaskManager.RunningSchedules.Any())
                Thread.Sleep(1);

            // Assert
            task1.Verify(m => m.Execute(), Times.Once());
            task2.Verify(m => m.Execute(), Times.Once());
        }
Beispiel #5
0
        public void Should_Remove_LongRunning_Task_But_Keep_Running()
        {
            // Act
            var schedule = new Schedule(() => Thread.Sleep(100));
            schedule.WithName("remove long running task").ToRunNow().AndEvery(2).Seconds();
            schedule.Execute();

            // Assert
            Assert.IsTrue(TaskManager.RunningSchedules.Any(s => s.Name == "remove long running task"));

            // Act
            TaskManager.RemoveTask("remove long running task");

            // Assert
            Assert.IsNull(TaskManager.GetSchedule("remove long running task"));
            Assert.IsTrue(TaskManager.RunningSchedules.Any(s => s.Name == "remove long running task"));
            Thread.Sleep(2000);
            Assert.IsFalse(TaskManager.RunningSchedules.Any(s => s.Name == "remove long running task"));
        }
Beispiel #6
0
        public void Should_Execute_Tasks_In_Order()
        {
            var task1 = new Mock<ITask>();
            var task2 = new Mock<ITask>();
            var task1Runtime = DateTime.MinValue;
            var task2Runtime = DateTime.MinValue;
            task1.Setup(m => m.Execute()).Callback(() =>
                {
                    task1Runtime = DateTime.Now;
                    Thread.Sleep(1);
                });
            task2.Setup(m => m.Execute()).Callback(() => task2Runtime = DateTime.Now);
            var schedule = new Schedule(() => task1.Object.Execute()).AndThen(() => task2.Object.Execute());
            schedule.Execute();

            while (TaskManager.RunningSchedules.Any())
            {
                Thread.Sleep(1);
            }
            Assert.Less(task1Runtime.Ticks, task2Runtime.Ticks);
        }