/// <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_IsSlotAvailableWithoutRunningJobs() { Scheduler s = new Scheduler(); // With no running jobs, should return true Assert.IsTrue(s.IsSlotAvailable()); }