Skip to content

Commit f7aa615

Browse files
Add test-cases for client stream-stream
1 parent 159c30d commit f7aa615

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import asyncio
2+
from dataclasses import dataclass
3+
from typing import AsyncIterator
4+
5+
import pytest
6+
7+
import betterproto
8+
from betterproto.grpc.util.async_channel import AsyncChannel
9+
10+
11+
@dataclass
12+
class Message(betterproto.Message):
13+
body: str = betterproto.string_field(1)
14+
15+
16+
async def to_list(generator: AsyncIterator):
17+
lis = []
18+
async for value in generator:
19+
lis.append(value)
20+
return lis
21+
22+
23+
@pytest.fixture
24+
def expected_responses():
25+
return [Message("Hello world 1"), Message("Hello world 2"), Message("Done")]
26+
27+
28+
class ClientStub:
29+
async def connect(self, requests):
30+
await asyncio.sleep(0.1)
31+
async for request in requests:
32+
await asyncio.sleep(0.1)
33+
yield request
34+
await asyncio.sleep(0.1)
35+
yield Message("Done")
36+
37+
38+
@pytest.fixture
39+
def client():
40+
# channel = Channel(host='127.0.0.1', port=50051)
41+
# return ClientStub(channel)
42+
return ClientStub()
43+
44+
45+
@pytest.mark.asyncio
46+
async def test_from_list_close_automatically(client, expected_responses):
47+
requests = AsyncChannel(
48+
[Message(body="Hello world 1"), Message(body="Hello world 2")], close=True
49+
)
50+
51+
responses = client.connect(requests)
52+
53+
assert await to_list(responses) == expected_responses
54+
55+
56+
@pytest.mark.asyncio
57+
async def test_from_list_close_manually_immediately(client, expected_responses):
58+
requests = AsyncChannel(
59+
[Message(body="Hello world 1"), Message(body="Hello world 2")], close=False
60+
)
61+
62+
requests.close()
63+
64+
responses = client.connect(requests)
65+
66+
assert await to_list(responses) == expected_responses
67+
68+
69+
@pytest.mark.asyncio
70+
async def test_from_list_close_manually_after_connect(client, expected_responses):
71+
requests = AsyncChannel(
72+
[Message(body="Hello world 1"), Message(body="Hello world 2")], close=False
73+
)
74+
75+
responses = client.connect(requests)
76+
77+
requests.close()
78+
79+
assert await to_list(responses) == expected_responses
80+
81+
82+
@pytest.mark.asyncio
83+
async def test_send_from_before_connect_and_close_automatically(
84+
client, expected_responses
85+
):
86+
requests = AsyncChannel()
87+
88+
await requests.send_from(
89+
[Message(body="Hello world 1"), Message(body="Hello world 2")], close=True
90+
)
91+
92+
responses = client.connect(requests)
93+
94+
assert await to_list(responses) == expected_responses
95+
96+
97+
@pytest.mark.asyncio
98+
async def test_send_from_after_connect_and_close_automatically(
99+
client, expected_responses
100+
):
101+
requests = AsyncChannel()
102+
103+
responses = client.connect(requests)
104+
105+
await requests.send_from(
106+
[Message(body="Hello world 1"), Message(body="Hello world 2")], close=True
107+
)
108+
109+
assert await to_list(responses) == expected_responses
110+
111+
112+
@pytest.mark.asyncio
113+
async def test_send_from_close_manually_immediately(client, expected_responses):
114+
requests = AsyncChannel()
115+
116+
responses = client.connect(requests)
117+
118+
await requests.send_from(
119+
[Message(body="Hello world 1"), Message(body="Hello world 2")], close=False
120+
)
121+
122+
requests.close()
123+
124+
assert await to_list(responses) == expected_responses

0 commit comments

Comments
 (0)