public void TestFirstTickTwoSecondsHourly() { // Create a dummy controller to record the callbacks. DummyControllerContext controllerContext = new DummyControllerContext(); // Set up parameters for the WinQualSyncTimerTask WinQualSyncTimerTaskParameters params1 = new WinQualSyncTimerTaskParameters(); // Standard task parameters. params1.IsBackgroundTask = true; params1.Name = "TestRunOneTask"; params1.RunInParallel = false; params1.UseSeparateThread = true; params1.ControllerContext = controllerContext; // Specific task parameters. params1.ThisWinQualContext = new WinQualContext(null); // Set the schedule so the task runs in 2 seconds time (every hour). params1.ScheduleCollection = new ScheduleCollection(); Schedule schedule = new Schedule(); schedule.DaysOfWeek = DaysOfWeek.Monday | DaysOfWeek.Tuesday | DaysOfWeek.Wednesday | DaysOfWeek.Thursday | DaysOfWeek.Friday | DaysOfWeek.Saturday | DaysOfWeek.Sunday; DateTime timeToRunTask = DateTime.Now.AddSeconds(2); schedule.Time = new ScheduleTime(timeToRunTask.Hour, timeToRunTask.Minute, timeToRunTask.Second); schedule.Period = SchedulePeriod.Hourly; params1.ScheduleCollection.Add(schedule); // Create the task and run it. WinQualSyncTimerTask task1 = new WinQualSyncTimerTask(params1); TaskManager taskManager = new TaskManager("Test"); taskManager.Enqueue(task1); Assert.AreEqual(0, controllerContext.SynchronizeCallCount); // Now wait for a little over 2 seconds. Thread.Sleep(4000); Assert.AreEqual(1, controllerContext.SynchronizeCallCount); // Task should still be running after a tick. Assert.AreEqual(false, task1.CurrentTaskState.TaskCompleted); taskManager.AbortAllTasks(false); taskManager.WaitForTaskCompletion(task1, s_TaskTimeout); // Should be set to trigger in approximately an hour. long timeDiff = Math.Abs(60 * 60 * 1000 - task1.TimeInMilliseconds); Assert.AreEqual(true, timeDiff < 2000); }
public void TestFirstTickTwoSecondsWeekly() { // Create a dummy controller to record the call backs. DummyControllerContext controllerContext = new DummyControllerContext(); WinQualSyncTimerTaskParameters params1 = new WinQualSyncTimerTaskParameters(); params1.IsBackgroundTask = true; params1.Name = "TestRunOneTask"; params1.RunInParallel = false; params1.UseSeparateThread = true; params1.ControllerContext = controllerContext; params1.ThisWinQualContext = new WinQualContext(null); params1.ScheduleCollection = new ScheduleCollection(); Schedule schedule = new Schedule(); schedule.DaysOfWeek = Schedule.ConvertDateTimeDayToStackHashDay(DateTime.Now.DayOfWeek); DateTime timeToRunTask = DateTime.Now.AddSeconds(2); schedule.Time = new ScheduleTime(timeToRunTask.Hour, timeToRunTask.Minute, timeToRunTask.Second); schedule.Period = SchedulePeriod.Weekly; params1.ScheduleCollection.Add(schedule); WinQualSyncTimerTask task1 = new WinQualSyncTimerTask(params1); // Create a task manager for the context. TaskManager taskManager = new TaskManager("Test"); taskManager.Enqueue(task1); Assert.AreEqual(0, controllerContext.SynchronizeCallCount); // Now wait for a little over 2 seconds + 1 (the extra 1 is because the sync task starts 1 second after the timer triggers). Thread.Sleep(4000); Assert.AreEqual(1, controllerContext.SynchronizeCallCount); // Task should still be running after a tick. Assert.AreEqual(false, task1.CurrentTaskState.TaskCompleted); taskManager.AbortAllTasks(false); taskManager.WaitForTaskCompletion(task1, s_TaskTimeout); taskManager.Dispose(); // Should be set to trigger in approximately an hour. long timeDiff = Math.Abs(7 * 24 * 60 * 60 * 1000 - task1.TimeInMilliseconds); Assert.AreEqual(true, timeDiff < (500 + 60 * 60 * 1000)); // Could be an hour out if you run this test during the week before a daylight saving change. }