Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def __aenter__(self):
self.temp = tempfile.mkdtemp()
self.sock = os.path.join(self.temp, 'grpclib.sock')
dummy_service = DummyService()
self.server = Server([dummy_service])
await self.server.start(path=self.sock)
self.channel = Channel(path=self.sock)
dummy_stub = DummyServiceStub(self.channel)
return dummy_service, dummy_stub
async def test_concurrent_connect(loop):
count = 5
reqs = [DummyRequest(value='ping') for _ in range(count)]
reps = [DummyReply(value='pong') for _ in range(count)]
channel = Channel()
stub = DummyServiceStub(channel)
async with ChannelFor([DummyService()]) as _channel:
with patch.object(loop, 'create_connection') as po:
po.side_effect = _create_connection_gen(_channel._protocol)
tasks = [loop.create_task(stub.UnaryUnary(req)) for req in reqs]
replies = await asyncio.gather(*tasks)
assert replies == reps
po.assert_called_once_with(ANY, '127.0.0.1', 50051, ssl=None)
async def __aenter__(self):
host = '127.0.0.1'
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('127.0.0.1', 0))
_, port = s.getsockname()
handler = self.handler_cls()
self.server = server.Server([handler], codec=self.codec)
await self.server.start(host, port)
self.channel = client.Channel(host, port, codec=self.codec,
config=self.config)
stub = self.stub_cls(self.channel)
return handler, stub
def channel_fixture(loop, port):
services = [DummyService()]
services = ServerReflection.extend(services)
server = Server(services)
loop.run_until_complete(server.start(port=port))
channel = Channel(port=port)
try:
yield channel
finally:
channel.close()
server.close()
loop.run_until_complete(server.wait_closed())
async def __started_async(self):
# self.__logger.log_debug("Connecting to address {_address}")
host, port = self._address.split(':')
try:
self._channel = Channel(host=host, port=port, loop=asyncio.get_event_loop())
self._client = RemotingStub(self._channel)
res = await self._client.Connect(ConnectRequest())
self._serializer_id = res.default_serializer_id
except Exception:
# self.__logger.log_error("GRPC Failed to connect to address {_address}\n{ex}")
await asyncio.sleep(2000)
raise Exception()
await GlobalEventStream.publish(EndpointConnectedEvent(self._address))
# self.__logger.log_debug("")
async def run(parser: ArgumentParser, args: Namespace,
command_cls: Type[ClientCommand]) -> int:
loop = asyncio.get_event_loop()
path = args.socket or AdminService.get_socket_path()
channel = Channel(path=path, loop=loop)
stub = AdminStub(channel)
command = command_cls(stub, args)
try:
code = await command.run(args.outfile)
finally:
channel.close()
return code
async def provide_client(self) -> CompanionClient:
await self.daemon_spawner.start_daemon_if_needed(
force_kill=self.force_kill_daemon
)
if not self.channel or not self.stub:
self.channel = Channel(self.host, self.port, loop=asyncio.get_event_loop())
self.stub = CompanionServiceStub(channel=self.channel)
return CompanionClient(
stub=self.stub, is_local=True, udid=self.target_udid, logger=self.logger
)
if platform.system() == "Darwin" and self.target_udid:
self.logger.info(
f"will attempt to spawn a companion for {self.target_udid}"
)
port = await self.companion_spawner.spawn_companion(
target_udid=self.target_udid
)
host = "localhost"
self.companion_info = CompanionInfo(
host=host, port=port, udid=self.target_udid, is_local=True
)
self.direct_companion_manager.add_companion(self.companion_info)
else:
raise e
self.logger.info(f"using companion {self.companion_info}")
channel = Channel(
self.companion_info.host,
self.companion_info.port,
loop=asyncio.get_event_loop(),
)
yield CompanionServiceStub(channel=channel)
finally:
if channel:
channel.close()
def get_stub_for_address(self, host: str, port: int) -> CompanionServiceStub:
self._logger.debug(f"creating grpc stub for companion at {host}:{port}")
channel = Channel(host, port, loop=asyncio.get_event_loop())
return CompanionServiceStub(channel)