def test_timing_02(capsys): '''stopping in state STARTED after action''' ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Task( print_it, args=(ts, 'hello'), action_stop=print_it, args_stop=(ts, 'stopped'), action_cont=print_it, args_cont=(ts, 'continued'), duration=.2) + Task(print_it, args=(ts, 'finished')) t.start(.1) sleep(.2) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_SLEEP t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.1:started 0.1:hello 0.2:stopped ' sleep(.1) t.cont().join() assert t.state == STATE_FINISHED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.3:continued 0.4:finished '
def test_standard(capsys): ts = Timespan() acc = Accelerate(ts, 0.3) t = Task(print_it, args=(ts, 'started')) + Repeated( acc.step, action_stop=print_it, args_stop=(ts, 'stopped'), action_cont=print_it, args_cont=(ts, 'continued')) + Task(print_it, args=(ts, 'finished')) t.start() sleep(.4) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_SLEEP t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.0:started 0.0:hi 0.3:hi 0.4:stopped ' t.cont(.1).join() assert t.state == STATE_FINISHED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.out == \ '0.5:continued 0.6:hi 0.7:hi 0.7:hi ' + \ '0.7:finished '
def test_action_start(capsys): # without delay t = Task( print, args=('started', ), kwargs={'end': ' '}, ) + Task(print, args=('hello, world!', ), kwargs={'end': ''}, action_stop=print, args_stop=(' stopped', ), kwargs_stop={'end': ''}) t.start() t.join() assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == 'started hello, world!' # with delay t.start(.1) sleep(.05) assert t.state == STATE_TO_START t.stop().join() assert t.state == STATE_STOPPED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '' t.cont().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == 'started hello, world!'
def test_repeated(capsys): ts = Timespan() t = Task( print_it, args=(ts, 'parent_started') ) + Repeated( Task( print_it, args=(ts, 'child_started'), ) + Task( print_it, duration=.2, args=(ts, 'child'), action_stop=print_it, args_stop=(ts, 'child_stopped'), action_cont=print_it, args_cont=(ts, 'child_continued') ) + Task( print_it, args=(ts, 'child_finished') ), num=2, action_stop=print_it, args_stop=(ts, 'parent_stopped'), action_cont=print_it, args_cont=(ts, 'parent_continued') ) + Task( print_it, args=(ts, 'parent_finished') ) t.start() sleep(.3) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_BUSY t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.0:parent_started 0.0:child_started 0.0:child ' + \ '0.2:child_finished 0.2:child_started 0.2:child ' + \ '0.3:child_stopped ' sleep(.1) t.cont(thread=False) assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.4:child_continued ' + \ '0.5:child_finished 0.5:parent_finished '
def test_join_03(capsys): '''stop child from outside --> no continuous joining''' ts = Timespan() t_child = Task(print_it, args=(ts, 'child'), duration=.2) t_parent = concat( Task( print_it, args=(ts, 'parent-root-link'), duration=.1 ), Task(t_child.start), Task(t_child.join), Task(print_it, args=(ts, 'parent-link-4'), duration=.1), Task(print_it, args=(ts, 'parent-finished')) ).start() sleep(.2) assert t_parent.state == STATE_STARTED assert t_parent.activity == ACTIVITY_JOIN assert t_child.state == STATE_STARTED assert t_child.activity == ACTIVITY_SLEEP t_child.stop().join() assert t_parent.state == STATE_STARTED assert t_parent.activity == ACTIVITY_SLEEP assert t_child.state == STATE_STOPPED assert t_child.activity == ACTIVITY_NONE assert len(t_parent.children) == 0 assert t_child.parent is None captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.0:parent-root-link 0.1:child 0.2:parent-link-4 ' t_parent.stop().join() assert t_parent.state == STATE_STOPPED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '' sleep(.2) t_parent.cont().join() assert t_parent.state == STATE_FINISHED assert t_child.state == STATE_STOPPED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.5:parent-finished '
def test_action_stop(capsys): t = Task(print, args=('hello, world!', ), kwargs={'end': ''}, duration=.1, action_stop=print, args_stop=(' stopped', ), kwargs_stop={'end': ''}) + Task( print, args=(' finished', ), kwargs={'end': ''}) t.start() sleep(.05) t.stop() t.join() captured = capsys.readouterr() assert t.state == STATE_STOPPED assert captured.err == '' assert captured.out == 'hello, world! stopped'
def test_links_threadless_stop(): '''stop threadless child from outside''' t_child = Task(do_nothing, duration=.1) t_parent = concat( Task(do_nothing, duration=.1), Task(t_child), Task(do_nothing, duration=.1) ).start() sleep(.15) t_child.stop() sleep(.01) assert t_parent.state == STATE_STARTED assert t_child.state == STATE_STOPPED assert len(t_parent.children) == 0 assert t_child.parent is None t_parent.stop().join() assert t_parent.state == STATE_STOPPED
def test_timing_01(capsys): '''stopping in state TO_START (in delay, before started)''' ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Task( print_it, args=(ts, 'hello'), duration=.1) + Task( print_it, args=(ts, 'finished')) t.start(.2) sleep(.1) assert t.state == STATE_TO_START t.stop().join() assert t.state == STATE_STOPPED sleep(.1) t.cont().join() assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.3:started 0.3:hello 0.4:finished '
def test_periodic(capsys): ts = Timespan() t = Task( print_it, args=(ts, 'parent_started') ) + Periodic( .1, ( Task( print_it, args=(ts, 'child_started') ) + Task( print_it, args=(ts, 'child'), action_stop=print_it, args_stop=(ts, 'child_stopped'), action_cont=print_it, args_cont=(ts, 'child_continued') ) + Task( print_it, args=(ts, 'child_finished') ) ).start, args=(.2,), kwargs={'thread': False}, num=3, action_stop=print_it, args_stop=(ts, 'parent_stopped'), action_cont=print_it, args_cont=(ts, 'parent_continued') ) + Task( print_it, args=(ts, 'parent_finished') ) t.start() sleep(.25) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_BUSY captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.0:parent_started 0.2:child_started 0.2:child ' + \ '0.2:child_finished ' sleep(.05) t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.3:parent_stopped ' sleep(.1) t.cont(thread=False) assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.4:parent_continued ' + \ '0.5:child_started 0.5:child 0.5:child_finished ' + \ '0.7:child_started 0.7:child 0.7:child_finished ' + \ '0.7:parent_finished '