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 FindById_ThreadExecutor_ShouldWorkCorrectly() { //Arrange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 6, Priority.HIGH); Task task2 = new Task(6, 2, Priority.LOW); Task task3 = new Task(7, 4, Priority.LOW); Task task4 = new Task(0, 56, Priority.EXTREME); Task task5 = new Task(0, 56, Priority.EXTREME); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task3); executor.Execute(task4); Task result1 = executor.GetById(5); Task result2 = executor.GetById(6); Task result3 = executor.GetById(7); Task result4 = executor.GetById(0); //Assert Assert.AreSame(result1, task1); Assert.AreSame(result2, task2); Assert.AreSame(result3, task3); Assert.AreSame(result4, task4); Assert.AreNotSame(result4, task5); }
static void Main(string[] args) { var executor = new ThreadExecutor(); executor.Execute(new Task(12, 5, Priority.EXTREME)); executor.Execute(new Task(13, 10, Priority.EXTREME)); executor.Execute(new Task(14, 15, Priority.EXTREME)); Console.WriteLine(executor.Cycle(5)); }
public void GetByPriorityAndMinimumConsumption_ThreadExecutor_ShouldReturnEmptyCollection() { //Arange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(52, 5, Priority.LOW); Task task2 = new Task(56, 12, Priority.HIGH); Task task3 = new Task(58, 12, Priority.LOW); Task task4 = new Task(100, 51, Priority.HIGH); Task task5 = new Task(600, 15, Priority.MEDIUM); Task task6 = new Task(12, 5, Priority.EXTREME); Task task7 = new Task(125, 6, Priority.MEDIUM); Task task8 = new Task(0, 8, Priority.HIGH); List <Task> expected = new List <Task>(); //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); executor.ChangePriority(56, Priority.LOW); executor.ChangePriority(52, Priority.HIGH); executor.Cycle(12); //Assert List <Task> actual = executor.GetByPriorityAndMinimumConsumption(Priority.LOW, 10).ToList(); CollectionAssert.AreEqual(expected, actual); }
public void GetByPriority_ThreadExecutor_ShouldWorkCorrectly() { //Arange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(52, 5, Priority.HIGH); Task task2 = new Task(56, 12, Priority.HIGH); Task task3 = new Task(58, 12, Priority.LOW); Task task4 = new Task(100, 51, Priority.HIGH); Task task5 = new Task(600, 15, Priority.MEDIUM); Task task6 = new Task(12, 5, Priority.EXTREME); Task task7 = new Task(125, 6, Priority.MEDIUM); Task task8 = new Task(0, 8, Priority.LOW); List <Task> expected = new List <Task>() { task4, task2, task1 }; //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.AreEqual(8, executor.Count); //Assert List <Task> actual = executor.GetByPriority(Priority.HIGH).ToList(); CollectionAssert.AreEqual(expected, actual); }
public void AddingTask_ThreadExecutor_CountShouldIncrease() { //Arrange Task task1 = new Task(52, 12, Priority.EXTREME); Task task2 = new Task(13, 66, Priority.HIGH); IScheduler executor = new ThreadExecutor(); //Act executor.Execute(task1); executor.Execute(task2); //Assert Assert.AreEqual(2, executor.Count); }
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 Contains_ThreadExecutor_ShouldWorkCorrectly() { //Arrange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(52, 12, Priority.EXTREME); Task task2 = new Task(13, 66, Priority.HIGH); //Act executor.Execute(task1); executor.Execute(task2); //Assert Assert.AreEqual(2, executor.Count); Assert.True(executor.Contains(task2)); }
public void FindById_ThreadExecutor_ShouldWorkFast() { // Arrange IScheduler executor = new ThreadExecutor(); const int count = 100000; LinkedList <Task> tasks = new LinkedList <Task>(); for (int i = 0; i < count; i++) { tasks.AddLast(new Task(i + 1, i, Priority.HIGH)); executor.Execute(tasks.Last.Value); } // Act Stopwatch sw = Stopwatch.StartNew(); LinkedListNode <Task> node = tasks.First; while (node != null) { Assert.AreSame(node.Value, executor.GetById(node.Value.Id)); node = node.Next; } sw.Stop(); Assert.Less(sw.ElapsedMilliseconds, 250); }
public void Contains_100000_Elements_ShouldExecuteFast() { // Arrange IScheduler executor = new ThreadExecutor(); const int count = 100000; LinkedList <Task> tasks = new LinkedList <Task>(); for (int i = 0; i < count; i++) { tasks.AddLast(new Task(i, i, Priority.HIGH)); executor.Execute(tasks.Last.Value); } // Act Stopwatch sw = Stopwatch.StartNew(); LinkedListNode <Task> node = tasks.First; while (node != null) { Assert.True(executor.Contains(node.Value)); node = node.Next; } sw.Stop(); Assert.Less(sw.ElapsedMilliseconds, 250); }
public void Execute_ThreadExecutor_ShouldWorkFast() { //Arange const int items = 80_000; IScheduler executor = new ThreadExecutor(); Stopwatch watch = new Stopwatch(); //Act watch.Start(); Random rand = new Random(); for (int i = 0; i < items; i++) { executor.Execute(new Task(i, rand.Next(0, 2000), Priority.EXTREME)); } watch.Stop(); Assert.AreEqual(items, executor.Count); //Assert long elapsed = watch.ElapsedMilliseconds; Assert.Less(elapsed, 400); }
public void GetByConsumptionRange_ThreadExecutor_ShouldReturnAnEmptyCollection() { //Arange IScheduler executor = new ThreadExecutor(); IScheduler executor2 = new ThreadExecutor(); Task task1 = new Task(52, 5, Priority.HIGH); Task task2 = new Task(153, 7, Priority.LOW); //Act executor.Execute(task1); executor.Execute(task2); //Assert CollectionAssert.IsEmpty(executor2.GetByConsumptionRange(5, 6, true)); CollectionAssert.AreEqual(new List <Task>(), executor.GetByConsumptionRange(6, 6, true)); }
public void AddingExistingId_ThreadExecutor_ShouldThrowException() { //Arrange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 1, Priority.EXTREME); Task task2 = new Task(6, 1, Priority.HIGH); Task task3 = new Task(12, 5, Priority.MEDIUM); Task task4 = new Task(12, 5, Priority.HIGH); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task3); //Assert Assert.AreEqual(3, executor.Count); Assert.Throws <ArgumentException>(() => executor.Execute(task4)); }
public void FindById_ThreadExecutor_OnNonExistingId_ShouldThrow() { //Arrange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(5, 6, Priority.HIGH); Task task2 = new Task(6, 2, Priority.LOW); Task task3 = new Task(7, 4, Priority.LOW); Task task4 = new Task(0, 56, Priority.EXTREME); Task task5 = new Task(0, 56, Priority.EXTREME); //Act executor.Execute(task1); executor.Execute(task2); executor.Execute(task3); executor.Execute(task4); //Assert Assert.AreEqual(4, executor.Count); Assert.Throws <ArgumentException>(() => executor.GetById(12)); }
public void GetByConsumptionRange_ThreadExecutor_ShouldWorkFast() { //Arange const int items = 100_000; IScheduler executor = new ThreadExecutor(); Stopwatch watch = new Stopwatch(); List <Task> expected = new List <Task>(); Priority[] priorities = new Priority[] { Priority.LOW, Priority.MEDIUM, Priority.HIGH, Priority.EXTREME }; Random rand = new Random(); //Act for (int i = 0; i < items; i++) { Task task = new Task(i, rand.Next(0, 10000), priorities[rand.Next(0, 4)]); expected.Add(task); executor.Execute(task); } //do List <Tuple <int, int> > ranges = new List <Tuple <int, int> >(); List <List <Task> > tasks = new List <List <Task> >(); for (int i = 0; i < 100; i++) { int lower = rand.Next(2500, 5000); int upper = rand.Next(5005, 9999); ranges.Add(new Tuple <int, int>(lower, upper)); tasks.Add(expected .Where(x => x.Consumption >= lower && x.Consumption <= upper) .OrderBy(x => x.Consumption) .ThenByDescending(x => x.TaskPriority) .ToList() ); } watch.Start(); List <IEnumerable <Task> > actualTasks = new List <IEnumerable <Task> >(); for (int i = 0; i < 100; i++) { var range = ranges[i]; actualTasks.Add(executor.GetByConsumptionRange(range.Item1, range.Item2, true)); } watch.Stop(); //Assert Assert.Less(watch.ElapsedMilliseconds, 200); CollectionAssert.AreEqual(tasks, actualTasks); }
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); }
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 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 GetByPriorityAndMinimumConsumption_ThreadExecutor_ShouldWorkCorrectly_AfterCycle() { //Arange IScheduler executor = new ThreadExecutor(); Task task1 = new Task(52, 5, Priority.LOW); Task task2 = new Task(56, 12, Priority.LOW); Task task3 = new Task(58, 12, Priority.LOW); Task task4 = new Task(100, 14, Priority.LOW); Task task5 = new Task(600, 15, Priority.LOW); Task task6 = new Task(12, 5, Priority.LOW); Task task7 = new Task(125, 6, Priority.LOW); Task task8 = new Task(0, 8, Priority.EXTREME); List <Task> expected = new List <Task>() { task5 }; //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.AreEqual(8, executor.Count); executor.Cycle(5); executor.Cycle(5); executor.Cycle(4); //Assert List <Task> actual = executor.GetByPriorityAndMinimumConsumption(Priority.LOW, 10).ToList(); CollectionAssert.AreEqual(expected, actual); }
public void Cycle_ThreadExecutor_ShouldRemoveFast() { //Arange const int count = 100_000; IScheduler executor = new ThreadExecutor(); Stopwatch watch = new Stopwatch(); Random rand = new Random(); Priority[] priorities = new Priority[] { Priority.LOW, Priority.MEDIUM, Priority.HIGH, Priority.EXTREME }; List <Task> tasks = new List <Task>(); //Act for (int i = 1; i < count; i++) { Task t = new Task(i, i, priorities[rand.Next(0, 4)]); tasks.Add(t); executor.Execute(t); } watch.Start(); int totalCycles = 0; for (int i = 0; i < 1000; i++) { int cycles = rand.Next(10, 100); executor.Cycle(cycles); totalCycles += cycles; } List <Task> exepcted = tasks.Skip(totalCycles).ToList(); CollectionAssert.AreEqual(exepcted, executor.ToList()); watch.Stop(); //Assert Assert.Less(watch.ElapsedMilliseconds, 200); }
public void ChangePriority_ThreadExecutor_ShouldWorkFast() { //Arange const int items = 100_000; IScheduler executor = new ThreadExecutor(); Stopwatch watch = new Stopwatch(); Dictionary <Priority, List <Task> > dict = new Dictionary <Priority, List <Task> >(); dict.Add(Priority.LOW, new List <Task>()); dict.Add(Priority.MEDIUM, new List <Task>()); dict.Add(Priority.HIGH, new List <Task>()); dict.Add(Priority.EXTREME, new List <Task>()); Priority[] priorities = new Priority[] { Priority.LOW, Priority.MEDIUM, Priority.HIGH, Priority.EXTREME }; Random rand = new Random(); //Act for (int i = 0; i < items; i++) { Task task = new Task(i, rand.Next(0, 10000), priorities[rand.Next(0, 4)]); dict[task.TaskPriority].Add(task); executor.Execute(task); } dict[Priority.LOW] = dict[Priority.LOW].OrderByDescending(x => x.Id).ToList(); dict[Priority.MEDIUM] = dict[Priority.MEDIUM].OrderByDescending(x => x.Id).ToList(); dict[Priority.HIGH] = dict[Priority.HIGH].OrderByDescending(x => x.Id).ToList(); dict[Priority.EXTREME] = dict[Priority.EXTREME].OrderByDescending(x => x.Id).ToList(); //Act Priority p1 = priorities[rand.Next(0, 4)]; Priority p2 = priorities[rand.Next(0, 4)]; int randomCount = rand.Next(5000, 6000); List <Task> p2Tasks = dict[p2].Skip(randomCount - (randomCount / 2)).Take(randomCount).ToList(); List <Task> p1Tasks = dict[p1].Skip(randomCount - (randomCount / 2)).Take(randomCount).ToList(); watch.Start(); int min = Math.Min(p1Tasks.Count, p2Tasks.Count); for (int i = 0; i < min; i++) { executor.ChangePriority(p1Tasks[i].Id, p2); executor.ChangePriority(p2Tasks[i].Id, p1); } watch.Stop(); dict[p1].RemoveRange(randomCount - (randomCount / 2), randomCount); dict[p2].RemoveRange(randomCount - (randomCount / 2), randomCount); dict[p1].AddRange(p2Tasks); dict[p2].AddRange(p1Tasks); dict[p1] = dict[p1].OrderByDescending(x => x.Id).ToList(); dict[p2] = dict[p2].OrderByDescending(x => x.Id).ToList(); CollectionAssert.AreEqual(dict[p1], executor.GetByPriority(p1)); CollectionAssert.AreEqual(dict[p2], executor.GetByPriority(p2)); //Assert Assert.Less(watch.ElapsedMilliseconds, 200); }