示例#1
0
        public void TaskStarting_TaskCompleted_Success()
        {
            bool TaskStartingExecuted  = false;
            bool TaskCompletedExecuted = false;
            Scheduler <BlockingScheduledAction> scheduler = new Scheduler <BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));

            scheduler.TaskCompleted += (sender, e) =>
            {
                TaskCompletedExecuted = true;
            };
            scheduler.TaskStarting += (sender, e) =>
            {
                TaskStartingExecuted = true;
            };
            BlockingScheduledAction action1 = new BlockingScheduledAction();

            scheduler.Tasks.Add(new ScheduledTask <BlockingScheduledAction>(Schedule.CreateOneTime(DateTimeOffset.Now), action1, true, null, null));
            scheduler.Start();

            action1.Waiter.WaitOne();
            scheduler.Stop();

            Assert.IsTrue(TaskCompletedExecuted, nameof(TaskCompletedExecuted));
            Assert.IsTrue(TaskStartingExecuted, nameof(TaskStartingExecuted));

            scheduler.Dispose();
        }
		public void Ctor_TimeSpan_Success()
		{
			Scheduler<BlockingScheduledAction> scheduler = new Scheduler<BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));
			BlockingScheduledAction action = new BlockingScheduledAction();
			scheduler.Tasks.Add(new ScheduledTask<BlockingScheduledAction>(Schedule.CreateOneTime(DateTimeOffset.Now.AddSeconds(1.0)), action, true, null, null));
			scheduler.Start();
			Assert.IsTrue(scheduler.IsEnabled);

			Stopwatch watch = Stopwatch.StartNew();
			action.Waiter.WaitOne();
			Assert.IsTrue(watch.ElapsedMilliseconds < 2000);
			scheduler.Dispose();
		}
		public void MultipleIterations_Success()
		{
			Scheduler<BlockingScheduledAction> scheduler = new Scheduler<BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));
			BlockingScheduledAction action1 = new BlockingScheduledAction(10);
			scheduler.Tasks.Add(new ScheduledTask<BlockingScheduledAction>(Schedule.CreateInterval(DateTimeOffset.Now, TimeSpan.FromMilliseconds(100.0), Weekdays.All), action1, true, null, null));
			BlockingScheduledAction action2 = new BlockingScheduledAction(5);
			scheduler.Tasks.Add(new ScheduledTask<BlockingScheduledAction>(Schedule.CreateInterval(DateTimeOffset.Now, TimeSpan.FromMilliseconds(200.0), Weekdays.All), action2, true, null, null));
			scheduler.Start();

			Stopwatch watch = Stopwatch.StartNew();
			ManualResetEvent.WaitAny(new WaitHandle[] { action1.Waiter, action2.Waiter });
			Assert.IsTrue(watch.ElapsedMilliseconds < 1200);
			scheduler.Dispose();
		}
示例#4
0
        public void Ctor_TimeSpan_Success()
        {
            Scheduler <BlockingScheduledAction> scheduler = new Scheduler <BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));
            BlockingScheduledAction             action    = new BlockingScheduledAction();

            scheduler.Tasks.Add(new ScheduledTask <BlockingScheduledAction>(Schedule.CreateOneTime(DateTimeOffset.Now.AddSeconds(1.0)), action, true, null, null));
            scheduler.Start();
            Assert.IsTrue(scheduler.IsEnabled);

            Stopwatch watch = Stopwatch.StartNew();

            action.Waiter.WaitOne();
            Assert.IsTrue(watch.ElapsedMilliseconds < 2000);
            scheduler.Dispose();
        }
示例#5
0
        public void MultipleIterations_Success()
        {
            Scheduler <BlockingScheduledAction> scheduler = new Scheduler <BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));
            BlockingScheduledAction             action1   = new BlockingScheduledAction(10);

            scheduler.Tasks.Add(new ScheduledTask <BlockingScheduledAction>(Schedule.CreateInterval(DateTimeOffset.Now, TimeSpan.FromMilliseconds(100.0), Weekdays.All), action1, true, null, null));
            BlockingScheduledAction action2 = new BlockingScheduledAction(5);

            scheduler.Tasks.Add(new ScheduledTask <BlockingScheduledAction>(Schedule.CreateInterval(DateTimeOffset.Now, TimeSpan.FromMilliseconds(200.0), Weekdays.All), action2, true, null, null));
            scheduler.Start();

            Stopwatch watch = Stopwatch.StartNew();

            ManualResetEvent.WaitAny(new WaitHandle[] { action1.Waiter, action2.Waiter });
            Assert.IsTrue(watch.ElapsedMilliseconds < 1200);
            scheduler.Dispose();
        }
示例#6
0
        public void Interval_Success()
        {
            Scheduler <BlockingScheduledAction> scheduler = new Scheduler <BlockingScheduledAction>(TimeSpan.FromSeconds(1000.0));
            BlockingScheduledAction             action1   = new BlockingScheduledAction(5);

            scheduler.Tasks.Add(new ScheduledTask <BlockingScheduledAction>(Schedule.CreateInterval(DateTimeOffset.Now, TimeSpan.FromMilliseconds(200.0), Weekdays.All), action1, true, null, null));
            scheduler.Start();

            Assert.AreEqual(1000.0, scheduler.Interval.TotalSeconds);
            scheduler.Interval = TimeSpan.FromSeconds(0.1);

            Stopwatch watch = Stopwatch.StartNew();

            action1.Waiter.WaitOne();
            Assert.IsTrue(watch.ElapsedMilliseconds < 1200);
            scheduler.Dispose();
        }
示例#7
0
        public void TaskStarting_Canceled_Success()
        {
            bool TaskStartingExecuted = false;
            Scheduler <BlockingScheduledAction> scheduler = new Scheduler <BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));

            scheduler.TaskStarting += (sender, e) =>
            {
                e.Cancel             = true;
                TaskStartingExecuted = true;
            };
            BlockingScheduledAction action1 = new BlockingScheduledAction();
            ScheduledTask <BlockingScheduledAction> task = new ScheduledTask <BlockingScheduledAction>(Schedule.CreateOneTime(DateTimeOffset.Now), action1, true, null, null);

            scheduler.Tasks.Add(task);
            scheduler.Start();

            SpinWait.SpinUntil(() => { return(TaskStartingExecuted); });
            scheduler.Stop();

            Assert.IsNull(task.LastEndTime);

            scheduler.Dispose();
        }
		public void TaskStarting_Canceled_Success()
		{
			bool TaskStartingExecuted = false;
			Scheduler<BlockingScheduledAction> scheduler = new Scheduler<BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));
			scheduler.TaskStarting += (sender, e) =>
			{
				e.Cancel = true;
				TaskStartingExecuted = true;
			};
			BlockingScheduledAction action1 = new BlockingScheduledAction();
			ScheduledTask<BlockingScheduledAction> task = new ScheduledTask<BlockingScheduledAction>(Schedule.CreateOneTime(DateTimeOffset.Now), action1, true, null, null);
			scheduler.Tasks.Add(task);
			scheduler.Start();

			SpinWait.SpinUntil(() => { return TaskStartingExecuted; });
			scheduler.Stop();

			Assert.IsNull(task.LastEndTime);

			scheduler.Dispose();
		}
		public void TaskStarting_TaskCompleted_Success()
		{
			bool TaskStartingExecuted = false;
			bool TaskCompletedExecuted = false;
			Scheduler<BlockingScheduledAction> scheduler = new Scheduler<BlockingScheduledAction>(TimeSpan.FromSeconds(0.1));
			scheduler.TaskCompleted += (sender, e) =>
			{
				TaskCompletedExecuted = true;
			};
			scheduler.TaskStarting += (sender, e) =>
			{
				TaskStartingExecuted = true;
			};
			BlockingScheduledAction action1 = new BlockingScheduledAction();
			scheduler.Tasks.Add(new ScheduledTask<BlockingScheduledAction>(Schedule.CreateOneTime(DateTimeOffset.Now), action1, true, null, null));
			scheduler.Start();

			action1.Waiter.WaitOne();
			scheduler.Stop();

			Assert.IsTrue(TaskCompletedExecuted, nameof(TaskCompletedExecuted));
			Assert.IsTrue(TaskStartingExecuted, nameof(TaskStartingExecuted));

			scheduler.Dispose();
		}
		public void Interval_Success()
		{
			Scheduler<BlockingScheduledAction> scheduler = new Scheduler<BlockingScheduledAction>(TimeSpan.FromSeconds(1000.0));
			BlockingScheduledAction action1 = new BlockingScheduledAction(5);
			scheduler.Tasks.Add(new ScheduledTask<BlockingScheduledAction>(Schedule.CreateInterval(DateTimeOffset.Now, TimeSpan.FromMilliseconds(200.0), Weekdays.All), action1, true, null, null));
			scheduler.Start();

			Assert.AreEqual(1000.0, scheduler.Interval.TotalSeconds);
			scheduler.Interval = TimeSpan.FromSeconds(0.1);

			Stopwatch watch = Stopwatch.StartNew();
			action1.Waiter.WaitOne();
			Assert.IsTrue(watch.ElapsedMilliseconds < 1200);
			scheduler.Dispose();
		}