Ejemplo n.º 1
0
def assert_running(loop):
    """
    Raise if the given IOLoop is not running.
    """
    q = queue.Queue()
    loop.add_callback(q.put, 42)
    assert q.get(timeout=1) == 42
Ejemplo n.º 2
0
def assert_running(loop):
    """
    Raise if the given IOLoop is not running.
    """
    q = Queue()
    loop.add_callback(q.put, 42)
    assert q.get(timeout=1) == 42
Ejemplo n.º 3
0
def thread(loop):
    from threading import Thread, Event
    thread = Thread(target=loop.start)
    thread.daemon = True
    thread.start()

    event = Event()
    loop.add_callback(event.set)
    event.wait()

    return thread
Ejemplo n.º 4
0
def test_basic_no_loop():
    try:
        with LocalCluster(0, scheduler_port=0, silence_logs=False,
                          diagnostics_port=None) as cluster:
            with Client(cluster) as client:
                cluster.adapt()
                future = client.submit(lambda x: x + 1, 1)
                assert future.result() == 2
            loop = cluster.loop
    finally:
        loop.add_callback(loop.stop)
Ejemplo n.º 5
0
def test_basic_no_loop():
    try:
        with LocalCluster(0, scheduler_port=0, silence_logs=False,
                          diagnostics_port=None) as cluster:
            with Client(cluster) as client:
                cluster.adapt()
                future = client.submit(lambda x: x + 1, 1)
                assert future.result() == 2
            loop = cluster.loop
    finally:
        loop.add_callback(loop.stop)
Ejemplo n.º 6
0
def assert_not_running(loop):
    """
    Raise if the given IOLoop is running.
    """
    q = queue.Queue()
    try:
        loop.add_callback(q.put, 42)
    except RuntimeError:
        # On AsyncIOLoop, can't add_callback() after the loop is closed
        pass
    else:
        with pytest.raises(queue.Empty):
            q.get(timeout=0.02)
Ejemplo n.º 7
0
def test_basic_no_loop(loop):
    with clean(threads=False):
        try:
            with LocalCluster(
                0, scheduler_port=0, silence_logs=False, dashboard_address=None
            ) as cluster:
                with Client(cluster) as client:
                    cluster.adapt()
                    future = client.submit(lambda x: x + 1, 1)
                    assert future.result() == 2
                loop = cluster.loop
        finally:
            loop.add_callback(loop.stop)
Ejemplo n.º 8
0
def assert_not_running(loop):
    """
    Raise if the given IOLoop is running.
    """
    q = Queue()
    try:
        loop.add_callback(q.put, 42)
    except RuntimeError:
        # On AsyncIOLoop, can't add_callback() after the loop is closed
        pass
    else:
        with pytest.raises(Empty):
            q.get(timeout=0.02)
Ejemplo n.º 9
0
def test_sync_error(loop):
    e = Event()

    @gen.coroutine
    def wait_until_event():
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event,))
    thread.daemon = True
    thread.start()

    with pytest.raises(Exception):
        result = sync(loop, throws, 1)

    loop.add_callback(e.set)
    thread.join()
Ejemplo n.º 10
0
def test_sync_error(loop):
    e = Event()

    @gen.coroutine
    def wait_until_event():
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event, ))
    thread.daemon = True
    thread.start()

    with pytest.raises(Exception):
        result = sync(loop, throws, 1)

    loop.add_callback(e.set)
    thread.join()
Ejemplo n.º 11
0
def test_sync_timeout(loop):
    e = Event()

    @gen.coroutine
    def wait_until_event():
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event, ))
    thread.daemon = True
    thread.start()
    while not loop._running:
        sleep(0.01)

    with pytest.raises(gen.TimeoutError):
        sync(loop, gen.sleep, 0.5, callback_timeout=0.05)

    loop.add_callback(e.set)
    thread.join()
Ejemplo n.º 12
0
def test_as_completed_cancel_last(loop):
    with cluster() as (s, [a, b]):
        with Client(s['address'], loop=loop) as c:
            w = c.submit(sleep, 0.3)
            x = c.submit(inc, 1)
            y = c.submit(sleep, 0.3)

            @gen.coroutine
            def _():
                yield gen.sleep(0.1)
                yield w.cancel(asynchronous=True)
                yield y.cancel(asynchronous=True)

            loop.add_callback(_)

            ac = as_completed([x, y])
            result = list(ac)

            assert result == [x]
Ejemplo n.º 13
0
def test_sync(loop):
    e = Event()
    e2 = threading.Event()

    @gen.coroutine
    def wait_until_event():
        e2.set()
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event,))
    thread.daemon = True
    thread.start()

    e2.wait()
    result = sync(loop, inc, 1)
    assert result == 2

    loop.add_callback(e.set)
    thread.join()
Ejemplo n.º 14
0
def test_sync(loop):
    e = Event()
    e2 = threading.Event()

    @gen.coroutine
    def wait_until_event():
        e2.set()
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event, ))
    thread.daemon = True
    thread.start()

    e2.wait()
    result = sync(loop, inc, 1)
    assert result == 2

    loop.add_callback(e.set)
    thread.join()
Ejemplo n.º 15
0
def test_sync_error(loop):
    e = Event()

    @gen.coroutine
    def wait_until_event():
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event, ))
    thread.daemon = True
    thread.start()
    while not loop._running:
        sleep(0.01)

    try:
        result = sync(loop, throws, 1)
    except Exception as exc:
        f = exc
        assert 'hello' in str(exc)
        tb = get_traceback()
        L = traceback.format_tb(tb)
        assert any('throws' in line for line in L)

    def function1(x):
        return function2(x)

    def function2(x):
        return throws(x)

    try:
        result = sync(loop, function1, 1)
    except Exception as exc:
        assert 'hello' in str(exc)
        tb = get_traceback()
        L = traceback.format_tb(tb)
        assert any('function1' in line for line in L)
        assert any('function2' in line for line in L)

    loop.add_callback(e.set)
    thread.join()
Ejemplo n.º 16
0
def test_sync_error(loop):
    e = Event()

    @gen.coroutine
    def wait_until_event():
        yield e.wait()

    thread = Thread(target=loop.run_sync, args=(wait_until_event,))
    thread.daemon = True
    thread.start()
    while not loop._running:
        sleep(0.01)

    try:
        result = sync(loop, throws, 1)
    except Exception as exc:
        f = exc
        assert 'hello' in str(exc)
        tb = get_traceback()
        L = traceback.format_tb(tb)
        assert any('throws' in line for line in L)

    def function1(x):
        return function2(x)

    def function2(x):
        return throws(x)

    try:
        result = sync(loop, function1, 1)
    except Exception as exc:
        assert 'hello' in str(exc)
        tb = get_traceback()
        L = traceback.format_tb(tb)
        assert any('function1' in line for line in L)
        assert any('function2' in line for line in L)

    loop.add_callback(e.set)
    thread.join()