예제 #1
0
def test_nested_schedulers():

    class MyCallback(Callback):
        def _start(self, dsk):
            self.dsk = dsk

        def _pretask(self, key, dsk, state):
            assert key in self.dsk

    inner_callback = MyCallback()
    inner_dsk = {'x': (add, 1, 2),
                 'y': (add, 'x', 3)}

    def nested_call(x):
        assert not Callback.active
        with inner_callback:
            return get_threaded(inner_dsk, 'y') + x

    outer_callback = MyCallback()
    outer_dsk = {'a': (nested_call, 1),
                 'b': (add, 'a', 2)}

    with outer_callback:
        get_threaded(outer_dsk, 'b')

    assert not Callback.active
    assert outer_callback.dsk == outer_dsk
    assert inner_callback.dsk == inner_dsk
    assert not Callback.active
예제 #2
0
def test_progressbar(capsys):
    with ProgressBar():
        out = get_threaded(dsk, 'e')
    assert out == 6
    check_bar_completed(capsys)
    with ProgressBar(width=20):
        out = get_threaded(dsk, 'e')
    check_bar_completed(capsys, 20)
예제 #3
0
def test_clean_exit(get):
    dsk = {'a': (lambda: 1 / 0, )}
    try:
        with ProgressBar() as pbar:
            get_threaded(dsk, 'a')
    except ZeroDivisionError:
        pass
    assert not pbar._running
    assert not pbar._timer.is_alive()
예제 #4
0
def test_with_alias(capsys):
    dsk = {'a': 1,
           'b': 2,
           'c': (add, 'a', 'b'),
           'd': (add, 1, 2),
           'e': 'd',
           'f': (mul, 'e', 'c')}
    with ProgressBar():
        get_threaded(dsk, 'f')
    check_bar_completed(capsys)
예제 #5
0
def test_register(capsys):
    try:
        p = ProgressBar()
        p.register()

        assert Callback.active

        get_threaded(dsk, 'e')
        check_bar_completed(capsys)

        p.unregister()

        assert not Callback.active
    finally:
        Callback.active.clear()
예제 #6
0
def test_register(capsys):
    try:
        p = ProgressBar()
        p.register()

        assert _globals['callbacks']

        get_threaded(dsk, 'e')
        check_bar_completed(capsys)

        p.unregister()

        assert not _globals['callbacks']
    finally:
        _globals['callbacks'].clear()
예제 #7
0
def test_with_cache(capsys):
    cachey = pytest.importorskip('cachey')
    from dask.cache import Cache
    c = cachey.Cache(10000)
    cc = Cache(c)

    with cc:
        with ProgressBar():
            assert get_threaded({'x': (mul, 1, 2)}, 'x') == 2
    check_bar_completed(capsys)
    assert c.data['x'] == 2

    with cc:
        with ProgressBar():
            assert get_threaded({'x': (mul, 1, 2), 'y': (mul, 'x', 3)}, 'y') == 6
    check_bar_completed(capsys)
예제 #8
0
def test_finish_always_called():
    flag = [False]

    class MyCallback(Callback):
        def _finish(self, dsk, state, errored):
            flag[0] = True
            assert errored

    dsk = {'x': (lambda: 1 / 0,)}

    # `raise_on_exception=True`
    try:
        with MyCallback():
            get_sync(dsk, 'x')
    except Exception as e:
        assert isinstance(e, ZeroDivisionError)
    assert flag[0]

    # `raise_on_exception=False`
    flag[0] = False
    try:
        with MyCallback():
            get_threaded(dsk, 'x')
    except Exception as e:
        assert isinstance(e, ZeroDivisionError)
    assert flag[0]

    # KeyboardInterrupt
    def raise_keyboard():
        raise KeyboardInterrupt()

    dsk = {'x': (raise_keyboard,)}
    flag[0] = False
    try:
        with MyCallback():
            get_sync(dsk, 'x')
    except BaseException as e:
        assert isinstance(e, KeyboardInterrupt)
    assert flag[0]
예제 #9
0
def test_finish_always_called():
    flag = [False]

    class MyCallback(Callback):
        def _finish(self, dsk, state, errored):
            flag[0] = True
            assert errored

    dsk = {"x": (lambda: 1 / 0, )}

    # `raise_on_exception=True`
    try:
        with MyCallback():
            get_sync(dsk, "x")
    except Exception as e:
        assert isinstance(e, ZeroDivisionError)
    assert flag[0]

    # `raise_on_exception=False`
    flag[0] = False
    try:
        with MyCallback():
            get_threaded(dsk, "x")
    except Exception as e:
        assert isinstance(e, ZeroDivisionError)
    assert flag[0]

    # KeyboardInterrupt
    def raise_keyboard():
        raise KeyboardInterrupt()

    dsk = {"x": (raise_keyboard, )}
    flag[0] = False
    try:
        with MyCallback():
            get_sync(dsk, "x")
    except BaseException as e:
        assert isinstance(e, KeyboardInterrupt)
    assert flag[0]
예제 #10
0
def test_minimum_time(capsys):
    with ProgressBar(1.0):
        out = get_threaded(dsk, "e")
    out, err = capsys.readouterr()
    assert out == "" and err == ""
예제 #11
0
def test_no_tasks(capsys):
    with ProgressBar():
        get_threaded({"x": 1}, "x")
    check_bar_completed(capsys)
예제 #12
0
 def nested_call(x):
     assert not _globals['callbacks']
     with inner_callback:
         return get_threaded(inner_dsk, 'y') + x
예제 #13
0
def test_store_time():
    p = ProgressBar()
    with p:
        get_threaded({"x": 1}, "x")

    assert isinstance(p.last_duration, float)
예제 #14
0
def test_no_tasks(capsys):
    with ProgressBar():
        get_threaded({'x': 1}, 'x')
    check_bar_completed(capsys)
예제 #15
0
 def nested_call(x):
     assert not Callback.active
     with inner_callback:
         return get_threaded(inner_dsk, 'y') + x
예제 #16
0
 def nested_call(x):
     assert not _globals['callbacks']
     with inner_callback:
         return get_threaded(inner_dsk, 'y') + x
예제 #17
0
def test_minimum_time(capsys):
    with ProgressBar(1.0):
        out = get_threaded(dsk, 'e')
    out, err = capsys.readouterr()
    assert out == '' and err == ''
예제 #18
0
def test_store_time():
    p = ProgressBar()
    with p:
        get_threaded({'x': 1}, 'x')

    assert isinstance(p.last_duration, float)