/// <summary> /// With maximum simultaneous running jobs /// </summary> //[TestMethod] public void Test_IsSlotAvailableWhileMaxed() { Scheduler s = new Scheduler(); Func<string[], int> funk = st => { Thread.Sleep(500); return 0; }; Mutex mutex = new Mutex(false, "Test_PopJobWhileMaxed"); // Insert 63 jobs into the scheduler for (int i = 0; i < 21; i++) { s.AddJob(new Job(new User("test" + i, ""), 10, 5, funk)); s.AddJob(new Job(new User("test" + i, ""), 100, 5, funk)); s.AddJob(new Job(new User("test" + i, ""), 500, 5, 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); } } } catch { // To the finally block! } finally { ready = true; while (!mutexReleased) { Thread.Sleep(500); } Assert.IsFalse(s.IsSlotAvailable()); } }
public void Test_PopJobWithNoJobs() { Scheduler s = new Scheduler(); try { s.PopJob(); Assert.Fail("It was possible to pop a job when there were no jobs to pop"); } catch (JobsDepletedException) { // This is good } }
/// <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(); Func<string[], int> funk = st => 0; Mutex mutex = new Mutex(false, "Test_PopJobWhileMaxed"); // Insert 63 jobs into the scheduler for (int i = 0; i < 21; i++) { s.AddJob(new Job(new User("test" + i, ""), 10, 5, funk)); s.AddJob(new Job(new User("test" + i, ""), 100, 5, funk)); s.AddJob(new Job(new User("test" + i, ""), 500, 5, 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."); } catch (InvalidOperationException) { // This is good } } catch { // To the finally block! } finally { ready = true; while (!mutexReleased) { Thread.Sleep(500); } } }