def test_validate_secondary_period_returns_false_when_provided_sp_is_negative(
):
    planner = CyclicExecutivePlanner(
        [Task('T1', 30, 6),
         Task('T2', 20, 3),
         Task('T3', 120, 14)])
    assert not planner.validate_secondary_period(-5)
def test_validate_secondary_period_returns_false_when_not_full_frame_from_task_start_to_task_deadline_for_each_task(
):
    planner = CyclicExecutivePlanner(
        [Task('T1', 30, 6),
         Task('T2', 20, 3),
         Task('T3', 120, 14)])
    assert not planner.validate_secondary_period(15)
def test_can_add_task_returns_true_when_task_can_fit_in_secondary_period_and_was_not_executed_in_this_deadline_slice(
):
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = CyclicExecutivePlanner(tasks)
    planner.matrix = ExecutionMatrix(planner.hyperperiod, planner.hyperperiod,
                                     planner.secondary_period)
    assert planner.can_add_task(tasks[0], 0)
Esempio n. 4
0
    def test_task_CRUD(self):
        origin_task = Task()
        origin_task.name = 'origin task'
        origin_task.status = False

        # create
        db.session.add(origin_task)
        db.session.commit()

        # read
        tasks = Task.query.filter_by(name=origin_task.name)
        task = tasks.first()
        self.assertEqual(1, tasks.count())
        self.assertEqual(origin_task.status, task.status)

        # update
        task.status = True
        db.session.commit()
        tasks = Task.query.filter_by(name=origin_task.name)
        task = tasks.first()
        self.assertEqual(True, task.status)

        # delete
        db.session.delete(task)
        db.session.commit()

        tasks = Task.query.filter_by(name=task.name)
        self.assertEqual(0, tasks.count())
def test_calculate_remaining_sp_space_returns_remaining_secondary_period_space_when_timestamp_provided(
):
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = CyclicExecutivePlanner(tasks)
    assert planner.calculate_remaining_sp_space(0) == 20
    assert planner.calculate_remaining_sp_space(1) == 19
    assert planner.calculate_remaining_sp_space(19) == 1
    assert planner.calculate_remaining_sp_space(20) == 20
    assert planner.calculate_remaining_sp_space(39) == 1
def test_can_add_task_returns_false_when_task_not_fit_in_secondary_period():
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = CyclicExecutivePlanner(tasks)
    planner.matrix = ExecutionMatrix(planner.hyperperiod, planner.hyperperiod,
                                     planner.secondary_period)
    time_stamp = 15
    for x in range(time_stamp):
        planner.matrix.processors[0].add_time_unit()
    assert not planner.can_add_task(tasks[0], time_stamp)
def test_is_task_with_same_id_in_list_returns_true_when_task_with_same_id_is_in_list():
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = RateMonotonicPlanner(tasks)
    t_3_edited = deepcopy(tasks[2])
    t_3_edited.compute_time = 2
    t_list = [tasks[0], tasks[1], t_3_edited]
    assert planner.is_task_with_same_id_in_list(tasks[0], t_list)
    assert planner.is_task_with_same_id_in_list(tasks[1], t_list)
    assert planner.is_task_with_same_id_in_list(tasks[2], t_list)
Esempio n. 8
0
def test_repr_tasks():

    e_task = Task(
        name="A",
        deadline=1,
        compute_time=100
    )

    assert  e_task.__str__() == "A -> C:100 | D:1"
def test_can_add_task_returns_true_when_task_not_executed_in_current_deadline():
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = RateMonotonicPlanner(tasks)
    planner.matrix = ExecutionMatrix(planner.processors, planner.hyperperiod)
    planner.matrix.processors[0].add_time_unit()
    planner.matrix.processors[0].set_task(tasks[1])
    for x in range(tasks[1].deadline - 1):
        planner.matrix.processors[0].add_time_unit()
    assert planner.can_add_task(tasks[1], tasks[1].deadline)
Esempio n. 10
0
    def setUp(self):
        db.create_all()

        # example task
        t = Task()
        t.name = 'test added task'

        db.session.add(t)
        db.session.commit()

        self.task = Task.query.filter_by(name=t.name).first()
def test_can_add_task_returns_false_when_task_was_executed_in_current_deadline_in_any_processor():
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = RateMonotonicPlanner(tasks, 2)
    planner.matrix = ExecutionMatrix(planner.processors, planner.hyperperiod)
    planner.matrix.processors[0].add_time_unit()
    planner.matrix.processors[1].add_time_unit()
    planner.matrix.processors[0].set_task(tasks[1])
    for x in range(int(tasks[1].deadline / 2)):
        planner.matrix.processors[0].add_time_unit()
        planner.matrix.processors[1].add_time_unit()
    assert not planner.can_add_task(tasks[1], int(tasks[1].deadline / 2))
def test_can_add_task_returns_false_when_task_was_already_enqueued_in_this_deadline_slice(
):
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = CyclicExecutivePlanner(tasks)
    planner.matrix = ExecutionMatrix(planner.hyperperiod, planner.hyperperiod,
                                     planner.secondary_period)
    planner.matrix.processors[0].add_time_unit()
    planner.matrix.processors[0].set_task(tasks[0])
    for x in range(tasks[0].deadline - 1):
        planner.matrix.processors[0].add_time_unit()
    assert not planner.can_add_task(tasks[0], planner.secondary_period + 1)
Esempio n. 13
0
        def post(self):
            args = parser.parse_args()
            task = Task()
            task.name = args['name']

            try:
                db.session.add(task)
                db.session.commit()
            except exc.SQLAlchemyError:
                db.session.rollback()
                app.logger.error(self)

            return '', 201
Esempio n. 14
0
def exampleData():
    init_db()

    tasks = list()

    for idx in range(1, 100):
        task = Task()
        task.name = 'taskname{}'.format(idx)
        tasks.append(task)

    for task in tasks:
        db.session.add(task)
        db.session.commit()
Esempio n. 15
0
def test_get_task_last_start_time_returns_task_start_timestamp_when_scheduled_task_provided(
):
    processor = Processor()
    t1 = Task('T1', 2, 2)
    t2 = Task('T2', 2, 1)
    processor.time_units = [None, t1, t1, None, None]
    assert processor.get_task_last_start_time(t1) == 1
    processor.time_units = [None, t2, t1, t1, None]
    assert processor.get_task_last_start_time(t1) == 2
    processor.time_units = [None, None, t1, t1, t2]
    assert processor.get_task_last_start_time(t2) == 4
    processor.time_units = [t1, t1, t2, None, None]
    assert processor.get_task_last_start_time(t2) == 2
    processor.time_units = [t1, t1, None, t2, None]
    assert processor.get_task_last_start_time(t1) == 0
Esempio n. 16
0
def test_last_task_ended_returns_true_when_last_assigned_task_ended():
    processor = Processor()
    processor.add_time_unit()
    t = Task('T1', 2, 2)
    processor.set_task(t)
    processor.add_time_unit()
    assert processor.last_task_ended()
Esempio n. 17
0
def test_add_time_unit_inserts_none_when_last_task_ended():
    processor = Processor()
    processor.add_time_unit()
    t = Task('T1', 2, 1)
    processor.set_task(t)
    processor.add_time_unit()
    assert len(processor.time_units) == 2
    assert processor.time_units[1] is None
Esempio n. 18
0
def test_add_time_unit_inserts_previous_task_when_last_task_not_ended():
    processor = Processor()
    processor.add_time_unit()
    t = Task('T1', 2, 2)
    processor.set_task(t)
    processor.add_time_unit()
    assert len(processor.time_units) == 2
    assert processor.time_units[1] == t
Esempio n. 19
0
def test_attributes_tasks():

    e_task = Task(
        name="A",
        deadline=1,
        compute_time=100
    )

    assert e_task.name == "A"
    assert e_task.deadline == 1
    assert e_task.compute_time == 100
    assert e_task.task_id != ""
def schedule():
    body = request.json
    print(body)
    tasks = [
        Task(t['name'], t['deadline'], t['computeTime']) for t in body['tasks']
    ]
    try:
        scheduler = Scheduler(tasks, body['processors'], body['planner'])
        result = scheduler.schedule()
        return make_response(result.to_dict())
    except Exception as e:
        response = jsonify({'message': str(e)})
        response.status_code = ERROR_RESPONSE_CODE
        return response
def test_get_plan_returns_cyclic_executive_planified_matrix_when_tasks_are_valids(
):
    # Case 1
    planner = CyclicExecutivePlanner(
        [Task('T1', 5, 2),
         Task('T2', 10, 3),
         Task('T3', 5, 1)])
    matrix = planner.get_plan()
    expected = ['T1', 'T1', 'T3', 'T2', 'T2', 'T2', 'T1', 'T1', 'T3', '']
    compare_each_processor_value(expected, matrix.processors[0])
    # Case 2
    planner = CyclicExecutivePlanner(
        [Task('T1', 6, 2),
         Task('T2', 5, 1),
         Task('T3', 10, 2)])
    matrix = planner.get_plan()
    expected = [
        'T1', 'T1', 'T2', '', 'T3', 'T3', 'T1', 'T1', 'T2', '', 'T2', '', 'T1',
        'T1', 'T3', 'T3', 'T2', '', 'T1', 'T1', 'T2', '', 'T3', 'T3', 'T1',
        'T1', 'T2', '', '', ''
    ]
    compare_each_processor_value(expected, matrix.processors[0])
    # Case 3
    planner = CyclicExecutivePlanner(
        [Task('T1', 15, 3),
         Task('T2', 10, 2),
         Task('T3', 25, 5)])
    matrix = planner.get_plan()
    expected = [
        'T1', 'T1', 'T1', 'T2', 'T2', 'T3', 'T3', 'T3', 'T3', 'T3', 'T2', 'T2',
        '', '', '', 'T1', 'T1', 'T1', '', '', 'T2', 'T2', '', '', '', 'T3',
        'T3', 'T3', 'T3', 'T3', 'T1', 'T1', 'T1', 'T2', 'T2', '', '', '', '',
        '', 'T2', 'T2', '', '', '', 'T1', 'T1', 'T1', '', '', 'T2', 'T2', '',
        '', '', 'T3', 'T3', 'T3', 'T3', 'T3', 'T1', 'T1', 'T1', 'T2', 'T2', '',
        '', '', '', '', 'T2', 'T2', '', '', '', 'T1', 'T1', 'T1', '', '', 'T2',
        'T2', '', '', '', 'T3', 'T3', 'T3', 'T3', 'T3', 'T1', 'T1', 'T1', 'T2',
        'T2', '', '', '', '', '', 'T2', 'T2', '', '', '', 'T1', 'T1', 'T1', '',
        '', 'T2', 'T2', '', '', '', 'T3', 'T3', 'T3', 'T3', 'T3', 'T1', 'T1',
        'T1', 'T2', 'T2', 'T3', 'T3', 'T3', 'T3', 'T3', 'T2', 'T2', '', '', '',
        'T1', 'T1', 'T1', '', '', 'T2', 'T2', '', '', '', '', '', '', '', ''
    ]
    compare_each_processor_value(expected, matrix.processors[0])
def test_calculates_secondary_period_when_initialized():
    planner = CyclicExecutivePlanner([Task('T1', 10, 5)])
    assert planner.secondary_period == 5
    planner = CyclicExecutivePlanner(
        [Task('T1', 20, 3),
         Task('T2', 5, 2),
         Task('T3', 10, 2)])
    assert planner.secondary_period == 3
    planner = CyclicExecutivePlanner(
        [Task('T1', 30, 6),
         Task('T2', 20, 3),
         Task('T3', 120, 14)])
    assert planner.secondary_period == 20
Esempio n. 23
0
def populate_db():
    """Populate the database with report."""

    db.session.add(Task(description='task1'))
    db.session.add(Task(description='task2'))
    db.session.add(Task(description='task3'))
    db.session.add(Task(description='task4'))
    db.session.add(Task(description='task5'))
    db.session.add(Task(description='task6'))

    
    db.session.commit()
def test_get_plan_raises_exception_when_task_cannot_be_planned_correctly():
    # Case 1
    planner = CyclicExecutivePlanner(
        [Task('T1', 5, 2),
         Task('T2', 10, 5),
         Task('T3', 5, 1)])
    with pytest.raises(Exception):
        planner.get_plan()
    # Case 2
    planner = CyclicExecutivePlanner(
        [Task('T1', 6, 2),
         Task('T2', 4, 1),
         Task('T3', 10, 3)])
    with pytest.raises(Exception):
        planner.get_plan()
def test_get_plan_returns_cyclic_executive_planified_matrix_when_tasks_are_valids():
    # Case 1
    planner = RateMonotonicPlanner([Task('T1', 5, 2), Task('T2', 10, 3), Task('T3', 5, 1)], 1)
    matrix = planner.get_plan()
    expected = [['T1',  'T1',  'T3',  'T2', 'T2',  'T1',  'T1',  'T3',  'T2', '']]
    compare_each_processor_value(expected, matrix.processors)
    # Case 2
    planner = RateMonotonicPlanner([Task('T1', 5, 2), Task('T2', 10, 3), Task('T3', 5, 1)], 2)
    matrix = planner.get_plan()
    expected = [['T1',  'T1',  '',  '', '',  'T1',  'T1',  '',  '', ''],
                ['T3',  'T2',  'T2',  'T2', '',  'T3',  '',  '',  '', '']]
    compare_each_processor_value(expected, matrix.processors)
    # Case 3
    planner = RateMonotonicPlanner([Task('T1', 20, 3), Task('T2', 5, 2), Task('T3', 10, 2)], 1)
    matrix = planner.get_plan()
    expected = [['T2', 'T2', 'T3', 'T3', 'T1', 'T2', 'T2', 'T1', 'T1', '', 'T2', 'T2', 'T3', 'T3', '', 'T2', 'T2', '', '', '']]
    compare_each_processor_value(expected, matrix.processors)
    # Case 4
    planner = RateMonotonicPlanner([Task('T1', 20, 3), Task('T2', 5, 2), Task('T3', 10, 2)], 2)
    matrix = planner.get_plan()
    expected = [['T2', 'T2', 'T1', 'T1', 'T1', 'T2', 'T2', '', '', '', 'T2', 'T2', '', '', '', 'T2', 'T2', '', '', ''],
                ['T3', 'T3', '', '', '', '', '', '', '', '', 'T3', 'T3', '', '', '', '', '', '', '', '']]
    compare_each_processor_value(expected, matrix.processors)
Esempio n. 26
0
def add_task(description):
    task = Task(description=description)
    db.session.add(task)
    db.session.commit()
    return task
Esempio n. 27
0
from config import db
from src.models.task import Task

if __name__ == '__main__':
    db.create_all()
    db.session.add(Task(description="wash cars", state=0, user_id=1))
    db.session.add(Task(description="wash clothes", state=1, user_id=1))
    db.session.add(Task(description="run", state=1, user_id=1))
    db.session.add(Task(description="wash cars", state=0, user_id=2))
    db.session.add(Task(description="run", state=1, user_id=2))
    db.session.commit()
def test_sort_tasks_return_task_sorted_by_min_deadline_first_when_tasks_provided():
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = RateMonotonicPlanner(tasks)
    assert planner.sort_tasks(tasks)[0] == tasks[1]
    assert planner.sort_tasks(tasks)[1] == tasks[0]
    assert planner.sort_tasks(tasks)[2] == tasks[2]
def test_is_task_with_same_id_in_list_returns_false_when_task_with_same_id_is_not_in_list():
    tasks = [Task('T1', 30, 6), Task('T2', 20, 3), Task('T3', 120, 14)]
    planner = RateMonotonicPlanner(tasks)
    t_list = [tasks[0], tasks[1]]
    assert not planner.is_task_with_same_id_in_list(tasks[2], t_list)
Esempio n. 30
0
def test_get_task_last_start_time_returns_minus_one_when_task_was_never_scheduled(
):
    processor = Processor()
    processor.add_time_unit()
    t = Task('T1', 1, 1)
    assert processor.get_task_last_start_time(t) == -1