Example #1
0
 public void CrontabExceed()
 {
     var dt = new DateTime(2000, 1, 1, 1, 0, 0);
     var target = new Crontab("* */80 * * * * *");
     Assert.IsTrue(target.MayRunAt(dt));
     dt += TimeSpan.FromMinutes(40);
     Assert.IsFalse(target.MayRunAt(dt));
     dt += TimeSpan.FromMinutes(40);
     Assert.IsFalse(target.MayRunAt(dt));
 }
Example #2
0
        public void CrontabNextRun()
        {
            var target = new Crontab("* * * * * * *");
            Assert.AreEqual(target.Next(new DateTime(2000, 1, 1, 1, 1, 0,0)), new DateTime(2000, 1, 1, 1, 1, 0,0));

            Assert.AreEqual(target.Next(new DateTime(2200, 1, 1, 1, 1, 0)), new DateTime(2200, 1, 1, 1, 1, 0));

            Assert.AreEqual(target.Next(new DateTime(1000, 1, 1, 1, 1, 0)), new DateTime(1000, 1, 1, 1, 1, 0));

            target = new Crontab("* */1 * * * * *");
            Assert.AreEqual(target.Next(new DateTime(2000, 1, 1, 1, 1, 0)), new DateTime(2000, 1, 1, 1, 1, 0));

            target = new Crontab("* * * * * * 2008");
            Assert.AreEqual(target.Next(new DateTime(2007, 12, 31, 23,59, 59)), new DateTime(2008, 1, 1, 0, 0, 0));

            target = new Crontab("* * * * 2 * 2008");
            Assert.AreEqual(target.Next(new DateTime(2008, 1, 31, 23, 59, 59)), new DateTime(2008, 2, 1, 0, 0, 0));

            target = new Crontab("* * * 2 * * 2008");
            Assert.AreEqual(target.Next(new DateTime(2008, 1, 1, 23, 59, 59)), new DateTime(2008, 1, 2, 0, 0, 0));

            target = new Crontab("* * * * * 3 *");
            Assert.AreEqual(target.Next(new DateTime(2012, 11, 27, 23, 59, 59)), new DateTime(2012, 11, 28, 0, 0, 0));

            target = new Crontab("* * * * * 1 *");
            Assert.AreEqual(target.Next(new DateTime(2012, 11, 27, 23, 59, 59)), new DateTime(2012, 12, 3, 0, 0, 0));

            target = new Crontab("* * * * * 1 *");
            Assert.AreEqual(target.Next(new DateTime(2012, 11, 27, 23, 59, 59)), new DateTime(2012, 12, 3, 0, 0, 0));

            target = new Crontab("* 2-59/3 1,9,22 11-26 1-6 ? 2003");
            Assert.AreEqual(target.Next(new DateTime(2003, 1, 11, 9, 5, 0)), new DateTime(2003, 1, 11, 9, 5, 0));
            Assert.AreEqual(target.Next(new DateTime(2003, 1, 11, 9, 6, 0)), new DateTime(2003, 1, 11, 9, 8, 0));
            Assert.AreEqual(target.Next(new DateTime(2003, 1, 11, 23, 6, 0)), new DateTime(2003, 1, 12, 1, 2, 0));
            Assert.AreEqual(target.Next(new DateTime(2003, 1, 26, 23, 00, 0)), new DateTime(2003, 2, 11, 1, 2, 0));

            target = new Crontab("* 2-59/3 1,9,22 11-26 1-6 ? 2003-2012");
            Assert.AreEqual(target.Next(new DateTime(2008, 7, 11, 9, 5, 0)), new DateTime(2009, 1, 11, 1, 2, 0));
        }
Example #3
0
        public void CrontabMayRunAt()
        {
            var target = new Crontab("* * * * * * *");
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));

            target = new Crontab("* * * * * 9* *");
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));

            target = new Crontab("* */1 * * * * *");
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));

            target = new Crontab("* 2-59/3 1,9,22 11-26 1-6 ? 2003");
            Assert.IsTrue(target.MayRunAt(new DateTime(2003, 1, 11, 9, 5, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 11, 9, 5, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 11, 9, 6, 0)));

            target = new Crontab("* 2-59/3 1,9,22 11-26 1-6 ? 2003-2012");
            Assert.IsTrue(target.MayRunAt(new DateTime(2008, 1, 11, 9, 5, 0)));

            target = new Crontab("* 0 0/1 * * * ?");
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 0, 0)));
        }
Example #4
0
 public void FailGetNeares()
 {
     var now = DateTime.Now;
     var target = new Crontab(now, 1000);
     var good = now + TimeSpan.FromMilliseconds(1000);
     var bad = now + TimeSpan.FromMilliseconds(1002);
     Assert.IsTrue(target.MayRunAt(good));
     Assert.IsFalse(target.MayRunAt(bad));
 }
Example #5
0
        public void CrontabWithSeconds()
        {
            var target = new Crontab("10 */1 * * * * *", true);

            var nxt = target.Next();
            var now = DateTime.Now;
            now += TimeSpan.FromMinutes(1);
            now += TimeSpan.FromSeconds(11);
            Assert.IsTrue(now>=nxt);

            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 10)));

            target = new Crontab("* * * * * * *", true);
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));

            target = new Crontab("10* * * * * * *", true);
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 10)));

            target = new Crontab("* */1 * * * * *", true);
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));

            target = new Crontab("* 2-59/3 1,9,22 11-26 1-6 ? 2003", true);
            Assert.IsTrue(target.MayRunAt(new DateTime(2003, 1, 11, 9, 5, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 11, 9, 5, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 11, 9, 6, 0)));

            target = new Crontab("* 2-59/3 1,9,22 11-26 1-6 ? 2003-2012", true);
            Assert.IsTrue(target.MayRunAt(new DateTime(2008, 1, 11, 9, 5, 0)));

            target = new Crontab("* 0 0/1 * * * ?", true);
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 1, 1, 1, 0)));
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 1, 0, 0)));
        }
Example #6
0
        public void CrontabWithInvalidParameters()
        {
            Exception expex = null;
            try
            {
                var target = new Crontab(null);
                Assert.IsTrue(target == null);
            }
            catch (ArgumentNullException ex)
            {
                expex = ex;
            }
            Assert.IsTrue(expex is ArgumentNullException);

            try
            {
                var target = new Crontab("* * * * * * * *", true);
                Assert.IsTrue(target == null);
            }
            catch (IndexOutOfRangeException ex)
            {
                expex = ex;
            }
            Assert.IsTrue(expex is IndexOutOfRangeException);

            try
            {
                var target = new Crontab("@fakeCommand", true);
                Assert.IsTrue(target==null);
            }
            catch (IndexOutOfRangeException ex)
            {
                expex = ex;
            }
            Assert.IsTrue(expex is IndexOutOfRangeException);
        }
Example #7
0
        public void CrontabWithDateInput()
        {
            var now = DateTime.Now;
            var target = new Crontab(now,5000);
            var good = new DateTime(now.Ticks + 5000* TimeSpan.TicksPerMillisecond);
            var bad = new DateTime(now.Ticks + 1040 * TimeSpan.TicksPerMillisecond);
            var next1 = target.Next();
            Thread.Sleep(100);
            var next = target.Next();
            Assert.IsTrue(target.MayRunAt(good));
            Assert.IsFalse(target.MayRunAt(bad));

            var showDiff = string.Format("Next <{0}> now <{1}>", next1, now);
            Assert.AreEqual(next1.DayOfYear, now.DayOfYear,showDiff);
            Assert.AreEqual(next1.Hour, now.Hour,showDiff);
            Assert.AreEqual(next1.Minute, now.Minute,showDiff);
            //Assert.IsTrue(Math.Abs(next1.Second - now.Second) <= 1, showDiff);

            Assert.AreEqual(next.DayOfYear, good.DayOfYear);
            Assert.AreEqual(next.Hour, good.Hour);
            Assert.AreEqual(next.Minute, good.Minute);
            Assert.IsTrue(Math.Abs(next.Second - good.Second) <= 1);

            DateTime? newNext = new DateTime(next.Ticks + 2600*TimeSpan.TicksPerMillisecond);
            var wrongNext = target.Next(newNext);
            Assert.IsFalse(Math.Abs(newNext.Value.Second - wrongNext.Second) <= 1);
        }
Example #8
0
        public void CrontabSpecial()
        {
            var target = new Crontab("@yearly");
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 0, 0, 0)));
            Assert.IsTrue(target.MayRunAt(new DateTime(2001, 1, 1, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 1, 0, 1, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 1, 1, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 2, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 2, 1, 0, 0, 0)));

            target = new Crontab("@annually");
            Assert.IsTrue(target.MayRunAt(new DateTime(2000, 1, 1, 0, 0, 0)));
            Assert.IsTrue(target.MayRunAt(new DateTime(2001, 1, 1, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 1, 0, 1, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 1, 1, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 1, 2, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 2, 1, 0, 0, 0)));

            target = new Crontab("@monthly");
            Assert.IsTrue(target.MayRunAt(new DateTime(2001, 2, 1, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2000, 2, 1, 0, 1, 0)));

            target = new Crontab("@weekly");
            Assert.IsTrue(target.MayRunAt(new DateTime(2012, 4, 15, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2012, 4, 18, 0, 0, 0)));

            target = new Crontab("@daily");
            Assert.IsTrue(target.MayRunAt(new DateTime(2012, 4, 15, 0, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2012, 4, 15, 0, 1, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2012, 4, 18, 1, 0, 0)));

            target = new Crontab("@hourly");
            Assert.IsTrue(target.MayRunAt(new DateTime(2012, 4, 15, 1, 0, 0)));
            Assert.IsFalse(target.MayRunAt(new DateTime(2012, 4, 15, 1, 1, 0)));
        }