Simplify the logic and improve readability

pull/22028/head
Lidi Zheng 5 years ago
parent 5622e9e78c
commit 545f312050
  1. 30
      src/python/grpcio_health_checking/grpc_health/v1/_async.py
  2. 6
      src/python/grpcio_tests/tests_aio/health_check/health_servicer_test.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]

@ -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')

Loading…
Cancel
Save