-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_waypoint_sse.py
220 lines (180 loc) · 6.11 KB
/
test_waypoint_sse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import asyncio
from unittest.mock import ANY, AsyncMock, patch
import pytest
from fastapi import BackgroundTasks, Request
from sse_starlette import EventSourceResponse
from shared.constants import DISCONNECT_CHECK_PERIOD
from shared.models.webhook_events.payloads import CloudApiWebhookEventGeneric
from waypoint.routers.sse import (
check_disconnect,
nats_event_stream_generator,
sse_wait_for_event_with_field_and_state,
)
from waypoint.services.nats_service import NatsEventsProcessor
wallet_id = "wallet123"
topic = "some_topic"
field = "some_field"
field_id = "some_field_id"
desired_state = "some_state"
group_id = "some_group"
dummy_cloudapi_event = CloudApiWebhookEventGeneric(
wallet_id=wallet_id,
group_id=group_id,
topic=topic,
origin="xyz",
payload={
"dummy": "data",
field: "some_other_field_id",
"state": "some_other_state",
},
)
expected_cloudapi_event = CloudApiWebhookEventGeneric(
wallet_id=wallet_id,
topic=topic,
origin="xyz",
group_id=group_id,
payload={"dummy": "data", field: field_id, "state": desired_state},
)
@pytest.fixture
def nats_processor_mock():
mock = AsyncMock(spec=NatsEventsProcessor)
return mock
@pytest.fixture
def request_mock():
mock_request = AsyncMock(spec=Request)
mock_request.is_disconnected.return_value = False
return mock_request
@pytest.fixture
def async_generator_mock():
async def _mock_gen(*args):
for value in args[0]:
yield value
return _mock_gen
@pytest.mark.anyio
async def test_check_disconnection():
request = AsyncMock(spec=Request)
request.is_disconnected.return_value = True
stop_event = asyncio.Event()
task = asyncio.create_task(check_disconnect(request, stop_event))
await asyncio.sleep(DISCONNECT_CHECK_PERIOD * 1.1)
assert stop_event.is_set()
assert task.done() is True
@pytest.mark.anyio
async def test_sse_event_stream_generator_wallet_id_topic_field_desired_state(
nats_processor_mock, request_mock # pylint: disable=redefined-outer-name
):
async def mock_event_generator():
yield expected_cloudapi_event
nats_processor_mock.process_events.return_value.__aenter__.return_value = (
mock_event_generator()
)
events = []
async for event in nats_event_stream_generator(
request=request_mock,
background_tasks=BackgroundTasks(),
wallet_id=wallet_id,
topic=topic,
field=field,
field_id=field_id,
desired_state=desired_state,
group_id=group_id,
nats_processor=nats_processor_mock,
look_back=300,
):
events.append(event)
assert len(events) == 1
assert events[0] == expected_cloudapi_event.model_dump_json()
@pytest.mark.anyio
async def test_sse_event_stream_generator_disconnects(
nats_processor_mock, # pylint: disable=redefined-outer-name
):
request = AsyncMock(spec=Request)
request.is_disconnected.return_value = True
async def mock_event_generator():
yield dummy_cloudapi_event
yield expected_cloudapi_event
nats_processor_mock.process_events.return_value.__aenter__.return_value = (
mock_event_generator()
)
async for _ in nats_event_stream_generator(
request=request,
background_tasks=BackgroundTasks(),
wallet_id=wallet_id,
topic=topic,
field=field,
field_id=field_id,
desired_state=desired_state,
group_id=group_id,
look_back=300,
nats_processor=nats_processor_mock,
):
pass
assert request.is_disconnected.called
nats_processor_mock.process_events.assert_called_once()
@pytest.mark.anyio
async def test_nats_event_stream_generator_cancelled_error_handling(
nats_processor_mock, # pylint: disable=redefined-outer-name
request_mock, # pylint: disable=redefined-outer-name
):
background_tasks = BackgroundTasks()
async def mock_event_generator():
raise asyncio.CancelledError
yield # Make this function an asynchronous generator
nats_processor_mock.process_events.return_value.__aenter__.return_value = (
mock_event_generator()
)
generator = nats_event_stream_generator(
request=request_mock,
background_tasks=background_tasks,
wallet_id="wallet123",
topic="some_topic",
field="some_field",
field_id="some_field_id",
desired_state="some_state",
group_id="some_group",
look_back=300,
nats_processor=nats_processor_mock,
)
with pytest.raises(asyncio.CancelledError):
async for _ in generator:
pass
nats_processor_mock.process_events.assert_called_once()
request_mock.is_disconnected.assert_not_called()
@pytest.mark.anyio
async def test_sse_event_stream(
async_generator_mock, # pylint: disable=redefined-outer-name
nats_processor_mock, # pylint: disable=redefined-outer-name
request_mock, # pylint: disable=redefined-outer-name
):
with patch(
"waypoint.routers.sse.nats_event_stream_generator"
) as nats_event_stream_generator_mock:
nats_event_stream_generator_mock.return_value = async_generator_mock(
[expected_cloudapi_event]
)
event_stream = await sse_wait_for_event_with_field_and_state(
request=request_mock,
background_tasks=BackgroundTasks(),
wallet_id=wallet_id,
topic=topic,
field=field,
field_id=field_id,
desired_state=desired_state,
group_id=group_id,
look_back=300,
nats_processor=nats_processor_mock,
)
assert isinstance(event_stream, EventSourceResponse)
assert event_stream.status_code == 200
nats_event_stream_generator_mock.assert_called_once_with(
request=request_mock,
background_tasks=ANY,
wallet_id=wallet_id,
topic=topic,
field=field,
field_id=field_id,
desired_state=desired_state,
group_id=group_id,
look_back=300,
nats_processor=nats_processor_mock,
)