Esempio n. 1
0
    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));
    }
Esempio n. 2
0
    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);
    }
Esempio n. 3
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. 4
0
    public void Cycle_ThreadExecutor_ShouldThrowWhenEmpty()
    {
        //Arange
        IScheduler executor = new ThreadExecutor();

        //Act
        //Assert
        Assert.Throws <InvalidOperationException>(() => executor.Cycle(1));
    }
Esempio n. 5
0
    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));
    }
Esempio n. 6
0
    public void Enumerator_ThreadExecutor_ShouldWorkCorrectly_AfterCycle()
    {
        //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>()
        {
            task2, task3, task4, 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(3);

        Assert.AreEqual(4, executor.Count);

        executor.ChangePriority(600, Priority.HIGH);

        //Assert
        List <Task> actual = executor.ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Esempio n. 7
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);
    }
Esempio n. 8
0
    public void Contains_ThreadExecutor_ShouldWorkCorrectly_AfterCycle()
    {
        //Arrange
        IScheduler executor = new ThreadExecutor();

        Task task1 = new Task(52, 12, Priority.EXTREME);
        Task task2 = new Task(13, 8, Priority.HIGH);

        //Act
        executor.Execute(task1);
        executor.Execute(task2);

        executor.Cycle(5);

        Assert.True(executor.Contains(task2));

        executor.Cycle(3);

        //Assert
        Assert.AreEqual(1, executor.Count);
        Assert.False(executor.Contains(task2));
    }
Esempio n. 9
0
    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);
    }