private void RunPreciseTimerTest(CommonTools.Components.Threading.Jobs jobs, JobSection section, int sleepTimeInMs) { _Ticks = new List <long>(); jobs.Start(section); Thread.Sleep(sleepTimeInMs); jobs.Stop(); }
public void Test_PreciseTimer() { int ms = 1; JobSection section = new JobSection(); section.JobItems.Add( new JobElement() { Enabled = true, EnableShutDown = false, ExecuteDaily = false, ExecuteOnOwnThread = true, FirstRunAtInitialization = true, Period = TimeSpan.FromMilliseconds(ms), PreciseTimerCallbackMode = PreciseTimerCallbackMode.Async, UsePreciseTimer = true, Type = "CommonTools.TestSuite.Tests.PreciseTimerJob, CommonTools.TestSuite" }); CommonTools.Components.Threading.Jobs jobs = CommonTools.Components.Threading.Jobs.Instance(); int iterations = 100; // run first time to compile the C# code RunPreciseTimerTest(jobs, section, ms * iterations); Thread.Sleep(1000); // wait a bit so the timer gets its stuff sorted // now run the proper test RunPreciseTimerTest(jobs, section, ms * iterations); Thread.Sleep(ms * 10); int length = _Ticks.Count; long difference; for (int i = 20; i < length; i++) {// start at the 10th iteration to give some time for the compiler to optimize... difference = _Ticks[i] - _Ticks[i - 1]; Trace.WriteLine(difference); Assert.GreaterOrEqual(difference, ms * 10000); Assert.LessOrEqual(difference, ms * 10000 + 120); // we should have at least a 120 nano second precision } Assert.AreEqual(length, _Ticks.Count); }
public void Test_DailyJobExecution() { DateTime now = DateTime.Now; int ms = 1000; JobSection section = new JobSection(); section.JobItems.Add(new JobElement() { Enabled = true, EnableShutDown = false, ExecuteDaily = true, ExecuteOnOwnThread = true, FirstRunAtInitialization = false, DailyLocalizedExecutionTimeZoneName = "GMT Standard Time", DailyLocalizedExecutionTime = now.AddMilliseconds(ms), Type = "CommonTools.TestSuite.Tests.ResetEventJob, CommonTools.TestSuite", Options = new Dictionary <string, string>() { { "Index", "0" }, { "Name", "Test_DailyJobExecution" } } }); _JobTestResetEvent = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) }; _JobTestSynchronizationCounter = new int[] { 0, 0, 0 }; CommonTools.Components.Threading.Jobs jobs = CommonTools.Components.Threading.Jobs.Instance(); jobs.Start(section); Stopwatch sw = Stopwatch.StartNew(); _JobTestResetEvent[0].WaitOne(10000); sw.Stop(); Assert.LessOrEqual(sw.ElapsedMilliseconds, ms + 1000); Assert.AreEqual(1, _JobTestSynchronizationCounter[0]); jobs.Stop(); }
//Create single instance of Jobs static Jobs() { _Jobs = new Jobs(); }
public void Test_SeveralJobsOnOwnThread() { JobSection section = new JobSection(); double msJobShort = 1000 , msJobMedium = 3000 , msJobLong = 7000; section.JobItems.Add(new JobElement() { Enabled = true, EnableShutDown = false, ExecuteDaily = false, ExecuteOnOwnThread = true, FirstRunAtInitialization = false, Period = TimeSpan.FromMilliseconds(msJobShort), Type = "CommonTools.TestSuite.Tests.ResetEventJob, CommonTools.TestSuite", Options = new Dictionary <string, string>() { { "Index", "0" }, { "Name", "msJobShort" } } }); section.JobItems.Add(new JobElement() { Enabled = true, EnableShutDown = false, ExecuteDaily = false, ExecuteOnOwnThread = true, FirstRunAtInitialization = false, Period = TimeSpan.FromMilliseconds(msJobMedium), Type = "CommonTools.TestSuite.Tests.ResetEventJob, CommonTools.TestSuite", Options = new Dictionary <string, string>() { { "Index", "1" }, { "Name", "msJobMedium" } } }); section.JobItems.Add(new JobElement() { Enabled = true, EnableShutDown = false, ExecuteDaily = false, ExecuteOnOwnThread = true, FirstRunAtInitialization = false, Period = TimeSpan.FromMilliseconds(msJobLong), Type = "CommonTools.TestSuite.Tests.ResetEventJob, CommonTools.TestSuite", Options = new Dictionary <string, string>() { { "Index", "2" }, { "Name", "msJobLong" } } }); _JobTestResetEvent = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) }; _JobTestSynchronizationCounter = new int[] { 0, 0, 0 }; CommonTools.Components.Threading.Jobs jobs = CommonTools.Components.Threading.Jobs.Instance(); jobs.Start(section); Stopwatch sw = Stopwatch.StartNew(); _JobTestResetEvent[0].WaitOne(10000); _JobTestResetEvent[1].WaitOne(10000); _JobTestResetEvent[2].WaitOne(10000); sw.Stop(); Assert.LessOrEqual(sw.ElapsedMilliseconds, msJobLong + 1000); // the reset events should not take longer than the Assert.GreaterOrEqual(_JobTestSynchronizationCounter[0], 5); Assert.GreaterOrEqual(_JobTestSynchronizationCounter[1], 2); Assert.GreaterOrEqual(_JobTestSynchronizationCounter[2], 1); jobs.Stop(); }