def watch_connections(address, event): # Sleep for 4 seconds to ensure that the Python gRPC server has started time.sleep(4) number_of_failures = 0 timeout_sec = 1 while True: # If there are three failures, shutdown the gRPC server. if number_of_failures == 3: event.set() break channel = grpc.insecure_channel(address) try: channelz_stub = channelz_pb2_grpc.ChannelzStub(channel) # Wait for the channel to be ready. grpc.channel_ready_future(channel).result(timeout=timeout_sec) except grpc.FutureTimeoutError: number_of_failures += 1 continue servers = channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) sockets = channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest( server_id=servers.server[0].ref.server_id, start_socket_id=0)) # There should be always more than one socket connected to the server if len(sockets.socket_ref) == 1: number_of_failures += 1 else: number_of_failures = 0 # Sleep for 2 seconds before polling next time time.sleep(2)
async def test_invalid_query_get_server_sockets(self): with self.assertRaises(aio.AioRpcError) as exception_context: await self._channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest( server_id=_LARGE_UNASSIGNED_ID, start_socket_id=0, )) self.assertEqual(grpc.StatusCode.NOT_FOUND, exception_context.exception.code())
def test_invalid_query_get_server_sockets(self): try: self._channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest( server_id=10000, start_socket_id=0, )) except BaseException as e: self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected')
def test_server_sockets(self): self._pairs = _generate_channel_server_pairs(1) self._send_successful_unary_unary(0) self._send_failed_unary_unary(0) gs_resp = self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.assertEqual(len(gs_resp.server), 1) self.assertEqual(gs_resp.server[0].data.calls_started, 2) self.assertEqual(gs_resp.server[0].data.calls_succeeded, 1) self.assertEqual(gs_resp.server[0].data.calls_failed, 1) gss_resp = self._channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest( server_id=gs_resp.server[0].ref.server_id, start_socket_id=0))
async def test_server_sockets(self): pairs = await _create_channel_server_pairs(1, self._channelz_stub) await self._send_successful_unary_unary(pairs[0]) await self._send_failed_unary_unary(pairs[0]) resp = await self._get_server_by_ref_id(pairs[0].server_ref_id) self.assertEqual(resp.data.calls_started, 2) self.assertEqual(resp.data.calls_succeeded, 1) self.assertEqual(resp.data.calls_failed, 1) gss_resp = await self._channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest(server_id=resp.ref.server_id, start_socket_id=0)) # If the RPC call failed, it will raise a grpc.RpcError # So, if there is no exception raised, considered pass await _destroy_channel_server_pairs(pairs)