public void Test_AddJob() { Scheduler s = new Scheduler(3, 10); // Add legal jobs with the three types: Short, Long, VeryLong Func<string[], int> funk = st => 10; Job shortJob = new Job(new User("test", ""), 100, 5, funk); Job longJob = new Job(new User("test", ""), 1000, 5, funk); Job veryLongJob = new Job(new User("test", ""), 2500, 5, funk); s.AddJob(shortJob); s.AddJob(longJob); s.AddJob(veryLongJob); Assert.AreEqual(3, s.JobsInSequence.Count); // Add 4000 jobs of varying type for (uint i = 0; i < 4000; i++) { s.AddJob(new Job(new User("test", ""), i + 100, 5, funk)); } Assert.AreEqual(4003, s.JobsInSequence.Count); }
public void Test_AddJobNull() { Scheduler s = new Scheduler(3, 10); try { s.AddJob(null); Assert.Fail("It was possible to add a null job"); } catch (Exception e) { // If it is not a contract exception the test fails if (!e.ToString().ToLower().Contains("precondition failed")) { Assert.Fail("Unexpected exception thrown: " + e); } } }
/// <summary> /// Helper method. Checks that the sequence of jobs returned is in the same order as they were added /// </summary> /// <param name="s">The scheduler</param> /// <param name="first">Job to add first</param> /// <param name="second">Job to add second</param> /// <param name="third">Job to add last</param> private void AssertSequence(Scheduler s, Job first, Job second, Job third) { s.AddJob(first); s.AddJob(second); s.AddJob(third); Assert.AreEqual(first, s.PopJob()); Assert.AreEqual(second, s.PopJob()); Assert.AreEqual(third, s.PopJob()); }
public void Test_PopJobWhileMaxed() { Scheduler s = new Scheduler(3, 10); Func<string[], int> funk = st => { Thread.Sleep(1); return 0; }; Mutex mutex = new Mutex(false, "Test_PopJobWhileMaxed"); for (int i = 1; i <= 30+1; i++) { s.AddJob(new Job(new User("test" + i, ""), 100, 1, funk)); } bool ready = false; bool mutexReleased = false; Thread blocker = new Thread(new ThreadStart(() => { mutex.WaitOne(); while (!ready) { Thread.Sleep(500); } mutex.ReleaseMutex(); mutexReleased = true; })); try { blocker.Start(); Job job = null; for (int i = 0; i < 3; i++) { job = s.PopJob(); Thread t = new Thread(new ThreadStart(() => job.Process(new String[] { "Test", "stuff" }))); t.Start(); while (t.ThreadState != ThreadState.WaitSleepJoin) { Thread.Sleep(500); } } try { s.PopJob(); Assert.Fail("It was possible to pop a job from scheduler while at maximum capacity. NOTE: this test does not work in release mode."); } catch (MaxedOutException) { // This is good } } catch { // To the finally block! } finally { ready = true; while (!mutexReleased) { Thread.Sleep(500); } } }
public void Test_RemoveJobInScheduler() { Scheduler s = new Scheduler(3, 10); Job job = new Job(new User("test", ""), 2000, 5, st => 10); s.AddJob(job); // Remove a job in the scheduler s.RemoveJob(job); Assert.AreEqual(0, s.JobsInSequence.Count); }