async def test_channel_ready(loop): async with ChannelFor([WorkingDummyService()]) as _channel: channel = Channel() async def _create_connection(): await asyncio.sleep(0.01) return _channel._protocol with patch.object(channel, '_create_connection', _create_connection): assert channel._state is _ChannelState.IDLE connect_task = loop.create_task(channel.__connect__()) await asyncio.sleep(0.001) assert channel._state is _ChannelState.CONNECTING await connect_task assert channel._state is _ChannelState.READY channel.close() assert channel._state is _ChannelState.IDLE
async def test_channel_transient_failure(loop): channel = Channel() async def _create_connection(): await asyncio.sleep(0.01) raise asyncio.TimeoutError('try again later') with patch.object(channel, '_create_connection', _create_connection): assert channel._state is _ChannelState.IDLE connect_task = loop.create_task(channel.__connect__()) await asyncio.sleep(0.001) assert channel._state is _ChannelState.CONNECTING with pytest.raises(asyncio.TimeoutError, match='try again later'): await connect_task assert channel._state is _ChannelState.TRANSIENT_FAILURE channel.close() assert channel._state is _ChannelState.IDLE