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
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)
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}
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()
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()
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
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()
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
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()
def test_slug_call_calls_fn(): fn = Mock() slug = Slug(fn=fn, spec=None) slug() fn.assert_called_once()
def test_slug_stores_attributes(): fn = object() spec = object() slug = Slug(fn, spec) assert slug.fn is fn assert slug.spec is spec
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