Ejemplo n.º 1
0
async def test_run_in_thread_returns_same_as_fn(event_loop):
    expected = object()

    def fn():
        return expected

    slug = Slug(None, None)
    assert await slug.run_in_thread(event_loop, fn) is expected
Ejemplo n.º 2
0
async def test_run_in_thread_raise_fn_exception(event_loop):
    class CustomException(Exception):
        pass

    def fn():
        raise CustomException()

    slug = Slug(None, None)
    with pytest.raises(CustomException):
        await slug.run_in_thread(event_loop, fn)
Ejemplo n.º 3
0
def test_slug_create_respect_parameters():
    spec = object()

    def fn():
        pass

    slug = Slug.create(spec=spec)(fn)

    assert slug.fn is fn
    assert slug.spec == {'spec': spec}
Ejemplo n.º 4
0
async def test_attach_resources_waits_for_resources(event_loop):
    slug = Slug(lambda: None, None)

    async def _wait_for_resources():
        return {}

    with patch('brainslug.runtime.wait_for_resources') as wait_for_resources:
        wait_for_resources.return_value = _wait_for_resources()
        await slug.attach_resources(event_loop)
        wait_for_resources.assert_called_once()
Ejemplo n.º 5
0
async def test_run_slug_starts_the_web_server(event_loop):
    async def ret_runner():
        runner = Mock()
        runner.cleanup.return_value = asyncio.sleep(0)
        return runner

    with patch('brainslug.web.run_web_server') as run_web_server:
        with patch.dict('brainslug.ribosome.RIBOSOMES', clear=True):
            run_web_server.return_value = ret_runner()
            await run_slug(Slug(lambda: None, {}))
            run_web_server.assert_called_once()
Ejemplo n.º 6
0
def test_slug_is_a_passthrough_callable():
    expected_a = object()
    expected_b = object()
    expected_ret = object()

    def fn(current_a, current_b=None):
        assert current_a is expected_a
        return expected_ret

    slug = Slug(fn=fn, spec=None)

    assert slug(expected_a, current_b=expected_b) is expected_ret
Ejemplo n.º 7
0
async def test_run_slug_stops_the_web_server_at_exit(event_loop):
    @Slug.create()
    def foo():
        raise Exception

    runner = Mock()

    async def ret_runner():
        return runner

    with patch('brainslug.web.run_web_server') as run_web_server:
        with patch.dict('brainslug.ribosome.RIBOSOMES', clear=True):
            run_web_server.return_value = ret_runner()
            with suppress(Exception):
                await run_slug(Slug(foo, {}))
            runner.cleanup.assert_called_once()
Ejemplo n.º 8
0
async def test_attach_resources_return_fn_wrapped(event_loop):
    resources = object()
    called = False

    def fn(arg):
        nonlocal called
        assert arg is resources
        called = True

    async def _wait_for_resources():
        return {'arg': resources}

    slug = Slug(fn, None)
    with patch('brainslug.runtime.wait_for_resources') as wait_for_resources:
        wait_for_resources.return_value = _wait_for_resources()
        wrapped = await slug.attach_resources(event_loop)
        wrapped()
        assert called
Ejemplo n.º 9
0
async def test_run_in_thread_calls_function(event_loop):
    fn = Mock()
    slug = Slug(None, None)
    res = await slug.run_in_thread(event_loop, fn)
    fn.assert_called_once()
Ejemplo n.º 10
0
def test_slug_call_calls_fn():
    fn = Mock()
    slug = Slug(fn=fn, spec=None)
    slug()
    fn.assert_called_once()
Ejemplo n.º 11
0
def test_slug_stores_attributes():
    fn = object()
    spec = object()
    slug = Slug(fn, spec)
    assert slug.fn is fn
    assert slug.spec is spec
Ejemplo n.º 12
0
async def test_run_in_thread_calls_function_in_a_thread(event_loop):
    slug = Slug(None, None)
    this_thread = threading.currentThread()
    other_thread = await slug.run_in_thread(event_loop,
                                            threading.currentThread)
    assert this_thread != other_thread