def test_custom_reschedule_condition():
    """Force reschedule task X times to test logic."""
    pool_name = ProcessPool.__name__
    plan = Testplan(name="ProcPlan", parse_cmdline=False)
    uid = "custom_task_uid"
    max_reschedules = 2

    def custom_reschedule(pool, task_result):
        if pool.task_assign_cnt[uid] == max_reschedules:
            return False
        return True

    pool_size = 4
    pool = ProcessPool(
        name=pool_name,
        size=pool_size,
        worker_heartbeat=2,
        heartbeats_miss_limit=2,
        max_active_loop_sleep=1,
        restart_count=0,
    )
    pool.set_reschedule_check(custom_reschedule)
    pool_uid = plan.add_resource(pool)

    dirname = os.path.dirname(os.path.abspath(__file__))

    plan.schedule(
        target="get_mtest",
        module="func_pool_base_tasks",
        path=dirname,
        kwargs=dict(name="0"),
        resource=pool_name,
        uid=uid,
    )

    with log_propagation_disabled(TESTPLAN_LOGGER):
        res = plan.run()

    assert (len([
        worker for worker in plan.resources[pool_uid]._workers
        if worker._aborted is True
    ]) == 0)

    assert res.success is True
    assert pool.task_assign_cnt[uid] == max_reschedules
    assert plan.report.status == Status.PASSED
Example #2
0
def test_custom_reschedule_condition():
    """Force reschedule task X times to test logic."""
    pool_name = ProcessPool.__name__
    plan = Testplan(
        name='ProcPlan',
        parse_cmdline=False,
    )
    uid = 'custom_task_uid'
    max_reschedules = 2

    def custom_reschedule(pool, task_result):
        if pool.task_assign_cnt[uid] == max_reschedules:
            return False
        return True

    pool_size = 4
    pool = ProcessPool(name=pool_name,
                       size=pool_size,
                       worker_heartbeat=2,
                       heartbeats_miss_limit=2)
    pool.set_reschedule_check(custom_reschedule)
    pool_uid = plan.add_resource(pool)

    dirname = os.path.dirname(os.path.abspath(__file__))

    plan.schedule(target='get_mtest',
                  module='func_pool_base_tasks',
                  path=dirname,
                  kwargs=dict(name='0'),
                  resource=pool_name,
                  uid=uid)

    with log_propagation_disabled(TESTPLAN_LOGGER):
        res = plan.run()

    # Check that the worker killed by test was aborted
    assert len([
        worker for worker in plan.resources[pool_uid]._workers
        if worker._aborted is True
    ]) == 0

    assert res.success is True
    assert pool.task_assign_cnt[uid] == max_reschedules
    assert plan.report.status == Status.PASSED