Пример #1
0
 public void Test_CompareToPositive()
 {
     Job job1 = new Job(new User("Test", ""), 100, 1, s => 0);
     Job job2 = new Job(new User("Test", ""), 100, 2, s => 0);
     job1.Process(new String[] { " not empty" });
     Assert.AreEqual(1, job1.CompareTo(job2));
 }
Пример #2
0
        /// <summary>
        /// Add a log to the database
        /// </summary>
        /// <param name="job">The job affecting the log</param>
        /// <param name="time">The timestamp for the log</param>
        public void AddLog(Job job, DateTime time)
        {
            if (job == null)
                throw new ArgumentNullException("job was null");
            if (job.User == null)
                throw new ArgumentNullException("job.User was null");
            if (job.JobId == 0)
                throw new ArgumentNullException("job.JobId was 0");
            if (time == null)
                throw new ArgumentNullException("time was null");

            lock (_objectLock)
            {
                var dbContext = new TaskManagerModelContainer();
                Debug.WriteLine("1 - Inside Log.AddJob(); Event: " + job.StateString + "; jobId: " + job.JobId );
                Log log = new Log();
                log.Event = job.StateString;
                log.Time = time;
                log.JobId = job.JobId;

                dbContext.Logs.AddObject(log);
                Debug.WriteLine("2 - Inside Log.AddJob(); State: " + job.StateString + "; jobId: " + job.JobId + "; logId: " + log.LogId);
                dbContext.SaveChanges();
            }
        }
Пример #3
0
        /// <summary>
        /// Add a job to the database
        /// </summary>
        /// <param name="job">The job to add</param>
        public void AddJob(Job job)
        {
            if (job == null)
                throw new ArgumentNullException("job was null");
            if(job.User == null)
                throw new ArgumentNullException("job.User was null");

            Debug.WriteLine("1 - Inside AddJob(); jobId: " + job.JobId);

            if (job.UserId == 0)
            {
                AddUser(job.User);
            }
            else if (job.JobId == 0)
            {
                lock (_objectLock)
                {
                    _dbContext.Jobs.AddObject(job);

                    Debug.WriteLine("2 - Inside AddJob(); jobId: " + job.JobId);

                    _dbContext.SaveChanges();
                }
            }
        }
Пример #4
0
        public void Test_AddJob()
        {
            User user = new User("Test User", "Test Password");
            _tmDAO.AddUser(user);
            Job job = new Job(user, 100, 5, s => 50);

            _tmDAO.AddJob(job);
        }
Пример #5
0
 /// <summary>
 /// Processes the job.
 /// </summary>
 /// <param name="job">The job.</param>
 public void ProcessJob(Job job)
 {
     Thread t = new Thread(new ThreadStart(() =>
     {
         IncrementActiveCpu(job.CPUs);
         Thread.Sleep(job.TimeExpected + _runningTimeDiscrepency.Next(-200, 200));
         DecrementActiveCpu(job.CPUs);
     }));
     t.Start();
 }
Пример #6
0
 public void EventHandlerRunningTest()
 {
     Job job = new Job(new User("test", ""), 1000, 1, s => 0);
     _b.JobCancelled += delegate { throw new Exception(); };
     _b.SubmitJob(job);
     try
     {
         _b.ExecuteAll();
         Assert.Fail();
     }
     catch (Exception)
     {
     }
 }
Пример #7
0
        public void CancelJobTest()
        {
            User user = new User("test", "");
            Job job = new Job(user, 100, 1, s => 0);
            _b.SubmitJob(job);

            //If job does anything else than runs or cancels...
            job.JobDone += delegate { Assert.Fail();};
            job.JobFailed += delegate { Assert.Fail(); };
            job.JobTerminated += delegate { Assert.Fail(); };

            _b.CancelJob(job);

            Assert.AreEqual(job.State, JobState.Cancelled);
        }
Пример #8
0
        public void Test_PopJobSequence()
        {
            Scheduler s = new Scheduler(3, 10);
            Func<string[], int> funk = st => 10;

            // Check the sequence og jobs returned
            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);

            // short, long, verylong
            AssertSequence(s, shortJob, longJob, veryLongJob);
            // long, short, verylong
            AssertSequence(s, longJob, shortJob, veryLongJob);
            // long, verylong, short
            AssertSequence(s, longJob, veryLongJob, shortJob);
            // short, verylong, long
            AssertSequence(s, shortJob, veryLongJob, longJob);
            // verylong, short, long
            AssertSequence(s, veryLongJob, shortJob, longJob);
            // verylong, long, short
            AssertSequence(s, veryLongJob, longJob, shortJob);
        }
Пример #9
0
        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);
        }
Пример #10
0
 public void Test_CompareToTransitive()
 {
     Job job1 = new Job(new User("Test", ""), 100, 1, s => 0);
     Job job2 = new Job(new User("Test", ""), 100, 2, s => 0);
     Job job3 = new Job(new User("Test", ""), 100, 3, s => 0);
     Assert.AreEqual(0, job1.CompareTo(job2));
     Assert.AreEqual(0, job2.CompareTo(job3));
     Assert.AreEqual(0, job1.CompareTo(job3));
 }
Пример #11
0
 public void Test_CompareZero()
 {
     Job job1 = new Job(new User("Test", ""), 100, 1, s => 0);
     Job job2 = new Job(new User("Test", ""), 100, 2, s => 0);
     Assert.AreEqual(0, job1.Compare(job2, job1));
 }
Пример #12
0
        /// <summary>
        /// Store the job, to let the owner access it later on
        /// </summary>
        /// <param name="job">The job to store</param>
#if DEBUG
        internal void StoreJob(Job job)
Пример #13
0
        private void StoreJob(Job job)
#endif
        {
            Contract.Requires(job != null);
            Contract.Requires(job.User != null);
            Contract.Assume(!_jobs.ContainsKey(job.User) || _jobs[job.User] != null);

            lock (_tmDAOLock)
                _tmDAO.AddJob(job);

            lock (_activityLock)
            {
                if (!_jobs.ContainsKey(job.User)) _jobs.Add(job.User, new List<Job>());
                _jobs[job.User].Add(job);
            }
        }
Пример #14
0
        public void StoreJobTest()
        {
            int expectedTime = 100; //Milliseconds
            User owner = new User("test", "");
            Job job = new Job(owner, (uint)expectedTime, 1, s => 0);
            _b.SubmitJob(job);

            IEnumerable<Job> jobs = _b.GetJobs(owner);

            if(!jobs.Contains(job)) Assert.Fail("The job was not stored!");
        }
Пример #15
0
        /// <summary>
        /// Cancel a waiting job
        /// </summary>
        /// <param name="job">The job to remove from the queue</param>
        public void CancelJob(Job job)
        {
            Contract.Requires(job != null);
            Contract.Requires(job.State == JobState.Waiting);

            lock (_stateLock)
            {
                job.CancelJob();

                EventHandler handler = JobCancelled;
                if (handler != null)
                    handler(job, EventArgs.Empty);
            }
        }
Пример #16
0
        private void AddJobs(uint amount)
        {
            for (uint index = 0; index < amount; index++)
            {
                uint time = (uint)_random.Next(100, 5000);
                int cpus = _random.Next(1, 6);
                Job job = new Job(_user, time, cpus, s => 0);

                _jobs.Enqueue(job);
            }

            _jobsAdded += amount;
        }
Пример #17
0
 /******************************************************************************************
  * The following methods relate to events
  * ****************************************************************************************/
 /// <summary>
 /// Subscribes from all event handlers related to parameter job
 /// </summary>
 /// <param name="job">The job to subscribe to</param>
 private void Subscribe(Job job)
 {
     job.JobRunning += OnJobRunning;
     job.JobCancelled += OnJobCancelled;
     job.JobTerminated += OnJobTerminated;
     job.JobFailed += OnJobFailed;
     job.JobDone += OnJobDone;
 }
Пример #18
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());
 }
Пример #19
0
 /// <summary>
 /// Unsubscribes from all event handlers related to parameter job
 /// </summary>
 /// <param name="job">The job to unsubscribe to</param>
 private void Unsubscribe(Job job)
 {
     job.JobRunning -= OnJobRunning;
     job.JobCancelled -= OnJobCancelled;
     job.JobTerminated -= OnJobTerminated;
     job.JobFailed -= OnJobFailed;
     job.JobDone -= OnJobDone;
 }
Пример #20
0
 public void Test_RemoveJobNoTInScheduler()
 {
     Scheduler s = new Scheduler(3, 10);
     Job phonyJob = new Job(new User("test", ""), 2500, 5, st => 10);
     try
     {
         s.RemoveJob(phonyJob);
         Assert.Fail("It was possible to remove a job not in the scheduler");
     }
     catch (ArgumentException)
     {
         // This is good
     }
 }
Пример #21
0
        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);
        }
Пример #22
0
 public void GetJobsTest()
 {
     User owner = new User("test", "");
     Job job = new Job(owner, 1000, 1, s => 0);
     _b.SubmitJob(job);
     _b.ExecuteAll();
     IEnumerable<Job> jobs = _b.GetJobs(owner);
     if (!jobs.Contains(job)) Assert.Fail();
 }
Пример #23
0
 public void Test_ProcessEmptyArgs()
 {
     Job job = new Job(new User("Test", ""), 100, 1, s => 0);
     job.Process(new String[] { });
 }
Пример #24
0
        public void FinishCurrentJobsTest()
        {
            uint numberOfJobs = 5;
            List<Job> jobs = new List<Job>((int)numberOfJobs);

            uint counter = 0;
            Job job;
            for (int index = 0; index < numberOfJobs; index++)
            {
                job = new Job(new User("test", ""), 100, 1, s => 0) { Description = "Job " + index };
                job.JobDone += delegate { counter++; };
                jobs.Add(job);
                _b.SubmitJob(job);

            }

            _b.JobDone += delegate
            {
                if (counter == numberOfJobs)
                    Assert.AreEqual(BSStatus.Stopped, _b.Status);
            };
            _b.ExecuteAll();
            _b.FinishCurrentJobs();
        }
Пример #25
0
 public void Test_ProcessNullArgs()
 {
     Job job = new Job(new User("Test", ""), 100, 1, s => 0);
     try
     {
         job.Process(null);
     }
     catch
     {
         // This is good
     }
 }
Пример #26
0
 /// <summary>
 /// Terminate a running job
 /// </summary>
 /// <param name="job">The job to terminate</param>
 public void TerminateJob(Job job)
 {
     Contract.Requires(job != null);
     Contract.Requires(job.State == JobState.Running);
     Contract.Ensures(job.State == JobState.Terminated);
     lock (_activityLock)
     {
         job.Terminate();
     }
 }
Пример #27
0
 public void Test_CompareToSymmetric()
 {
     Job job1 = new Job(new User("Test", ""), 100, 1, s => 0);
     Job job2 = new Job(new User("Test", ""), 100, 2, s => 0);
     Assert.AreEqual(0, job1.CompareTo(job2));
     Assert.AreEqual(0, job2.CompareTo(job1));
 }
Пример #28
0
 public void SubmitJobTest()
 {
     Job job = new Job(new User("test", ""), 1000, 1, s => 0);
     _b.SubmitJob(job);
     IEnumerable<Job> list = _b.GetJobs(job.User);
     Assert.IsTrue(list.Contains(job));
 }
Пример #29
0
        /// <summary>
        /// Add job to queue
        /// </summary>
        /// <param name="job">The job to be added</param>
        public void SubmitJob(Job job)
        {
            Contract.Requires(job != null);
            Contract.Requires(job.User != null);            

            StoreJob(job);
            lock (_schedulerLock)
                _scheduler.AddJob(job);

            EventHandler handler = JobSubmitted;
            if (handler != null)
                handler(job, EventArgs.Empty);
            StartJobFromEvent();
        }
Пример #30
0
        public void ExecuteAllTest()
        {
            //Also tests StartNextJob()
            uint numbOfJobs = 100;

            //Using the event from this job to see when all jobs are handled
            Job lastJob = null;

            User owner = new User("test", "");
            for (uint index = 0; index < numbOfJobs; index++)
            {
                Job job = new Job(owner, 100, 1, s => 0);
                _b.SubmitJob(job);
                lastJob = job;
            }

            IEnumerable jobs = _b.GetJobs(owner);

            uint counterDone = 0;
            uint counterUndone = 0;

            foreach (Job j in jobs)
                if (JobState.Done == j.State)
                    counterDone++;
                else
                    counterUndone++;

            //No jobs should be done
            Assert.AreEqual((uint)0, counterDone);
            Assert.AreEqual(numbOfJobs, counterUndone);

            _b.ExecuteAll();

            //Will let the test try to finish for 5 second, which should be more than enough!
            int counter = 0;
            while (_b.Status != BSStatus.Ready)
            {
                if (counter++ > 50) Assert.Fail("Benchmark was unable to reach the state: Ready");
                Thread.Sleep(100);
            }

            counterDone = 0;
            counterUndone = 0;

            foreach (Job j in jobs)
                if (j.State == JobState.Done)
                    counterDone++;
                else
                    counterUndone++;

            Assert.AreEqual(numbOfJobs, counterDone);
            Assert.AreEqual((uint)0, counterUndone);
        }