Пример #1
0
def test_overdue_tasks():
    schdlr = scheduler.Scheduler(4)

    task1 = task.Task('test-1', foo, (1, ), {
        'to_sleep': 1,
    })
    task2 = task.Task('test-2', foo, (1, ), {
        'to_sleep': 1,
    })
    task3 = task.Task('test-3', foo, (1, ), {
        'to_sleep': 1,
    })

    schdlr.start()
    schdlr.add_task(task1, priority=task.PRIORITY_MID, timeout=0.1)
    time.sleep(0.2)
    assert len(schdlr.overdue_tasks) == 1

    schdlr.add_task(task2, priority=task.PRIORITY_MID, timeout=0.1)
    time.sleep(0.2)
    assert len(schdlr.overdue_tasks) == 2

    schdlr.add_task(task3, priority=task.PRIORITY_MID, timeout=0.1)
    time.sleep(0.2)
    assert len(schdlr.overdue_tasks) == 3

    time.sleep(1)

    assert len(schdlr.overdue_tasks) == 0

    schdlr.stop()
Пример #2
0
def test_multiple_task_order_different_priority_2():
    schdlr = scheduler.Scheduler(1)

    task1 = task.Task('test-1', foo, (1, ), {
        'to_sleep': 0.3,
    })
    task2 = task.Task('test-2', foo, (1, ), {
        'to_sleep': 0.3,
    })
    task3 = task.Task('test-3', foo, (1, ), {
        'to_sleep': 0.3,
    })
    task4 = task.Task('test-4', foo, (1, ), {
        'to_sleep': 0.3,
    })

    schdlr.add_task(task1, priority=task.PRIORITY_MID)
    schdlr.add_task(task2, priority=task.PRIORITY_MID)
    schdlr.add_task(task3, priority=task.PRIORITY_MID)

    schdlr.start()
    time.sleep(0.1)
    schdlr.add_task(task4, priority=task.PRIORITY_HI)
    schdlr.stop()

    check_scheduler_state(schdlr, scheduler.STATE_STOPPED, 0, 0, 4, None)
    compare_task_list(
        schdlr.tasks_stat(short=False)["done"], [task1, task2, task4, task3])
Пример #3
0
def test_no_tasks_start_stop():
    schdlr = scheduler.Scheduler(1)

    check_scheduler_state(schdlr, scheduler.STATE_NOT_STARTED, 0, 0, 0, None)

    schdlr.start()
    check_scheduler_state(schdlr, scheduler.STATE_RUNNING, 0, 0, 0, None)

    schdlr.stop()
    check_scheduler_state(schdlr, scheduler.STATE_STOPPED, 0, 0, 0, None)
Пример #4
0
def test_one_worker_one_task_pass_added_before_start():
    schdlr = scheduler.Scheduler(1)
    task1 = task.Task('test-1', foo, (1, ), {
        'to_sleep': 0.3,
    })

    schdlr.add_task(task1)
    assert task1.done is False and task1.result is etc._undef_
    schdlr.start()
    time.sleep(0.1)
    check_scheduler_state(schdlr, scheduler.STATE_RUNNING, 0, 1, 0, None)
    schdlr.stop()
    assert task1.done is True and task1.result == 1
    check_scheduler_state(schdlr, scheduler.STATE_STOPPED, 0, 0, 1, None)
Пример #5
0
def test_complete_enqueued():
    schdlr = scheduler.Scheduler(4)

    task1 = task.Task('test-1', foo, (1, ), {
        'to_sleep': 1,
    })
    task2 = task.Task('test-2', foo, (1, ), {
        'to_sleep': 1,
    })
    task3 = task.Task('test-3', foo, (1, ), {
        'to_sleep': 1,
    })

    schdlr.start()
    schdlr.add_task(task1)
    schdlr.add_task(task2)
    schdlr.add_task(task3)

    schdlr.stop()
    check_scheduler_state(schdlr, scheduler.STATE_STOPPED, 0, 0, 3, None)
Пример #6
0
def test_multiple_task_order_same_priority():
    schdlr = scheduler.Scheduler(1)

    task1 = task.Task('test-1', foo, (1, ), {
        'to_sleep': 0.3,
    })
    task2 = task.Task('test-2', foo, (1, ), {
        'to_sleep': 0.3,
    })
    task3 = task.Task('test-3', foo, (1, ), {
        'to_sleep': 0.3,
    })

    for t in [task1, task2, task3]:
        schdlr.add_task(t)

    schdlr.start()
    time.sleep(0.5)
    schdlr.stop()

    check_scheduler_state(schdlr, scheduler.STATE_STOPPED, 0, 0, 3, None)
    assert schdlr.tasks_stat(short=False)["done"] == [task1, task2, task3]