def test_satisfy_xclock_unsatisfied_xclock():
    """Test satisfy_xclock for an unsatisfied clock trigger."""
    xtrigger_mgr = XtriggerManager(suite="sample_suite", user="******")
    # the clock xtrigger
    xtrig = SubFuncContext(label="wall_clock",
                           func_name="wall_clock",
                           func_args=[],
                           func_kwargs={})
    xtrig.out = "[\"True\", \"1\"]"
    xtrigger_mgr.add_clock("wall_clock", xtrig)
    # create a task
    tdef = TaskDef(name="foo",
                   rtcfg=None,
                   run_mode="live",
                   start_point=1,
                   spawn_ahead=False)
    tdef.xclock_label = "wall_clock"
    # cycle point for task proxy
    init()
    start_point = ISO8601Point('20000101T0000+05')
    # create task proxy
    itask = TaskProxy(tdef=tdef, start_point=start_point)
    itask.state.xclock = "wall_clock", False  # satisfied?
    # we are defining in the state of the TaskProxy. that its xclock trigger
    # has **not** been satisfied.
    assert not xtrigger_mgr.sat_xclock
    xtrigger_mgr.satisfy_xclock(itask)
    # as it was satisfied by the satisfy_clock function, we must have its
    # signature in the dict. NB the signature is not the same as
    # get_signature(), as it is actually the signature for that moment
    # when it was satisfied.
    assert xtrigger_mgr.sat_xclock
def test_satisfy_xclock_satisfied_xclock():
    """Test satisfy_xclock for a satisfied clock trigger."""
    xtrigger_mgr = XtriggerManager(suite="sample_suite", user="******")
    # the clock xtrigger
    xtrig = SubFuncContext(label="wall_clock",
                           func_name="wall_clock",
                           func_args=[],
                           func_kwargs={})
    xtrig.out = "[\"True\", \"1\"]"
    xtrigger_mgr.add_clock("wall_clock", xtrig)
    # create a task
    tdef = TaskDef(name="foo",
                   rtcfg=None,
                   run_mode="live",
                   start_point=1,
                   spawn_ahead=False)
    tdef.xclock_label = "wall_clock"
    # cycle point for task proxy
    init()
    start_point = ISO8601Point('20000101T0000+05')
    # create task proxy
    itask = TaskProxy(tdef=tdef, start_point=start_point)
    itask.state.xclock = "wall_clock", True  # satisfied?
    # we are defining in the state of the TaskProxy. that its xclock trigger
    # has been satisfied, without actually adding it to the right dict.
    assert not xtrigger_mgr.sat_xclock
    xtrigger_mgr.satisfy_xclock(itask)
    # as it was already satisfied, the function should return immediately,
    # without touching sat_xclock, therefore, it must remain empty
    assert not xtrigger_mgr.sat_xclock
def test_housekeeping_with_xclock_satisfied():
    """The housekeeping method makes sure only satisfied xclock function
    are kept."""
    xtrigger_mgr = XtriggerManager(suite="sample_suite", user="******")
    # the clock xtrigger
    xtrig = SubFuncContext(label="wall_clock",
                           func_name="wall_clock",
                           func_args=[],
                           func_kwargs={})
    xtrig.out = "[\"True\", \"1\"]"
    xtrigger_mgr.add_clock("wall_clock", xtrig)
    # create a task
    tdef = TaskDef(name="foo",
                   rtcfg=None,
                   run_mode="live",
                   start_point=1,
                   spawn_ahead=False)
    tdef.xclock_label = "wall_clock"
    # cycle point for task proxy
    # TODO: we need to call init, before we can use ISO8601 points in Cylc,
    #       why?
    init()
    start_point = ISO8601Point('20000101T0000+05')
    # create task proxy
    itask = TaskProxy(tdef=tdef, start_point=start_point)
    itask.state.xclock = "wall_clock", False  # satisfied?
    # satisfy xclock
    xtrigger_mgr.satisfy_xclock(itask)
    # tally
    xtrigger_mgr.collate([itask])
    assert xtrigger_mgr.sat_xclock
    xtrigger_mgr.housekeep()
    # here we still have the same number as before
    assert xtrigger_mgr.sat_xclock
def test_check_xtriggers():
    """Test check_xtriggers call.

    check_xtriggers does pretty much the same as collate. The
    difference is that besides tallying on all the xtriggers and
    clock xtriggers available, it then proceeds to trying to
    satisfy them."""
    xtrigger_mgr = XtriggerManager(suite="sample_suite",
                                   user="******",
                                   proc_pool=MockedProcPool())

    # add a xtrigger
    # that will cause all_xtrig to be populated, but not all_xclock
    get_name = SubFuncContext(label="get_name",
                              func_name="get_name",
                              func_args=[],
                              func_kwargs={})
    xtrigger_mgr.add_trig("get_name", get_name)
    get_name.out = "[\"True\", {\"name\": \"Yossarian\"}]"
    tdef1 = TaskDef(name="foo",
                    rtcfg=None,
                    run_mode="live",
                    start_point=1,
                    spawn_ahead=False)
    tdef1.xtrig_labels.add("get_name")
    start_point = ISO8601Point('20000101T0000+05')
    itask1 = TaskProxy(tdef=tdef1, start_point=start_point)
    itask1.state.xtriggers["get_name"] = False  # satisfied?

    # add a clock xtrigger
    # that will cause both all_xclock to be populated but not all_xtrig
    wall_clock = SubFuncContext(label="wall_clock",
                                func_name="wall_clock",
                                func_args=[],
                                func_kwargs={})
    wall_clock.out = "[\"True\", \"1\"]"
    xtrigger_mgr.add_clock("wall_clock", wall_clock)
    # create a task
    tdef2 = TaskDef(name="foo",
                    rtcfg=None,
                    run_mode="live",
                    start_point=1,
                    spawn_ahead=False)
    tdef2.xclock_label = "wall_clock"
    init()
    start_point = ISO8601Point('20000101T0000+05')
    # create task proxy
    itask2 = TaskProxy(tdef=tdef2, start_point=start_point)
    itask2.state.xclock = "wall_clock", False  # satisfied?

    xtrigger_mgr.check_xtriggers([itask1, itask2])
    assert xtrigger_mgr.sat_xclock
    assert xtrigger_mgr.all_xclock
    # won't be satisfied, as it is async, we are are not calling callback
    assert not xtrigger_mgr.sat_xtrig
    assert xtrigger_mgr.all_xtrig
def test_add_clock_xtrigger():
    """Test for adding a clock xtrigger. Clock xtriggers go to a different
    dict than normal xtriggers. This is useful as the execution varies
    for clock/non-clock xtriggers (e.g. sync vs. async)."""
    xtrigger_mgr = XtriggerManager(suite="sample_suite", user="******")
    xtrig = SubFuncContext(label="wall_clock",
                           func_name="wall_clock",
                           func_args=[],
                           func_kwargs={})
    xtrigger_mgr.add_clock("xtrig", xtrig)
    assert xtrig == xtrigger_mgr.clockx_map["xtrig"]
def test_collate():
    """Test that collate properly tallies the totals of current xtriggers."""
    xtrigger_mgr = XtriggerManager(suite="sample_suite", user="******")
    xtrigger_mgr.collate(itasks=[])
    assert not xtrigger_mgr.all_xclock
    assert not xtrigger_mgr.all_xtrig

    # add a xtrigger
    # that will cause all_xtrig to be populated, but not all_xclock
    get_name = SubFuncContext(label="get_name",
                              func_name="get_name",
                              func_args=[],
                              func_kwargs={})
    xtrigger_mgr.add_trig("get_name", get_name)
    get_name.out = "[\"True\", {\"name\": \"Yossarian\"}]"
    tdef = TaskDef(name="foo",
                   rtcfg=None,
                   run_mode="live",
                   start_point=1,
                   spawn_ahead=False)
    tdef.xtrig_labels.add("get_name")
    start_point = ISO8601Point('20000101T0000+05')
    itask = TaskProxy(tdef=tdef, start_point=start_point)
    itask.state.xtriggers["get_name"] = get_name

    xtrigger_mgr.collate([itask])
    assert not xtrigger_mgr.all_xclock
    assert xtrigger_mgr.all_xtrig

    # add a clock xtrigger
    # that will cause both all_xclock to be populated but not all_xtrig
    wall_clock = SubFuncContext(label="wall_clock",
                                func_name="wall_clock",
                                func_args=[],
                                func_kwargs={})
    wall_clock.out = "[\"True\", \"1\"]"
    xtrigger_mgr.add_clock("wall_clock", wall_clock)
    # create a task
    tdef = TaskDef(name="foo",
                   rtcfg=None,
                   run_mode="live",
                   start_point=1,
                   spawn_ahead=False)
    tdef.xclock_label = "wall_clock"
    init()
    start_point = ISO8601Point('20000101T0000+05')
    # create task proxy
    itask = TaskProxy(tdef=tdef, start_point=start_point)
    itask.state.xclock = "wall_clock", True

    xtrigger_mgr.collate([itask])
    assert xtrigger_mgr.all_xclock
    assert not xtrigger_mgr.all_xtrig