Beispiel #1
0
        public void TestAddActionRuns()
        {
            bool ran = false;

            Instant due = TimeHelpers.Clock.Now + Duration.FromMilliseconds(100);

            Assert.IsTrue(due > TimeHelpers.Clock.Now);

            ScheduledAction action = Scheduler.Add(() => { ran = true; }, new OneOffSchedule(due));

            Assert.IsTrue(due > TimeHelpers.Clock.Now);

            Assert.IsNotNull(action);
            Assert.IsTrue(action.Enabled);
            Assert.AreEqual(Scheduler.DefaultMaximumHistory, action.MaximumHistory);
            Assert.AreEqual(Scheduler.DefaultMaximumDuration, action.MaximumDuration);
            Thread.Sleep(200);
            Assert.IsTrue(ran);
            Assert.AreEqual(1, action.History.Count());

            ScheduledActionResult history = action.History.FirstOrDefault();

            Assert.IsNotNull(history);
            Assert.IsFalse(history.Cancelled);
            Assert.IsNull(history.Exception);
            Assert.AreEqual(due, history.Due);
            Assert.IsTrue(history.Due <= history.Started);
        }
Beispiel #2
0
        public void TestAddActionCancel()
        {
            bool            ran      = false;
            Instant         due      = TimeHelpers.Clock.Now + Duration.FromMilliseconds(100);
            Duration        duration = Duration.FromMilliseconds(20);
            ScheduledAction action   = Scheduler.Add(
                () =>
            {
                ran = true;
                // This needs to sleep longer than the max duration to ensure it will be cancelled
                Thread.Sleep((int)duration.TotalMilliseconds() * 2);
            },
                new OneOffSchedule(due),
                maximumDuration: duration);

            Assert.IsNotNull(action);
            Assert.IsTrue(action.Enabled);
            Assert.AreEqual(Scheduler.DefaultMaximumHistory, action.MaximumHistory);
            Assert.AreEqual(duration, action.MaximumDuration);
            Thread.Sleep(200);
            Assert.IsTrue(ran);
            Assert.AreEqual(1, action.History.Count());

            ScheduledActionResult history = action.History.FirstOrDefault();

            Assert.IsNotNull(history);
            Assert.IsTrue(history.Cancelled);
            Assert.IsNull(history.Exception);
            Assert.AreEqual(due, history.Due);
            Assert.IsTrue(history.Due <= history.Started);
        }
Beispiel #3
0
        public async Task TestDeBounce()
        {
            int             runCount = 0;
            ScheduledAction action   = Scheduler.Add(
                () =>
            {
                Interlocked.Increment(ref runCount);
            },
                Schedule.Never,
                4);

            Assert.IsNotNull(action);
            Assert.IsTrue(action.Enabled);
            Assert.AreEqual(4, action.MaximumHistory);

            // Create 3 tasks at the same time.
            Task <ScheduledActionResult>[] tasks = Enumerable.Range(0, 3).Select(i => action.ExecuteAsync()).ToArray();
            Assert.AreEqual(3, tasks.Length);

            // ReSharper disable once PossibleNullReferenceException
            await Task.WhenAll(tasks);

            Assert.AreEqual(1, runCount);
            Assert.AreEqual(1, action.History.Count());
            ScheduledActionResult last = action.LastResult;

            Assert.IsNotNull(last);

            foreach (Task <ScheduledActionResult> task in tasks)
            {
                Assert.IsTrue(task.IsCompleted);
                Assert.AreSame(last, task.Result);
            }

            // Confirm still runs again
            ScheduledActionResult result = await action.ExecuteAsync();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, runCount);
            Assert.AreEqual(2, action.History.Count()); last = action.LastResult;
            Assert.IsNotNull(last);
            Assert.AreSame(last, result);
        }