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);
    }
Exemple #2
0
    public void Enumerator_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.HIGH);
        List <Task> expected = new List <Task>()
        {
            task1, task2, task3, task4, task5, task6, 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.ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
    public void Enumerator_ThreadExecutor_ShouldReturnEmptyCollection()
    {
        //Arange
        IScheduler executor = new ThreadExecutor();

        Task task1 = new Task(52, 5, Priority.HIGH);
        Task task2 = new Task(56, 12, Priority.HIGH);

        List <Task> expected = new List <Task>();

        //Act
        Assert.AreEqual(0, executor.Count);

        //Assert
        List <Task> actual = executor.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);
    }