Esempio n. 1
0
    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);
    }
Esempio n. 2
0
    public void GetByPriorityAndMinimumConsumption_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, Priority> > ranges = new List <Tuple <int, Priority> >();
        List <List <Task> >           tasks  = new List <List <Task> >();

        for (int i = 0; i < 100; i++)
        {
            int      lower    = rand.Next(1000, 10000);
            Priority priority = priorities[rand.Next(0, 4)];

            ranges.Add(new Tuple <int, Priority>(lower, priority));
            tasks.Add(expected
                      .Where(x => x.Consumption >= lower && x.TaskPriority == priority)
                      .OrderByDescending(x => x.Id)
                      .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.GetByPriorityAndMinimumConsumption(range.Item2, range.Item1));
        }

        watch.Stop();

        //Assert
        Assert.Less(watch.ElapsedMilliseconds, 200);
        CollectionAssert.AreEqual(tasks, actualTasks);
    }
Esempio n. 3
0
    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);
    }