async def test_stream(): cs = ClientServer(DummyService, DummyServiceStub) async with cs as (_, stub): await stub.UnaryUnary(DummyRequest(value='ping')) handler = next(iter(cs.server._handlers)) handler.__gc_collect__() gc.collect() gc.disable() try: pre = set(collect()) await stub.UnaryUnary(DummyRequest(value='ping')) handler.__gc_collect__() post = collect() diff = set(post).difference(pre) diff.discard(id(pre)) for i in diff: try: print(repr(post[i])[:120]) except Exception: print('...') else: if 'grpclib.' in repr(post[i]): raise AssertionError('Memory leak detected') finally: gc.enable()
async def test_rpc_call(loop): ctx = ClientServer(PingServiceHandler, PingServiceStub, loop=loop, codec=JSONCodec()) async with ctx as (handler, stub): reply = await stub.UnaryUnary({'value': 'ping'}) assert reply == {'value': 'pong'}
async def test_stream_cancel_by_ping(): ctx = ClientServer(PingServiceHandler, PingServiceStub, config=Configuration(_keepalive_time=0.01, _keepalive_timeout=0.04, _http2_max_pings_without_data=1, )) # should be successful async with ctx as (handler, stub): await stub.UnaryStream(DummyRequest(value='ping')) assert ctx.channel._protocol.connection.last_ping_sent is not None # disable ping ack logic to cause a timeout and disconnect with patch.object(Connection, 'ping_ack_process') as p: p.return_value = None with pytest.raises(StreamTerminatedError, match='Connection lost'): async with ctx as (handler, stub): await stub.UnaryStream(DummyRequest(value='ping'))