fix(clients/python): concurrency-safe Session.close() caching the real reply (mxaccessgw#202)
This commit is contained in:
@@ -374,3 +374,34 @@ class FakeStream:
|
||||
|
||||
def cancel(self) -> None:
|
||||
self.cancelled = True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_session_close_is_concurrency_safe_and_caches_real_reply() -> None:
|
||||
stub = FakeGatewayStub()
|
||||
real = stub.close_session.replies[0]
|
||||
real.final_state = pb.SESSION_STATE_CLOSED
|
||||
original_call = stub.close_session.__call__
|
||||
|
||||
async def slow_close(request: Any, *, metadata: Any) -> Any:
|
||||
await asyncio.sleep(0.01)
|
||||
return await original_call(request, metadata=metadata)
|
||||
|
||||
class SlowUnary:
|
||||
requests = stub.close_session.requests
|
||||
|
||||
async def __call__(self, request: Any, *, metadata: Any) -> Any:
|
||||
return await slow_close(request, metadata=metadata)
|
||||
|
||||
stub.CloseSession = SlowUnary()
|
||||
client = await GatewayClient.connect(
|
||||
ClientOptions(endpoint="fake", api_key="k", plaintext=True),
|
||||
stub=stub,
|
||||
)
|
||||
session = await client.open_session(client_session_name="pytest")
|
||||
|
||||
replies = await asyncio.gather(*(session.close() for _ in range(5)))
|
||||
|
||||
assert len(stub.close_session.requests) == 1
|
||||
assert all(r is real for r in replies)
|
||||
assert (await session.close()) is real
|
||||
|
||||
Reference in New Issue
Block a user