async def _run():
            route_key = conf.PEER_QUEUE_NAME_FORMAT.format(
                amqp_key=conf.AMQP_KEY)
            peer_inner_service = PeerInnerService(conf.AMQP_TARGET,
                                                  route_key,
                                                  peer_service=None)
            peer_inner_service._callback_connection_lost_callback = lambda conn: None
            await peer_inner_service.connect()

            route_key = conf.CHANNEL_QUEUE_NAME_FORMAT.format(
                channel_name=conf.LOOPCHAIN_DEFAULT_CHANNEL,
                amqp_key=conf.AMQP_KEY)
            channel_inner_service = ChannelInnerService(conf.AMQP_TARGET,
                                                        route_key,
                                                        channel_service=None)
            channel_inner_service._callback_connection_lost_callback = lambda conn: None
            await channel_inner_service.connect()

            StubCollection().amqp_target = conf.AMQP_TARGET
            StubCollection().amqp_key = conf.AMQP_KEY

            await StubCollection().create_peer_stub()
            result = await StubCollection().peer_stub.async_task().hello()
            self.assertEqual(result, 'peer_hello')

            await StubCollection().create_channel_stub(
                conf.LOOPCHAIN_DEFAULT_CHANNEL)
            result = await StubCollection().channel_stubs[
                conf.LOOPCHAIN_DEFAULT_CHANNEL].async_task().hello()
            self.assertEqual(result, 'channel_hello')

            await peer_inner_service._connection.close()
            await channel_inner_service._connection.close()
    def test_peer_task(self):
        route_key = conf.PEER_QUEUE_NAME_FORMAT.format(amqp_key=conf.AMQP_KEY)

        service = PeerInnerService(conf.AMQP_TARGET,
                                   route_key,
                                   peer_service=None)
        service._callback_connection_lost_callback = lambda conn: None

        stub = PeerInnerStub(conf.AMQP_TARGET, route_key)
        stub._callback_connection_lost_callback = lambda conn: None

        async def _run():
            try:
                await service.connect(exclusive=True)
                await stub.connect()

                result = await stub.async_task().hello()
                self.assertEqual(result, 'peer_hello')

                bad_service = PeerInnerService(conf.AMQP_TARGET,
                                               route_key,
                                               peer_service=None)
                with self.assertRaises(ChannelClosed):
                    await bad_service.connect()

                with self.assertRaises(ChannelClosed):
                    await bad_service.connect(exclusive=True)
            finally:
                await service._connection.close()
                await stub._connection.close()

        loop = asyncio.get_event_loop()
        loop.run_until_complete(_run())