From 545f3120503f305e3f29c5e00f13f1186f4bcc90 Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Tue, 18 Feb 2020 10:36:56 -0800 Subject: [PATCH] Simplify the logic and improve readability --- .../grpc_health/v1/_async.py | 30 +++++++++---------- .../health_check/health_servicer_test.py | 6 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/python/grpcio_health_checking/grpc_health/v1/_async.py b/src/python/grpcio_health_checking/grpc_health/v1/_async.py index acf8d5700cb..6bbb8697d94 100644 --- a/src/python/grpcio_health_checking/grpc_health/v1/_async.py +++ b/src/python/grpcio_health_checking/grpc_health/v1/_async.py @@ -39,25 +39,25 @@ class AsyncHealthServicer(_health_pb2_grpc.HealthServicer): return _health_pb2.HealthCheckResponse(status=status) async def Watch(self, request: _health_pb2.HealthCheckRequest, context): - status = self._server_status.get(request.service) - - if status is None: - status = _health_pb2.HealthCheckResponse.SERVICE_UNKNOWN - + condition = self._server_watchers[request.service] try: - condition = self._server_watchers[request.service] async with condition: - # Responds with current health state - await context.write( - _health_pb2.HealthCheckResponse(status=status)) - - # Polling on health state changes while True: - await condition.wait() - status = self._server_status.get(request.service) - await context.write( - _health_pb2.HealthCheckResponse(status=status)) + + if status: + # Responds with current health state + await context.write( + _health_pb2.HealthCheckResponse(status=status)) + else: + # Responds with default value + await context.write( + _health_pb2.HealthCheckResponse( + status=_health_pb2.HealthCheckResponse. + SERVICE_UNKNOWN)) + + # Polling on health state changes + await condition.wait() finally: del self._server_watchers[request.service] diff --git a/src/python/grpcio_tests/tests_aio/health_check/health_servicer_test.py b/src/python/grpcio_tests/tests_aio/health_check/health_servicer_test.py index 78527057903..38ee6bb175f 100644 --- a/src/python/grpcio_tests/tests_aio/health_check/health_servicer_test.py +++ b/src/python/grpcio_tests/tests_aio/health_check/health_servicer_test.py @@ -143,6 +143,8 @@ class HealthServicerTest(AioTestBase): await self._servicer.set('some-other-service', health_pb2.HealthCheckResponse.SERVING) + # The change of health status in other service should be isolated. + # Hence, no additional notification should be observed. with self.assertRaises(asyncio.TimeoutError): await asyncio.wait_for(queue.get(), test_constants.SHORT_TIMEOUT) @@ -193,8 +195,8 @@ class HealthServicerTest(AioTestBase): await task # Wait for the serving coroutine to process client cancellation. - timeout = time.time() + test_constants.TIME_ALLOWANCE - while (time.time() < timeout and self._servicer._server_watchers): + timeout = time.monotonic() + test_constants.TIME_ALLOWANCE + while (time.monotonic() < timeout and self._servicer._server_watchers): await asyncio.sleep(1) self.assertFalse(self._servicer._server_watchers, 'There should not be any watcher left')