def __enter__(self) -> "TestClient": with contextlib.ExitStack() as stack: self.portal = portal = stack.enter_context( anyio.start_blocking_portal(**self.async_backend) ) @stack.callback def reset_portal() -> None: self.portal = None self.stream_send = StapledObjectStream( *anyio.create_memory_object_stream(math.inf) ) self.stream_receive = StapledObjectStream( *anyio.create_memory_object_stream(math.inf) ) self.task = portal.start_task_soon(self.lifespan) portal.call(self.wait_startup) @stack.callback def wait_shutdown() -> None: portal.call(self.wait_shutdown) self.exit_stack = stack.pop_all() return self
def __enter__(self) -> "TestClient": self.exit_stack = contextlib.ExitStack() self.portal = self.exit_stack.enter_context( anyio.start_blocking_portal(**self.async_backend) ) self.stream_send = StapledObjectStream( *anyio.create_memory_object_stream(math.inf) ) self.stream_receive = StapledObjectStream( *anyio.create_memory_object_stream(math.inf) ) try: self.task = self.portal.start_task_soon(self.lifespan) self.portal.call(self.wait_startup) except Exception: self.exit_stack.close() raise return self
async def test_bidirectional_stream(): send_stream, receive_stream = create_memory_object_stream(1) stapled_stream = StapledObjectStream(send_stream, receive_stream) text_stream = TextStream(stapled_stream) await text_stream.send('åäö') assert await receive_stream.receive() == b'\xc3\xa5\xc3\xa4\xc3\xb6' await send_stream.send(b'\xc3\xa6\xc3\xb8') assert await text_stream.receive() == 'æø'
async def test_bidirectional_stream() -> None: send_stream, receive_stream = create_memory_object_stream(1) stapled_stream = StapledObjectStream(send_stream, receive_stream) text_stream = TextStream(stapled_stream) await text_stream.send("åäö") assert await receive_stream.receive() == b"\xc3\xa5\xc3\xa4\xc3\xb6" await send_stream.send(b"\xc3\xa6\xc3\xb8") assert await text_stream.receive() == "æø" assert text_stream.extra_attributes == {}
def stapled( self, receive_stream: DummyObjectReceiveStream[str], send_stream: DummyObjectSendStream[str], ) -> StapledObjectStream[str]: return StapledObjectStream(send_stream, receive_stream)
def stapled(self): receive = self.DummyObjectReceiveStream(['hello', 'world']) send = self.DummyObjectSendStream() return StapledObjectStream(send, receive)