Esempio n. 1
0
async def test_async_gen_for_stream_stream_request():
    some_things = ["cake", "cricket", "coral reef"]
    more_things = [
        "ball", "that", "56kmodem", "liberal humanism", "cheesesticks"
    ]
    expected_things = (*some_things, *more_things)

    async with ChannelFor([ThingService()]) as channel:
        client = ThingServiceClient(channel)
        # Use an AsyncChannel to decouple sending and recieving, it'll send some_things
        # immediately and we'll use it to send more_things later, after recieving some
        # results
        request_chan = AsyncChannel()
        send_initial_requests = asyncio.ensure_future(
            request_chan.send_from(
                GetThingRequest(name) for name in some_things))
        response_index = 0
        async for response in client.get_different_things(request_chan):
            assert response.name == expected_things[response_index]
            assert response.version == response_index + 1
            response_index += 1
            if more_things:
                # Send some more requests as we receive responses to be sure coordination of
                # send/receive events doesn't matter
                await request_chan.send(GetThingRequest(more_things.pop(0)))
            elif not send_initial_requests.done():
                # Make sure the sending task it completed
                await send_initial_requests
            else:
                # No more things to send make sure channel is closed
                request_chan.close()
        assert response_index == len(
            expected_things), "Didn't receive all expected responses"
async def test_send_individually_and_close_after_connect(
        client, expected_responses):
    requests = AsyncChannel()
    await requests.send(Message(body="Hello world 1"))
    await requests.send(Message(body="Hello world 2"))
    responses = client.connect(requests)
    requests.close()

    assert await to_list(responses) == expected_responses
async def test_send_from_close_manually_immediately(client,
                                                    expected_responses):
    requests = AsyncChannel()
    responses = client.connect(requests)
    await requests.send_from(
        [Message(body="Hello world 1"),
         Message(body="Hello world 2")],
        close=False)
    requests.close()

    assert await to_list(responses) == expected_responses
async def test_send_from_after_connect_and_close_automatically(
        client, expected_responses):
    requests = AsyncChannel()
    responses = client.connect(requests)
    await requests.send_from(
        [Message(body="Hello world 1"),
         Message(body="Hello world 2")],
        close=True)

    assert await to_list(responses) == expected_responses