コード例 #1
0
        /// <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());
            }
        }
コード例 #2
0
        public void Test_AddJob()
        {
            Scheduler s = new Scheduler();

            // Add legal jobs with the three types: SHORT, LONG, VERY_LONG
            Func<string[], int> funk = st => 10;
            Job shortJob = new Job(new User("test", ""), 25, 5, funk);
            Job longJob = new Job(new User("test", ""), 115, 5, funk);
            Job veryLongJob = new Job(new User("test", ""), 500, 5, funk);

            s.AddJob(shortJob);
            s.AddJob(longJob);
            s.AddJob(veryLongJob);

            Assert.AreEqual(3, s.JobsInSequence.Count);

            // Add 5000 jobs of varying type
            for (uint i = 0; i < 5000; i++)
            {
                s.AddJob(new Job(new User("test", ""), i, 5, funk));
            }

            Assert.AreEqual(5003, s.JobsInSequence.Count);
        }
コード例 #3
0
        public void Test_AddJobNull()
        {
            Scheduler s = new Scheduler();

            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);
                }
            }
        }
コード例 #4
0
 /// <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());
 }
コード例 #5
0
        public void Test_RemoveJobInScheduler()
        {
            Scheduler s = new Scheduler();
            Func<string[], int> funk = st => 10;
            Job job = new Job(new User("test", ""), 20, 5, funk);
            s.AddJob(job);

            // Remove a job in the scheduler
            s.RemoveJob(job);
            Assert.AreEqual(0, s.JobsInSequence.Count);
        }
コード例 #6
0
        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); }
            }
        }