def test_check_xtriggers(xtrigger_mgr_procpool): """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.""" # 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_procpool.add_trig("get_name", get_name, 'fdir') get_name.out = "[\"True\", {\"name\": \"Yossarian\"}]" tdef1 = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, spawn_ahead=False ) init() sequence = ISO8601Sequence('P1D', '2019') tdef1.xtrig_labels[sequence] = ["get_name"] start_point = ISO8601Point('2019') 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_procpool.add_trig("wall_clock", wall_clock, "fdir") # create a task tdef2 = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, spawn_ahead=False ) tdef2.xtrig_labels[sequence] = ["wall_clock"] init() start_point = ISO8601Point('20000101T0000+05') # create task proxy itask2 = TaskProxy(tdef=tdef2, start_point=start_point) xtrigger_mgr_procpool.check_xtriggers([itask1, itask2]) # won't be satisfied, as it is async, we are are not calling callback assert not xtrigger_mgr_procpool.sat_xtrig assert xtrigger_mgr_procpool.all_xtrig
def test_collate(xtrigger_mgr): """Test that collate properly tallies the totals of current xtriggers.""" xtrigger_mgr.collate(itasks=[]) assert not xtrigger_mgr.all_xtrig # add a xtrigger # that will cause all_xtrig to be populated get_name = SubFuncContext( label="get_name", func_name="get_name", func_args=[], func_kwargs={} ) xtrigger_mgr.add_trig("get_name", get_name, 'fdir') get_name.out = "[\"True\", {\"name\": \"Yossarian\"}]" tdef = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, spawn_ahead=False ) init() sequence = ISO8601Sequence('P1D', '20190101T00Z') tdef.xtrig_labels[sequence] = ["get_name"] start_point = ISO8601Point('2019') itask = TaskProxy(tdef=tdef, start_point=start_point) itask.state.xtriggers["get_name"] = get_name xtrigger_mgr.collate([itask]) 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_trig("wall_clock", wall_clock, "fdir") # create a task tdef = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, spawn_ahead=False ) tdef.xtrig_labels[sequence] = ["wall_clock"] start_point = ISO8601Point('20000101T0000+05') # create task proxy itask = TaskProxy(tdef=tdef, start_point=start_point) xtrigger_mgr.collate([itask]) assert not xtrigger_mgr.all_xtrig
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_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
def test_check_xtriggers(xtrigger_mgr): """Test process_xtriggers call.""" xtrigger_mgr.validate_xtrigger = lambda *a, **k: True # Ignore validation # add an xtrigger get_name = SubFuncContext( label="get_name", func_name="get_name", func_args=[], func_kwargs={} ) xtrigger_mgr.add_trig("get_name", get_name, 'fdir') get_name.out = "[\"True\", {\"name\": \"Yossarian\"}]" tdef1 = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, initial_point=1 ) init() sequence = ISO8601Sequence('P1D', '2019') tdef1.xtrig_labels[sequence] = ["get_name"] start_point = ISO8601Point('2019') itask1 = TaskProxy(tdef1, start_point) itask1.state.xtriggers["get_name"] = False # satisfied? # add a clock xtrigger wall_clock = SubFuncContext( label="wall_clock", func_name="wall_clock", func_args=[], func_kwargs={} ) wall_clock.out = "[\"True\", \"1\"]" xtrigger_mgr.add_trig("wall_clock", wall_clock, "fdir") # create a task tdef2 = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, initial_point=1 ) tdef2.xtrig_labels[sequence] = ["wall_clock"] init() start_point = ISO8601Point('20000101T0000+05') # create task proxy TaskProxy(tdef2, start_point) xtrigger_mgr.check_xtriggers(itask1, lambda foo: None) # won't be satisfied, as it is async, we are are not calling callback assert not xtrigger_mgr.sat_xtrig
def test_housekeeping_with_xtrigger_satisfied(xtrigger_mgr): """The housekeeping method makes sure only satisfied xtrigger function are kept.""" xtrigger_mgr.validate_xtrigger = lambda *a, **k: True # Ignore validation xtrig = SubFuncContext( label="get_name", func_name="get_name", func_args=[], func_kwargs={} ) xtrigger_mgr.add_trig("get_name", xtrig, 'fdir') xtrig.out = "[\"True\", {\"name\": \"Yossarian\"}]" tdef = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, initial_point=1 ) init() sequence = ISO8601Sequence('P1D', '2019') tdef.xtrig_labels[sequence] = ["get_name"] start_point = ISO8601Point('2019') itask = TaskProxy(tdef, start_point) # pretend the function has been activated xtrigger_mgr.active.append(xtrig.get_signature()) xtrigger_mgr.callback(xtrig) assert xtrigger_mgr.sat_xtrig xtrigger_mgr.housekeep([itask]) # here we still have the same number as before assert xtrigger_mgr.sat_xtrig
def test_housekeeping_with_xtrigger_satisfied(): """The housekeeping method makes sure only satisfied xtrigger function are kept.""" xtrigger_mgr = XtriggerManager(suite="sample_suite", user="******") xtrig = SubFuncContext(label="get_name", func_name="get_name", func_args=[], func_kwargs={}) xtrigger_mgr.add_trig("get_name", xtrig) xtrig.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) xtrigger_mgr.collate([itask]) # pretend the function has been activated xtrigger_mgr.active.append(xtrig.get_signature()) xtrigger_mgr.callback(xtrig) assert xtrigger_mgr.sat_xtrig xtrigger_mgr.housekeep() # here we still have the same number as before assert xtrigger_mgr.sat_xtrig # however, we have no xclock trigger satisfied 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_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_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__call_xtriggers_async(xtrigger_mgr): """Test _call_xtriggers_async""" xtrigger_mgr.validate_xtrigger = lambda *a, **k: True # Ignore validation # the echo1 xtrig (not satisfied) echo1_xtrig = SubFuncContext(label="echo1", func_name="echo1", func_args=[], func_kwargs={}) echo1_xtrig.out = "[\"True\", {\"name\": \"herminia\"}]" xtrigger_mgr.add_trig("echo1", echo1_xtrig, "fdir") # the echo2 xtrig (satisfied through callback later) echo2_xtrig = SubFuncContext(label="echo2", func_name="echo2", func_args=[], func_kwargs={}) echo2_xtrig.out = "[\"True\", {\"name\": \"herminia\"}]" xtrigger_mgr.add_trig("echo2", echo2_xtrig, "fdir") # create a task tdef = TaskDef(name="foo", rtcfg=None, run_mode="live", start_point=1, initial_point=1) init() sequence = ISO8601Sequence('P1D', '2000') tdef.xtrig_labels[sequence] = ["echo1", "echo2"] # cycle point for task proxy init() start_point = ISO8601Point('2019') # create task proxy itask = TaskProxy(tdef, start_point, FlowLabelMgr().get_new_label()) # we start with no satisfied xtriggers, and nothing active assert len(xtrigger_mgr.sat_xtrig) == 0 assert len(xtrigger_mgr.active) == 0 # after calling the first time, we get two active xtrigger_mgr.call_xtriggers_async(itask) assert len(xtrigger_mgr.sat_xtrig) == 0 assert len(xtrigger_mgr.active) == 2 # calling again does not change anything xtrigger_mgr.call_xtriggers_async(itask) assert len(xtrigger_mgr.sat_xtrig) == 0 assert len(xtrigger_mgr.active) == 2 # now we call callback manually as the proc_pool we passed is a mock # then both should be satisfied xtrigger_mgr.callback(echo1_xtrig) xtrigger_mgr.callback(echo2_xtrig) # so both were satisfied, and nothing is active assert len(xtrigger_mgr.sat_xtrig) == 2 assert len(xtrigger_mgr.active) == 0 # calling satisfy_xtriggers again still does not change anything xtrigger_mgr.call_xtriggers_async(itask) assert len(xtrigger_mgr.sat_xtrig) == 2 assert len(xtrigger_mgr.active) == 0
def test_exclusions_sequences_points(self): """Test ISO8601Sequence methods for sequences with exclusions""" init(time_zone='Z') # Run every hour from 01:00 excluding every 3rd hour sequence = ISO8601Sequence('T01/PT1H!PT3H', '20000101T01Z') point_0 = ISO8601Point('20000101T00Z') point_1 = ISO8601Point('20000101T01Z') point_2 = ISO8601Point('20000101T02Z') point_3 = ISO8601Point('20000101T03Z') point_4 = ISO8601Point('20000101T04Z') self.assertFalse(point_0 in sequence.exclusions) self.assertTrue(point_1 in sequence.exclusions) self.assertTrue(sequence.is_on_sequence(point_2)) self.assertTrue(sequence.is_on_sequence(point_3)) self.assertFalse(sequence.is_on_sequence(point_4)) self.assertTrue(point_4 in sequence.exclusions)
def test_multiple_exclusions_extensive(self): """Test ISO8601Sequence methods for sequences with multiple exclusions """ init(time_zone='+05') sequence = ISO8601Sequence('PT1H!(20000101T02,20000101T03)', '20000101T00', '20000101T06') point_0 = ISO8601Point('20000101T0000+05') point_1 = ISO8601Point('20000101T0100+05') point_2 = ISO8601Point('20000101T0200+05') # First excluded point point_3 = ISO8601Point('20000101T0300+05') # Second excluded point point_4 = ISO8601Point('20000101T0400+05') # Check the excluded points are not on the sequence self.assertFalse(sequence.is_on_sequence(point_2)) self.assertFalse(sequence.is_on_sequence(point_3)) self.assertFalse(sequence.is_valid(point_2)) # Should be excluded self.assertFalse(sequence.is_valid(point_3)) # Should be excluded # Check that we can correctly retrieve previous points self.assertEqual(sequence.get_prev_point(point_2), point_1) # Should skip two excluded points self.assertEqual(sequence.get_prev_point(point_4), point_1) self.assertEqual(sequence.get_prev_point(point_2), point_1) self.assertEqual(sequence.get_nearest_prev_point(point_4), point_1) self.assertEqual(sequence.get_next_point(point_1), point_4) self.assertEqual(sequence.get_next_point(point_3), point_4) sequence = ISO8601Sequence('PT1H!20000101T00+05', '20000101T00') # Check that the first point is after 00. self.assertEqual(sequence.get_first_point(point_0), point_1) self.assertEqual(sequence.get_start_point(), point_1) # Check a longer list of exclusions # Also note you can change the format of the exclusion list # (removing the parentheses) sequence = ISO8601Sequence('PT1H!(20000101T02+05, 20000101T03+05,' '20000101T04+05)', '20000101T00', '20000101T06') self.assertEqual(sequence.get_prev_point(point_3), point_1) self.assertEqual(sequence.get_prev_point(point_4), point_1)
def test_simple(self): """Run some simple tests for date-time cycling.""" init(time_zone='Z') p_start = ISO8601Point('20100808T00') p_stop = ISO8601Point('20100808T02') i = ISO8601Interval('PT6H') self.assertEqual(p_start - i, ISO8601Point('20100807T18')) self.assertEqual(p_stop + i, ISO8601Point('20100808T08')) sequence = ISO8601Sequence( 'PT10M', str(p_start), str(p_stop), ) sequence.set_offset(-ISO8601Interval('PT10M')) point = sequence.get_next_point(ISO8601Point('20100808T0000')) self.assertEqual(point, ISO8601Point('20100808T0010')) output = [] # Test point generation forwards. while point and point < p_stop: output.append(point) self.assertTrue(sequence.is_on_sequence(point)) point = sequence.get_next_point(point) self.assertEqual([str(out) for out in output], [ '20100808T0010Z', '20100808T0020Z', '20100808T0030Z', '20100808T0040Z', '20100808T0050Z', '20100808T0100Z', '20100808T0110Z', '20100808T0120Z', '20100808T0130Z', '20100808T0140Z', '20100808T0150Z' ]) self.assertEqual(point, ISO8601Point('20100808T0200')) # Test point generation backwards. output = [] while point and point >= p_start: output.append(point) self.assertTrue(sequence.is_on_sequence(point)) point = sequence.get_prev_point(point) self.assertEqual([str(out) for out in output], [ '20100808T0200Z', '20100808T0150Z', '20100808T0140Z', '20100808T0130Z', '20100808T0120Z', '20100808T0110Z', '20100808T0100Z', '20100808T0050Z', '20100808T0040Z', '20100808T0030Z', '20100808T0020Z', '20100808T0010Z', '20100808T0000Z' ]) self.assertFalse(sequence.is_on_sequence( ISO8601Point('20100809T0005')))
def test_exclusions_extensive(self): """Test ISO8601Sequence methods for sequences with exclusions""" init(time_zone='+05') sequence = ISO8601Sequence('PT1H!20000101T02+05', '20000101T00', '20000101T05') point_0 = ISO8601Point('20000101T0000+05') point_1 = ISO8601Point('20000101T0100+05') point_2 = ISO8601Point('20000101T0200+05') # The excluded point. point_3 = ISO8601Point('20000101T0300+05') self.assertFalse(sequence.is_on_sequence(point_2)) self.assertFalse(sequence.is_valid(point_2)) self.assertEqual(sequence.get_prev_point(point_2), point_1) self.assertEqual(sequence.get_prev_point(point_3), point_1) self.assertEqual(sequence.get_prev_point(point_2), point_1) self.assertEqual(sequence.get_nearest_prev_point(point_3), point_1) self.assertEqual(sequence.get_next_point(point_1), point_3) self.assertEqual(sequence.get_next_point(point_2), point_3) sequence = ISO8601Sequence('PT1H!20000101T00+05', '20000101T00+05') self.assertEqual(sequence.get_first_point(point_0), point_1) self.assertEqual(sequence.get_start_point(), point_1)
def test_reset_state(self): """Test instantiation and simple resets.""" point = ISO8601Point('2020') taskdef = TaskDef('who-cares', {}, 'live', point, False) taskstate = TaskState(taskdef, point, TASK_STATUS_WAITING, None) self.assertIsNone( taskstate.reset_state(TASK_STATUS_WAITING), 'same status returns None', ) self.assertEqual( taskstate.reset_state(TASK_STATUS_SUCCEEDED), (TASK_STATUS_WAITING, None), 'different status returns previous (status, hold_swap)', ) self.assertEqual( (taskstate.status, taskstate.hold_swap), (TASK_STATUS_SUCCEEDED, None), 'reset status OK', )
def test_reset_state_respect_hold_swap(self): point = ISO8601Point('2020') taskdef = TaskDef('who-cares', {}, 'live', point, False) taskstate = TaskState(taskdef, point, TASK_STATUS_HELD, TASK_STATUS_RETRYING) self.assertIsNone( taskstate.reset_state(TASK_STATUS_RETRYING, respect_hold_swap=True), 'same status returns None', ) self.assertEqual( taskstate.reset_state(TASK_STATUS_SUCCEEDED, respect_hold_swap=True), (TASK_STATUS_HELD, TASK_STATUS_RETRYING), 'different status returns previous (status, hold_swap)', ) self.assertEqual( (taskstate.status, taskstate.hold_swap), (TASK_STATUS_SUCCEEDED, None), 'reset status OK', )
def test_satisfy_xtrigger(xtrigger_mgr_procpool_broadcast): """Test satisfy_xtriggers""" # the echo1 xtrig (not satisfied) echo1_xtrig = SubFuncContext( label="echo1", func_name="echo1", func_args=[], func_kwargs={} ) echo1_xtrig.out = "[\"True\", {\"name\": \"herminia\"}]" xtrigger_mgr_procpool_broadcast.add_trig("echo1", echo1_xtrig, "fdir") # the echo2 xtrig (satisfied through callback later) echo2_xtrig = SubFuncContext( label="echo2", func_name="echo2", func_args=[], func_kwargs={} ) echo2_xtrig.out = "[\"True\", {\"name\": \"herminia\"}]" xtrigger_mgr_procpool_broadcast.add_trig("echo2", echo2_xtrig, "fdir") # create a task tdef = TaskDef( name="foo", rtcfg=None, run_mode="live", start_point=1, spawn_ahead=False ) init() sequence = ISO8601Sequence('P1D', '2000') tdef.xtrig_labels[sequence] = ["echo1", "echo2"] # cycle point for task proxy init() start_point = ISO8601Point('2019') # create task proxy itask = TaskProxy(tdef=tdef, start_point=start_point) # we start with no satisfied xtriggers, and nothing active assert len(xtrigger_mgr_procpool_broadcast.sat_xtrig) == 0 assert len(xtrigger_mgr_procpool_broadcast.active) == 0 # after calling satisfy_xtriggers the first time, we get two active xtrigger_mgr_procpool_broadcast.satisfy_xtriggers(itask) assert len(xtrigger_mgr_procpool_broadcast.sat_xtrig) == 0 assert len(xtrigger_mgr_procpool_broadcast.active) == 2 # calling satisfy_xtriggers again does not change anything xtrigger_mgr_procpool_broadcast.satisfy_xtriggers(itask) assert len(xtrigger_mgr_procpool_broadcast.sat_xtrig) == 0 assert len(xtrigger_mgr_procpool_broadcast.active) == 2 # now we call callback manually as the proc_pool we passed is a mock # then both should be satisfied xtrigger_mgr_procpool_broadcast.callback(echo1_xtrig) xtrigger_mgr_procpool_broadcast.callback(echo2_xtrig) # so both were satisfied, and nothing is active assert len(xtrigger_mgr_procpool_broadcast.sat_xtrig) == 2 assert len(xtrigger_mgr_procpool_broadcast.active) == 0 # calling satisfy_xtriggers again still does not change anything xtrigger_mgr_procpool_broadcast.satisfy_xtriggers(itask) assert len(xtrigger_mgr_procpool_broadcast.sat_xtrig) == 2 assert len(xtrigger_mgr_procpool_broadcast.active) == 0
import pytest from pytest import param from typing import Callable, Optional from unittest.mock import Mock from cylc.flow.cycling import PointBase from cylc.flow.cycling.integer import IntegerPoint from cylc.flow.cycling.iso8601 import ISO8601Point from cylc.flow.task_proxy import TaskProxy @pytest.mark.parametrize('itask_point, point_str, expected', [ param(IntegerPoint(5), '5', True, id="Integer, basic"), param(IntegerPoint(5), '*', True, id="Integer, glob"), param(IntegerPoint(5), None, True, id="None same as glob(*)"), param(ISO8601Point('2012'), '2012-01-01', True, id="ISO, basic"), param(ISO8601Point('2012'), '2012*', True, id="ISO, glob"), param(ISO8601Point('2012'), '2012-*', False, id="ISO, bad glob (must use short datetime format)") ]) def test_point_match(itask_point: PointBase, point_str: Optional[str], expected: bool, set_cycling_type: Callable) -> None: """Test TaskProxy.point_match().""" set_cycling_type(itask_point.TYPE) mock_itask = Mock(point=itask_point.standardise()) assert TaskProxy.point_match(mock_itask, point_str) is expected
def test_satisfy_xtrigger(): """Test satisfy_xtriggers""" # the XtriggerManager instance xtrigger_mgr = XtriggerManager( suite="sample_suite", user="******", proc_pool=MockedProcPool(), broadcast_mgr=MockedBroadcastMgr(suite_db_mgr=None)) # the echo1 xtrig (not satisfied) echo1_xtrig = SubFuncContext(label="echo1", func_name="echo1", func_args=[], func_kwargs={}) echo1_xtrig.out = "[\"True\", {\"name\": \"herminia\"}]" xtrigger_mgr.add_trig("echo1", echo1_xtrig) # the echo2 xtrig (satisfied through callback later) echo2_xtrig = SubFuncContext(label="echo2", func_name="echo2", func_args=[], func_kwargs={}) echo2_xtrig.out = "[\"True\", {\"name\": \"herminia\"}]" xtrigger_mgr.add_trig("echo2", echo2_xtrig) # create a task tdef = TaskDef(name="foo", rtcfg=None, run_mode="live", start_point=1, spawn_ahead=False) tdef.xtrig_labels.add("echo1") tdef.xtrig_labels.add("echo2") # cycle point for task proxy init() start_point = ISO8601Point('20000101T0000+05') # create task proxy itask = TaskProxy(tdef=tdef, start_point=start_point) # we start with no satisfied xtriggers, and nothing active assert len(xtrigger_mgr.sat_xtrig) == 0 assert len(xtrigger_mgr.active) == 0 # after calling satisfy_xtriggers the first time, we get two active xtrigger_mgr.satisfy_xtriggers(itask) assert len(xtrigger_mgr.sat_xtrig) == 0 assert len(xtrigger_mgr.active) == 2 # calling satisfy_xtriggers again does not change anything xtrigger_mgr.satisfy_xtriggers(itask) assert len(xtrigger_mgr.sat_xtrig) == 0 assert len(xtrigger_mgr.active) == 2 # now we call callback manually as the proc_pool we passed is a mock # then both should be satisfied xtrigger_mgr.callback(echo1_xtrig) xtrigger_mgr.callback(echo2_xtrig) # so both were satisfied, and nothing is active assert len(xtrigger_mgr.sat_xtrig) == 2 assert len(xtrigger_mgr.active) == 0 # calling satisfy_xtriggers again still does not change anything xtrigger_mgr.satisfy_xtriggers(itask) assert len(xtrigger_mgr.sat_xtrig) == 2 assert len(xtrigger_mgr.active) == 0