async def test_first_step_returns_code_after_remote_eval(event_loop): channel = Channel() code = object() with suppress(asyncio.TimeoutError): await asyncio.wait_for(channel.remote_eval(code), 1) assert await channel.first_step() is code
async def test_first_remote_eval_hangs_after_first_step(event_loop): channel = Channel() task = asyncio.create_task(channel.first_step()) try: with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(channel.remote_eval(None), 1) finally: task.cancel()
async def test_first_remote_eval_returns_first_next_step_data(event_loop): channel = Channel() result = object() task = asyncio.create_task(channel.remote_eval(None)) await channel.first_step() with suppress(asyncio.TimeoutError): await asyncio.wait_for(channel.next_step(result), 1) assert await task is result
async def test_remote_eval_cant_be_used_in_parallel(event_loop): channel = Channel() async def right_side(): await channel.first_step() await channel.next_step(None) async def left_side(): await channel.remote_eval(None) task_left1 = asyncio.create_task(left_side()) task_left2 = asyncio.create_task(left_side()) task_right = asyncio.create_task(right_side()) # One of the left tasks should finish done, pending = await asyncio.wait([task_left1, task_left2], return_when=asyncio.FIRST_COMPLETED) assert len(done) == len(pending) == 1 pending = pending.pop() # The other should hang with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(pending, timeout=1) task_right.cancel() pending.cancel()
async def test_channel_call_next_step_if_exists(event_loop): last_result = object() with patch('brainslug.runtime.AGENT_INFO', new=AsyncTinyDB()) as CHANNELS: with patch('brainslug.channel.Channel.next_step') as next_step: next_step.return_value = asyncio.sleep(0) await CHANNELS.insert({'__key__': None, '__channel__': Channel()}) await process_agent_request(None, None, {}, last_result) next_step.assert_called_once_with(last_result)
async def test_par_return_next_code_when_channel_exists(event_loop): next_code = object() async def _next_code(): return next_code with patch('brainslug.runtime.AGENT_INFO', new=AsyncTinyDB()) as CHANNELS: with patch('brainslug.channel.Channel.next_step') as next_step: next_step.return_value = _next_code() await CHANNELS.insert({'__key__': None, '__channel__': Channel()}) assert await process_agent_request(None, None, {}, None) is next_code
async def process_agent_request(ribosome, key, meta, last_result): Q = Query() try: [document] = runtime.AGENT_INFO.search(Q['__key__'] == key) except ValueError: document = { **meta, '__key__': key, '__ribosome__': ribosome, '__channel__': Channel() } await runtime.AGENT_INFO.insert(document) return await document['__channel__'].first_step() else: return await document['__channel__'].next_step(last_result)
async def test_first_next_step_returns_second_remote_eval_code(event_loop): channel = Channel() code = object() async def right_side(): await channel.first_step() return await channel.next_step(None) async def left_side(): await channel.remote_eval(None) await channel.remote_eval(code) task_right = asyncio.create_task(right_side()) task_left = asyncio.create_task(left_side()) try: assert await task_right is code finally: task_left.cancel()
async def test_remote_eval_hangs_without_the_other_side(event_loop): channel = Channel() with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(channel.remote_eval(None), 1)
async def test_first_step_hangs_without_the_other_side(event_loop): channel = Channel() with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(channel.first_step(), 1)
def test_channel_needs_no_parameters(): Channel() # SHOULD NOT RAISE