Example #1
0
        public void ScheduleOnDayOfMonth()
        {
            //the schedule once, should be return the same date if in the future.
            var startDate = DateTime.Now;

            var expectedDate = startDate;

            // get the next 10th of month date
            while (expectedDate.Day != 10)
            {
                expectedDate = expectedDate.AddDays(1);
            }

            // this date is a monday

            var schedule = new ManagedTaskTrigger()
            {
                IntervalType = EIntervalType.Interval,
                StartDate    = startDate,
                StartTime    = new TimeSpan(1, 0, 0),
                MaxRecurs    = 0,
                DaysOfMonth  = new[]
                {
                    10
                }
            };

            var scheduled = schedule.NextOccurrence(startDate);

            // the expected date will be on the following friday
            var expected = expectedDate.Date.AddHours(1).ToUniversalTime();

            Assert.Equal(expected, scheduled.Value.ToUniversalTime());
        }
Example #2
0
        public void ScheduleWithInterval()
        {
            //the schedule once, should be return the same date if in the future.
            var startDate = DateTime.Now;
            var schedule  = new ManagedTaskTrigger()
            {
                Details      = "",
                EndDate      = null,
                EndTime      = null,
                IntervalTime = TimeSpan.FromHours(4),
                DaysOfWeek   = null,
                IntervalType = EIntervalType.Interval,
                MaxRecurs    = null,
                SkipDates    = null,
                StartDate    = null,
                StartTime    = startDate.TimeOfDay,
                DaysOfMonth  = null,
                WeeksOfMonth = null
            };

            var shiftTime = DateTime.UtcNow.AddSeconds(1);
            var scheduled = schedule.NextOccurrence(shiftTime);

            // add 4 hours to the base date which will give expected date.
            var expected = startDate.AddHours(4);

            _output.WriteLine($"Start Date: {startDate}, Shift Time: {shiftTime}, Scheduled: {scheduled}, Expected: {expected}");
            Assert.Equal(expected, scheduled.Value);
        }
        public async Task Test_ManagedTask_ScheduleAsync()
        {
            var managedTasks = new ManagedTasks.ManagedTasks();

            // simple task that takes 5 seconds
            var managedObject = new ProgressTask(5000, 1);

            var currentDate = DateTime.Now;

            // set a trigger 5 seconds in the future
            var trigger = new ManagedTaskTrigger()
            {
                StartDate    = currentDate,
                StartTime    = currentDate.AddSeconds(5).TimeOfDay,
                IntervalType = EIntervalType.Once
            };

            _output.WriteLine($"Time to Start: {trigger.NextOccurrence(DateTime.Now)-DateTime.Now}");

            var task1 = managedTasks.Add("123", "task3", "test", managedObject, new[] { trigger });

            var cts = new CancellationTokenSource();

            cts.CancelAfter(30000);
            await managedTasks.WhenAll(cts.Token);

            // time should be startDate + 5 second for the job to run.
            Assert.True(trigger.StartDate.Value.AddSeconds(5) < DateTime.Now);
        }
Example #4
0
        public void ScheduleTestOnce()
        {
            //the schedule once, should be return the same date if in the future.
            var startDate = DateTime.Now.AddHours(1);
            var schedule  = new ManagedTaskTrigger(startDate);

            Assert.Equal(startDate, schedule.NextOccurrence(DateTime.Now));

            //the schedule once should return null if the startdate is in the past.
            var startDate2 = DateTime.Now.AddHours(-1);
            var schedule2  = new ManagedTaskTrigger(startDate2);

            _output.WriteLine($"Schedule details {schedule.Details}.");
            Assert.Null(schedule2.NextOccurrence(DateTime.Now));
        }
Example #5
0
        public void ScheduleWithTimeZone()
        {
            //the schedule once, should be return the same date if in the future.
            var schedule = new ManagedTaskTrigger()
            {
                IntervalType = EIntervalType.Interval,
                StartTime    = new TimeSpan(12, 0, 0),
                IntervalTime = TimeSpan.FromDays(1),
                TimeZone     = TimeZoneInfo.GetSystemTimeZones().First().Id,
                MaxRecurs    = null
            };

            var scheduled = schedule.NextOccurrence(DateTime.Now);

            Assert.Equal(12, scheduled.Value.Hour);
        }
Example #6
0
        public void StartDateTest()
        {
            //Set a starttime 1 minute from now
            DateTime currentDate = DateTime.Now;

            var schedule = new ManagedTaskTrigger()
            {
                StartDate = currentDate,
                StartTime = currentDate.AddMinutes(1).TimeOfDay
            };

            var nextSchedule = (DateTimeOffset)schedule.NextOccurrence(currentDate);

            _output.WriteLine($"Schedule details {schedule.Details}.");
            _output.WriteLine($"Schedule time {nextSchedule}.");

            TimeTest(DateTime.Now.AddMinutes(1), nextSchedule);
        }
Example #7
0
        public void EndTimeTest()
        {
            DateTime currentDate = DateTime.Now;

            // set a start time 2 minutes ago, with end time 1 minute ago
            // which should schedule the next run time tomorrow
            var schedule = new ManagedTaskTrigger()
            {
                StartTime    = currentDate.AddMinutes(-2).TimeOfDay,
                EndTime      = currentDate.AddMinutes(-1).TimeOfDay,
                IntervalType = EIntervalType.Daily
            };

            var nextSchedule = (DateTimeOffset)schedule.NextOccurrence(currentDate);

            _output.WriteLine($"Schedule details {schedule.Details}.");
            _output.WriteLine($"Schedule time {nextSchedule}.");

            TimeTest(DateTime.Now.AddDays(1).AddMinutes(-2), nextSchedule);
        }
Example #8
0
        public void ScheduleOnceDaily()
        {
            //the schedule once, should be return the same date if in the future.
            var startDate = DateTime.Now;
            var schedule  = new ManagedTaskTrigger()
            {
                IntervalType = EIntervalType.Interval,
                StartDate    = startDate.AddHours(-24),
                StartTime    = new TimeSpan(1, 0, 0),
                MaxRecurs    = 0,
                DaysOfWeek   = new[]
                {
                    EDayOfWeek.Friday, EDayOfWeek.Monday, EDayOfWeek.Saturday, EDayOfWeek.Sunday, EDayOfWeek.Thursday,
                    EDayOfWeek.Tuesday, EDayOfWeek.Wednesday
                }
            };

            var scheduled = schedule.NextOccurrence(startDate);

            // add 25 hours to the base date which will give expected date next day at 1am.
            var expected = startDate.Date.AddHours(25).ToUniversalTime();

            Assert.Equal(expected, scheduled.Value.ToUniversalTime());
        }