public void GetByConsumptionRange_ThreadExecutor_ShouldWorkCorrectly_Exclusive()
    {
        //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.EXTREME);
        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>()
        {
            task7, task8
        };

        //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.GetByConsumptionRange(5, 12, false).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
    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);
    }
Exemple #3
0
    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));
    }