public void FindByIndex_ThreadExecutor_ShouldWorkCorrectly() { //Arrange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 1, Priority.HIGH); Task task2 = new Task(6, 3, Priority.LOW); Task task3 = new Task(7, 6, Priority.LOW); Task task4 = new Task(8, 3, Priority.EXTREME); Task task5 = new Task(9, 5, Priority.MEDIUM); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task4); executor.Execute(task5); executor.Execute(task3); //Assert Assert.AreEqual(5, executor.Count); Assert.AreSame(task1, executor.GetByIndex(0)); Assert.AreSame(task2, executor.GetByIndex(1)); Assert.AreSame(task4, executor.GetByIndex(2)); Assert.AreSame(task5, executor.GetByIndex(3)); Assert.AreSame(task3, executor.GetByIndex(4)); }
public void Cycle_ThreadExecutor_ShouldWorkCorrectly() { //Arange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 5, Priority.HIGH); Task task2 = new Task(6, 5, Priority.LOW); Task task3 = new Task(7, 12, Priority.MEDIUM); Task task4 = new Task(12, 5, Priority.HIGH); Task task5 = new Task(15, 3, Priority.LOW); Task task6 = new Task(19, 2, Priority.LOW); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task3); executor.Execute(task4); executor.Execute(task5); executor.Execute(task6); Assert.AreEqual(6, executor.Count); executor.Cycle(3); Assert.AreEqual(4, executor.Count); Assert.Throws <ArgumentException>(() => executor.GetById(19)); Assert.Throws <ArgumentException>(() => executor.GetById(15)); executor.Cycle(5); //Assert Assert.AreEqual(1, executor.Count); Task t = executor.GetByIndex(0); Assert.AreSame(task3, t); Assert.Throws <ArgumentOutOfRangeException>(() => executor.GetByIndex(1)); }
public void FindByIndex_ThreadExecutor_ShouldThrowWhenOutOfBounds() { //Arrange IScheduler executor = new ThreadExecutor(); //Act Task task1 = new Task(5, 6, Priority.HIGH); Task task2 = new Task(6, 2, Priority.LOW); executor.Execute(task1); executor.Execute(task2); //Assert Assert.Throws <ArgumentOutOfRangeException>(() => executor.GetByIndex(-5)); Assert.Throws <ArgumentOutOfRangeException>(() => executor.GetByIndex(5)); }
public void FindByIndex_ThreadExecutor_ShouldWorkFast() { // Arrange IScheduler executor = new ThreadExecutor(); const int count = 10000; List <Task> tasks = new List <Task>(); for (int i = 0; i < count; i++) { tasks.Add(new Task(i, i, Priority.HIGH)); executor.Execute(tasks[i]); } // Act Stopwatch sw = Stopwatch.StartNew(); Random rand = new Random(); for (int i = 0; i < 10_000; i++) { int rnd = rand.Next(0, 1500); Assert.AreEqual(tasks[rnd], executor.GetByIndex(rnd)); } // Assert sw.Stop(); Assert.Less(sw.ElapsedMilliseconds, 150); }
public void FindByIndex_ThreadExecutor_ShouldThrowWhenEmpty() { //Arrange IScheduler executor = new ThreadExecutor(); //Act //Assert Assert.Throws <ArgumentOutOfRangeException>(() => executor.GetByIndex(0)); }
public void Addition_ThreadExecutor_ShouldAddCorrectTasks() { //Arrange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 1, Priority.EXTREME); Task task2 = new Task(6, 5, Priority.HIGH); Task task3 = new Task(12, 12, Priority.LOW); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task3); //Assert Task result1 = executor.GetByIndex(0); Task result2 = executor.GetByIndex(1); Task result3 = executor.GetByIndex(2); Assert.AreSame(task1, result1); Assert.AreSame(task2, result2); Assert.AreSame(task3, result3); }
public void Cycle_ThreadExecutor_ShouldReturnCorrectRemovalCount() { //Arange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 5, Priority.HIGH); Task task2 = new Task(6, 5, Priority.LOW); Task task3 = new Task(7, 12, Priority.MEDIUM); Task task4 = new Task(12, 5, Priority.HIGH); Task task5 = new Task(15, 3, Priority.HIGH); Task task6 = new Task(19, 2, Priority.EXTREME); Task task7 = new Task(23, 16, Priority.LOW); Task task8 = new Task(73, 6, Priority.LOW); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task3); executor.Execute(task4); executor.Execute(task5); executor.Execute(task6); executor.Execute(task7); executor.Execute(task8); //Assert Assert.AreEqual(8, executor.Count); List <Task> expectedStructure = new List <Task>() { task1, task2, task3, task4, task5, task6, task7, task8 }; List <Task> actualStructure = new List <Task>(); executor.ToList().ForEach(x => actualStructure.Add(x)); CollectionAssert.AreEqual(expectedStructure, actualStructure); Assert.Throws <ArgumentOutOfRangeException>(() => executor.GetByIndex(-5)); int cycleCount = executor.Cycle(5); Assert.AreEqual(5, cycleCount); cycleCount = executor.Cycle(12); Assert.AreEqual(3, cycleCount); Assert.AreEqual(0, executor.Count); expectedStructure = new List <Task>() { }; actualStructure = new List <Task>(); executor.ToList().ForEach(x => actualStructure.Add(x)); CollectionAssert.AreEqual(expectedStructure, actualStructure); }