Example #1
0
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 '
Example #2
0
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_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_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 '
Example #5
0
def test_action_cont(capsys):
    # with duration
    t = Task(print,
             args=('hello, world!', ),
             kwargs={'end': ''},
             duration=.1,
             action_stop=print,
             args_stop=(' stopped', ),
             kwargs_stop={'end': ''},
             action_cont=print,
             args_cont=(' continued', ),
             kwargs_cont={'end': ''}) + Task(
                 print, args=(' finished', ), kwargs={'end': ''})
    t.start().stop()
    t.join()
    assert t.state == STATE_STOPPED
    t.cont().join()
    captured = capsys.readouterr()
    assert t.state == STATE_FINISHED
    assert captured.err == ''
    assert captured.out == 'hello, world! stopped continued finished'

    # without duration
    t = Task(print,
             args=('hello, world!', ),
             kwargs={'end': ''},
             action_stop=print,
             args_stop=(' stopped', ),
             kwargs_stop={'end': ''},
             action_cont=print,
             args_cont=(' continued', ),
             kwargs_cont={'end': ''}) + Task(
                 print, args=(' finished', ), kwargs={'end': ''})
    t.start().stop()
    t.join()
    assert t.state == STATE_FINISHED
    t.cont().join()
    captured = capsys.readouterr()
    assert t.state == STATE_FINISHED
    assert captured.err == ''
    assert captured.out == 'hello, world! finished'
Example #6
0
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 '