public void EventHandlerRunningTest() { Job job = new Job(new Owner("test"), 1); _b.JobCancelled += delegate { throw new Exception(); }; _b.SubmitJob(job); try { _b.ExecuteAll(); Assert.Fail(); } catch (Exception) { } }
public void CancelJobTest() { Owner owner = new Owner("Wing 3714 - The ladies man"); Job job = new Job(owner, 5); _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); }
public void Test_AddJob() { BenchmarkSystem.Scheduler s = new BenchmarkSystem.Scheduler(); // Add legal jobs with the three types: SHORT, LONG, VERY_LONG Job shortJob = new Job(new Owner("test"), 25); Job longJob = new Job(new Owner("test"), 115); Job veryLongJob = new Job(new Owner("test"), 500); 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 Owner("test"), i)); } Assert.AreEqual(5003, s.JobsInSequence.Count); }
/// <summary> /// Lets the user add a new job to the system /// </summary> private void AddNewJob() { int CPUs = 0; string description; int expectedRuntime; System.Console.WriteLine("To add a new job - please fill in:"); System.Console.Write("1: A short description: "); description = System.Console.ReadLine(); bool validInputTime = false; do { System.Console.Write("2: Expected runtime in minutes: "); validInputTime = Int32.TryParse(System.Console.ReadLine(), out expectedRuntime); if (!validInputTime) System.Console.WriteLine("Invalid input - must be a digit above 0.\n"); } while (!validInputTime); bool validInputCPUs = false; do { System.Console.Write("3: CPUs to use (1-6): "); validInputCPUs = Int32.TryParse(System.Console.ReadLine(), out CPUs); if (1 > CPUs || CPUs > 6) validInputCPUs = false; if (!validInputCPUs) System.Console.WriteLine("Invalid input - Must be a digit from 1 to 6.\n"); } while (!validInputCPUs); System.Console.WriteLine(); Job job = new Job(_user.Owner, (uint)expectedRuntime) { Description = description, CPUs = (uint)CPUs }; _benchmark.SubmitJob(job); System.Console.WriteLine("The job has been submitted"); }
/// <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); Contract.Ensures(_jobs[job.Owner].Contains(job)); job.CancelJob(); EventHandler handler = JobCancelled; if (handler != null) handler(job, EventArgs.Empty); }
public void CPUTest() { Job job = new Job(new Owner("as"), 30); job.CPUs = 1; }
public void SubmitJobTest() { Job job = new Job(new Owner("test"), 30); _b.SubmitJob(job); List<Job> list = _b.GetJobs(job.Owner); Assert.IsTrue(list.Contains(job)); }
public void GetJobsTest() { Owner owner = new Owner("Arnold"); Job job = new Job(owner, 56); _b.SubmitJob(job); _b.ExecuteAll(); List<Job> jobs = _b.GetJobs(owner); if (!jobs.Contains(job)) Assert.Fail(); }
public void ExecuteAllTest() { //Also tests StartNextJob() uint numbOfJobs = 100; int timeToComplete = 1000; Job job; Owner owner = new Owner("TestOwner"); for (uint index = 0; index < numbOfJobs; index++) { job = new Job(owner, index); _b.SubmitJob(job); } List<Job> jobs = _b.GetJobs(owner); uint counterDone = 0; uint counterUndone = 0; foreach (Job j in jobs) if (j.State != JobState.Done) counterUndone++; else counterDone++; Assert.AreEqual(counterDone, (uint)0); Assert.AreEqual(counterUndone, numbOfJobs); _b.ExecuteAll(); System.Threading.Thread.Sleep(timeToComplete); counterDone = 0; counterUndone = 0; foreach (Job j in jobs) if (j.State != JobState.Done) counterUndone++; else counterDone++; Assert.AreEqual(counterDone, numbOfJobs); Assert.AreEqual(counterUndone, (uint)0); }
/// <summary> /// Check if the counter associated with parameter job's type is below the maximum /// </summary> /// <param name="job">The job</param> /// <returns>True if counter associated with parameter job's type is below the maximum, false otherwise</returns> private bool BelowMax(Job job) { Contract.Assume(job != null); if (_counterByJobtype[job.Type] < 20) { return true; } return false; }
/// <summary> /// Removes the job from the list /// </summary> /// <param name="list">The list containing jobs</param> /// <param name="job">The job to remove from the list</param> private void ExcludeJob(List<Job> list, Job job) { _jobSequence.Remove(job); list.Remove(job); }
/// <summary> /// Returns the list containing the job. /// </summary> /// <param name="job">The job who's to look for</param> /// <returns>List containing job. If job is not contained in any list this method returns null</returns> private List<Job> ContainedInList(Job job) { Contract.Assume(_listByJobtype[job.Type] != null); List<Job> list = _listByJobtype[job.Type]; return list; }
/// <summary> /// Removes the job from the scheduler /// </summary> /// <param name="job">The job to remove from the scheduler</param> /// <exception cref="ArgumentNullException">If parameter is null</exception> /// <exception cref="ArgumentException">If job is not contained in scheduler</exception> public void RemoveJob(Job job) { if (job == null) { throw new ArgumentNullException(); } List<Job> list = ContainedInList(job); if (list.Count == 0) { throw new ArgumentException("Job is not contained in scheduler"); } ExcludeJob(list, job); Unsubscribe(job); }
/// <summary> /// Adds a job to the scheduler /// </summary> /// <param name="job">The job to add to the scheduler</param> /// <exception cref="ArgumentNullException">If parameter is null</exception> public void AddJob(Job job) { Contract.Requires(job != null); Contract.Assume(_listByJobtype[job.Type] != null); _listByJobtype[job.Type].Add(job); _jobSequence.Add(job); Subscribe(job); }
private void StoreJob(Job job) #endif { Contract.Requires(job != null); Contract.Requires(job.Owner != null); Contract.Ensures(_jobs[job.Owner].Contains(job)); Contract.Assume(!_jobs.ContainsKey(job.Owner) || _jobs[job.Owner] != null); if (!_jobs.ContainsKey(job.Owner)) _jobs.Add(job.Owner, new List<Job>()); _jobs[job.Owner].Add(job); }
/// <summary> /// Store the job, to let the owner access it later on /// </summary> /// <param name="o">The owner of the job</param> /// <param name="job">The job to store</param> #if DEBUG internal void StoreJob(Job job)
public void Test_PopJob() { BenchmarkSystem.Scheduler s = new BenchmarkSystem.Scheduler(); // Check the sequence og jobs returned Job shortJob = new Job(new Owner("test"), 25); Job longJob = new Job(new Owner("test"), 115); Job veryLongJob = new Job(new Owner("test"), 500); // 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); // Pop a job when there are none in the 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 } //// Below is meant for when threads are introduced to the program //// Pop a job when at maximum simultaneous running jobs //for (int i = 0; i < 21; i++) //{ // s.AddJob(new Job(new Owner("test" + i), 10)); // s.AddJob(new Job(new Owner("test" + i), 100)); // s.AddJob(new Job(new Owner("test" + i), 500)); //} //// Start 60 jobs, leaving 3 untouched in scheduler //for (int i = 0; i < 60; i++) //{ // Job job = s.PopJob(); // job.Process(new String[]{"Test", "stuff"}); //} //try //{ // s.PopJob(); // Assert.Fail("It was possible to pop a job from scheduler even though the maximum simultaneously running jobs were reached"); //} //catch (InvalidOperationException) //{ // // This is good //} }
/// <summary> /// Decrements the counter associated with parameter job, making room for another job /// </summary> /// <param name="job">Job with property Type = {SHORT, LONG, VERY_LONG}</param> private void DecrementRunningJobs(Job job) { _counterByJobtype[job.Type]--; }
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 Owner("test"), 1) { Description = "Job " + index }; job.JobDone += delegate { counter++; }; jobs.Add(job); _b.SubmitJob(job); } _b.ExecuteAll(); _b.FinishCurrentJobs(); //Wait to all the jobs are done (tested every ½ second) while (counter < numberOfJobs) { //Will make more sense, when the threading is implemented System.Threading.Thread.Sleep(500); } Assert.AreEqual(BSStatus.Stopped, _b.Status); }
/****************************************************************************************** * 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; }
public void StoreJobTest() { int expectedTime = 1; //Minutes Owner owner = new Owner("Undercover Brother"); Job job = new Job(owner, (uint)expectedTime); _b.SubmitJob(job); List<Job> jobs = _b.GetJobs(owner); foreach (Job j in jobs) if (j.State != JobState.Waiting) Assert.Fail(); job.JobDone += delegate { List<Job> newJobs = _b.GetJobs(owner); foreach (Job newJ in newJobs) if (newJ.State != JobState.Done) Assert.Fail(); }; _b.ExecuteAll(); }
/// <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; }
/// <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.Ensures(_jobs[job.Owner].Contains(job)); StoreJob(job); _scheduler.AddJob(job); if(JobSubmitted != null) JobSubmitted(job, EventArgs.Empty); StartJobFromEvent(); }
public void CancelJob([PexAssumeUnderTest]BenchmarkSystem target, Job job) { target.CancelJob(job); // TODO: add assertions to method BenchmarkSystemTest.CancelJob(BenchmarkSystem, Job) }
public Job Constructor(Owner o, uint time) { Job target = new Job(o, time); return target; // TODO: add assertions to method JobTest.Constructor(Owner, UInt32) }
public void Test_RemoveJob() { BenchmarkSystem.Scheduler s = new BenchmarkSystem.Scheduler(); Job job = new Job(new Owner("test"), 20); s.AddJob(job); // Try to remove null try { s.RemoveJob(null); Assert.Fail("It was possible to remove a \"null\" job"); } catch (ArgumentNullException) { // This is good } // Try to remove job not in scheduler Job phonyJob = new Job(new Owner("test"), 50); try { s.RemoveJob(phonyJob); Assert.Fail("It was possible to remove a job not in the scheduler"); } catch (ArgumentException) { // This is good } // Remove a job in the scheduler s.RemoveJob(job); Assert.AreEqual(0, s.JobsInSequence.Count); }
public void FinishCurrentJobs249() { Owner owner; Dictionary<Owner, List<Job>> dictionary; List<Job> list; Dictionary<JobType, List<Job>> dictionary1_; Dictionary<JobType, uint> dictionary2_; BenchmarkSystem.Scheduler scheduler; SBenchmarkSystem sBenchmarkSystem; SLogger sLogger; BenchmarkSystem benchmarkSystem; owner = PexInvariant.CreateInstance<Owner>(); PexInvariant.SetField<string>((object)owner, "<Name>k__BackingField", "\0"); PexInvariant.CheckInvariant((object)owner); dictionary = new Dictionary<Owner, List<Job>>(0); dictionary[owner] = (List<Job>)null; Job[] jobs = new Job[0]; list = new List<Job>((IEnumerable<Job>)jobs); dictionary1_ = new Dictionary<JobType, List<Job>>(4); dictionary1_[JobType.SHORT] = (List<Job>)null; dictionary2_ = new Dictionary<JobType, uint>(1); dictionary2_[JobType.SHORT] = 0u; scheduler = PexInvariant.CreateInstance<BenchmarkSystem.Scheduler>(); PexInvariant.SetField<uint>((object)scheduler, "_shortJobsRunning", 0u); PexInvariant.SetField<uint>((object)scheduler, "_longJobsRunning", 0u); PexInvariant.SetField<uint>((object)scheduler, "_veryLongJobsRunning", 0u); PexInvariant.SetField<List<Job>>((object)scheduler, "_shortList", list); PexInvariant.SetField<List<Job>>((object)scheduler, "_longList", list); PexInvariant.SetField<List<Job>>((object)scheduler, "_veryLongList", list); PexInvariant.SetField<List<Job>>((object)scheduler, "_jobSequence", list); PexInvariant.SetField<Dictionary<JobType, List<Job>>> ((object)scheduler, "_listByJobtype", dictionary1_); PexInvariant.SetField<Dictionary<JobType, uint>> ((object)scheduler, "_counterByJobtype", dictionary2_); PexInvariant.SetField<EventHandler> ((object)scheduler, "JobDone", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)scheduler, "JobFailed", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)scheduler, "JobTerminated", (EventHandler)null); PexInvariant.CheckInvariant((object)scheduler); sBenchmarkSystem = new SBenchmarkSystem(); sLogger = new SLogger((BenchmarkSystem)sBenchmarkSystem); benchmarkSystem = PexInvariant.CreateInstance<BenchmarkSystem>(); PexInvariant.SetField<BenchmarkSystem.Scheduler> ((object)benchmarkSystem, "_scheduler", scheduler); PexInvariant.SetField<Logger> ((object)benchmarkSystem, "_logger", (Logger)sLogger); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobSubmitted", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobCancelled", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobRunning", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobTerminated", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobFailed", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobDone", (EventHandler)null); PexInvariant.SetField<List<Job>>((object)benchmarkSystem, "_threads", list); PexInvariant.SetField<Dictionary<Owner, List<Job>>> ((object)benchmarkSystem, "_jobs", dictionary); PexInvariant.SetField<bool>((object)benchmarkSystem, "_isFinishing", false); PexInvariant.SetField<BSStatus> ((object)benchmarkSystem, "<Status>k__BackingField", BSStatus.Ready); PexInvariant.CheckInvariant((object)benchmarkSystem); this.FinishCurrentJobs(benchmarkSystem); Assert.IsNotNull((object)benchmarkSystem); Assert.AreEqual<BSStatus>(BSStatus.Stopped, benchmarkSystem.Status); }
/// <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(BenchmarkSystem.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 SubmitJobThrowsContractException506() { try { Owner owner; Dictionary<Owner, List<Job>> dictionary; List<Job> list; Dictionary<JobType, List<Job>> dictionary1_; Dictionary<JobType, uint> dictionary2_; BenchmarkSystem.Scheduler scheduler; SBenchmarkSystem sBenchmarkSystem; SLogger sLogger; BenchmarkSystem benchmarkSystem; owner = PexInvariant.CreateInstance<Owner>(); PexInvariant.SetField<string>((object)owner, "<Name>k__BackingField", "\0"); PexInvariant.CheckInvariant((object)owner); dictionary = new Dictionary<Owner, List<Job>>(0); dictionary[owner] = (List<Job>)null; Job[] jobs = new Job[0]; list = new List<Job>((IEnumerable<Job>)jobs); dictionary1_ = new Dictionary<JobType, List<Job>>(4); dictionary1_[JobType.SHORT] = (List<Job>)null; dictionary2_ = new Dictionary<JobType, uint>(1); dictionary2_[JobType.SHORT] = 0u; scheduler = PexInvariant.CreateInstance<BenchmarkSystem.Scheduler>(); PexInvariant.SetField<uint>((object)scheduler, "_shortJobsRunning", 0u); PexInvariant.SetField<uint>((object)scheduler, "_longJobsRunning", 0u); PexInvariant.SetField<uint>((object)scheduler, "_veryLongJobsRunning", 0u); PexInvariant.SetField<List<Job>>((object)scheduler, "_shortList", list); PexInvariant.SetField<List<Job>>((object)scheduler, "_longList", list); PexInvariant.SetField<List<Job>>((object)scheduler, "_veryLongList", list); PexInvariant.SetField<List<Job>>((object)scheduler, "_jobSequence", list); PexInvariant.SetField<Dictionary<JobType, List<Job>>> ((object)scheduler, "_listByJobtype", dictionary1_); PexInvariant.SetField<Dictionary<JobType, uint>> ((object)scheduler, "_counterByJobtype", dictionary2_); PexInvariant.SetField<EventHandler> ((object)scheduler, "JobDone", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)scheduler, "JobFailed", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)scheduler, "JobTerminated", (EventHandler)null); PexInvariant.CheckInvariant((object)scheduler); sBenchmarkSystem = new SBenchmarkSystem(); sLogger = new SLogger((BenchmarkSystem)sBenchmarkSystem); benchmarkSystem = PexInvariant.CreateInstance<BenchmarkSystem>(); PexInvariant.SetField<BenchmarkSystem.Scheduler> ((object)benchmarkSystem, "_scheduler", scheduler); PexInvariant.SetField<Logger> ((object)benchmarkSystem, "_logger", (Logger)sLogger); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobSubmitted", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobCancelled", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobRunning", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobTerminated", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobFailed", (EventHandler)null); PexInvariant.SetField<EventHandler> ((object)benchmarkSystem, "JobDone", (EventHandler)null); PexInvariant.SetField<List<Job>>((object)benchmarkSystem, "_threads", list); PexInvariant.SetField<Dictionary<Owner, List<Job>>> ((object)benchmarkSystem, "_jobs", dictionary); PexInvariant.SetField<bool>((object)benchmarkSystem, "_isFinishing", false); PexInvariant.SetField<BSStatus> ((object)benchmarkSystem, "<Status>k__BackingField", BSStatus.Ready); PexInvariant.CheckInvariant((object)benchmarkSystem); this.SubmitJob(benchmarkSystem, (Job)null); throw new AssertFailedException("expected an exception of type ContractException"); } catch(Exception ex) { if (!PexContract.IsContractException(ex)) throw ex; } }
/// <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); job.Terminate(); }