1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
import asyncio
import pytest
import async_timeout
from grpclib.const import Status
from grpclib.testing import ChannelFor
from grpclib.exceptions import GRPCError
from grpclib.health.check import ServiceCheck, ServiceStatus
from grpclib.health.service import Health
from grpclib.health.v1.health_pb2 import HealthCheckRequest, HealthCheckResponse
from grpclib.health.v1.health_grpc import HealthStub
class Check:
__current_status__ = None
async def __call__(self):
return self.__current_status__
SERVICE_NAME = 'namespace.ServiceName'
class Service:
async def Foo(self, stream):
raise NotImplementedError
def __mapping__(self):
return {'/{}/Foo'.format(SERVICE_NAME): self.Foo}
@pytest.mark.asyncio
async def test_check_unknown_service():
svc = Service()
health = Health({svc: []})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
with pytest.raises(GRPCError) as err:
await stub.Check(HealthCheckRequest(service='Unknown'))
assert err.value.status == Status.NOT_FOUND
@pytest.mark.asyncio
async def test_check_zero_checks():
svc = Service()
health = Health({svc: []})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
response = await stub.Check(HealthCheckRequest(service=SERVICE_NAME))
assert response == HealthCheckResponse(
status=HealthCheckResponse.SERVING,
)
@pytest.mark.asyncio
@pytest.mark.parametrize('v1, v2, status', [
(None, None, HealthCheckResponse.UNKNOWN),
(True, False, HealthCheckResponse.NOT_SERVING),
(False, True, HealthCheckResponse.NOT_SERVING),
(True, True, HealthCheckResponse.SERVING)
])
async def test_check_service_check(loop, v1, v2, status):
svc = Service()
c1 = Check()
c2 = Check()
health = Health({svc: [
ServiceCheck(c1, check_ttl=0),
ServiceCheck(c2, check_ttl=0),
]})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
c1.__current_status__ = v1
c2.__current_status__ = v2
response = await stub.Check(HealthCheckRequest(service=SERVICE_NAME))
assert response == HealthCheckResponse(status=status)
@pytest.mark.asyncio
@pytest.mark.parametrize('v1, v2, status', [
(None, None, HealthCheckResponse.UNKNOWN),
(True, False, HealthCheckResponse.NOT_SERVING),
(False, True, HealthCheckResponse.NOT_SERVING),
(True, True, HealthCheckResponse.SERVING)
])
async def test_check_service_status(v1, v2, status):
svc = Service()
s1 = ServiceStatus()
s2 = ServiceStatus()
health = Health({svc: [s1, s2]})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
s1.set(v1)
s2.set(v2)
response = await stub.Check(HealthCheckRequest(service=SERVICE_NAME))
assert response == HealthCheckResponse(status=status)
@pytest.mark.asyncio
async def test_watch_unknown_service():
svc = Service()
health = Health({svc: []})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
async with stub.Watch.open() as stream:
await stream.send_message(HealthCheckRequest(service='Unknown'),
end=True)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.SERVICE_UNKNOWN,
)
try:
async with async_timeout.timeout(0.01):
assert not await stream.recv_message()
except asyncio.TimeoutError:
pass
await stream.cancel()
@pytest.mark.asyncio
async def test_watch_zero_checks():
svc = Service()
health = Health({svc: []})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
async with stub.Watch.open() as stream:
await stream.send_message(HealthCheckRequest(service=SERVICE_NAME),
end=True)
response = await stream.recv_message()
assert response == HealthCheckResponse(
status=HealthCheckResponse.SERVING,
)
try:
async with async_timeout.timeout(0.01):
assert not await stream.recv_message()
except asyncio.TimeoutError:
pass
await stream.cancel()
@pytest.mark.asyncio
async def test_watch_service_check():
svc = Service()
c1 = Check()
c2 = Check()
health = Health({svc: [
ServiceCheck(c1, check_ttl=0.001),
ServiceCheck(c2, check_ttl=0.001),
]})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
async with stub.Watch.open() as stream:
await stream.send_message(HealthCheckRequest(service=SERVICE_NAME),
end=True)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.UNKNOWN,
)
# check that there are no unnecessary messages
try:
async with async_timeout.timeout(0.01):
assert not await stream.recv_message()
except asyncio.TimeoutError:
pass
c1.__current_status__ = True
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.NOT_SERVING,
)
c2.__current_status__ = True
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.SERVING,
)
c1.__current_status__ = False
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.NOT_SERVING,
)
c1.__current_status__ = True
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.SERVING,
)
await stream.cancel()
@pytest.mark.asyncio
async def test_watch_service_status():
svc = Service()
s1 = ServiceStatus()
s2 = ServiceStatus()
health = Health({svc: [s1, s2]})
async with ChannelFor([svc, health]) as channel:
stub = HealthStub(channel)
async with stub.Watch.open() as stream:
await stream.send_message(HealthCheckRequest(service=SERVICE_NAME),
end=True)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.UNKNOWN,
)
s1.set(True)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.NOT_SERVING,
)
s2.set(True)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.SERVING,
)
s1.set(False)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.NOT_SERVING,
)
s1.set(True)
assert await stream.recv_message() == HealthCheckResponse(
status=HealthCheckResponse.SERVING,
)
# check that there are no unnecessary messages if status isn't
# changed
s1.set(True)
try:
async with async_timeout.timeout(0.01):
assert not await stream.recv_message()
except asyncio.TimeoutError:
pass
await stream.cancel()
|