-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_task_dispatcher.py
More file actions
205 lines (160 loc) · 5.7 KB
/
test_task_dispatcher.py
File metadata and controls
205 lines (160 loc) · 5.7 KB
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
"""Tests for DefaultMessageDispatcher — request/notification routing."""
from __future__ import annotations
import asyncio
from collections.abc import AsyncGenerator
import pytest
import pytest_asyncio
from acp.task import RpcTask, RpcTaskKind
from acp.task.dispatcher import DefaultMessageDispatcher
from acp.task.queue import InMemoryMessageQueue
from acp.task.state import InMemoryMessageStateStore
from acp.task.supervisor import TaskSupervisor
@pytest_asyncio.fixture
async def supervisor() -> AsyncGenerator[TaskSupervisor, None]:
sup = TaskSupervisor(source="test")
sup.add_error_handler(lambda _t, _e: None)
yield sup
await sup.shutdown()
@pytest.fixture
def store() -> InMemoryMessageStateStore:
return InMemoryMessageStateStore()
@pytest_asyncio.fixture
async def queue() -> AsyncGenerator[InMemoryMessageQueue, None]:
q = InMemoryMessageQueue()
yield q
await q.close()
@pytest.mark.asyncio
async def test_dispatch_request(
supervisor: TaskSupervisor,
store: InMemoryMessageStateStore,
queue: InMemoryMessageQueue,
) -> None:
"""Dispatcher should route REQUEST tasks to the request runner."""
results: list[dict] = []
async def request_runner(msg: dict) -> dict:
results.append(msg)
return {"ok": True}
async def notification_runner(msg: dict) -> None:
pass
dispatcher = DefaultMessageDispatcher(
queue=queue,
supervisor=supervisor,
store=store,
request_runner=request_runner,
notification_runner=notification_runner,
)
dispatcher.start()
await queue.publish(RpcTask(kind=RpcTaskKind.REQUEST, message={"method": "test/req", "params": {}}))
await asyncio.sleep(0.1)
await queue.close()
await dispatcher.stop()
await supervisor.shutdown()
assert len(results) == 1
assert results[0]["method"] == "test/req"
@pytest.mark.asyncio
async def test_dispatch_notification(
supervisor: TaskSupervisor,
store: InMemoryMessageStateStore,
queue: InMemoryMessageQueue,
) -> None:
"""Dispatcher should route NOTIFICATION tasks to the notification runner."""
notifications: list[dict] = []
async def request_runner(msg: dict) -> dict:
return {}
async def notification_runner(msg: dict) -> None:
notifications.append(msg)
dispatcher = DefaultMessageDispatcher(
queue=queue,
supervisor=supervisor,
store=store,
request_runner=request_runner,
notification_runner=notification_runner,
)
dispatcher.start()
await queue.publish(RpcTask(kind=RpcTaskKind.NOTIFICATION, message={"method": "session/update"}))
await asyncio.sleep(0.1)
await queue.close()
await dispatcher.stop()
await supervisor.shutdown()
assert len(notifications) == 1
assert notifications[0]["method"] == "session/update"
@pytest.mark.asyncio
async def test_start_twice_raises(
supervisor: TaskSupervisor,
store: InMemoryMessageStateStore,
queue: InMemoryMessageQueue,
) -> None:
"""Starting the dispatcher twice should raise."""
async def noop(msg: dict) -> None:
pass
dispatcher = DefaultMessageDispatcher(
queue=queue,
supervisor=supervisor,
store=store,
request_runner=noop,
notification_runner=noop,
)
dispatcher.start()
with pytest.raises(RuntimeError, match="already started"):
dispatcher.start()
await queue.close()
await dispatcher.stop()
await supervisor.shutdown()
@pytest.mark.asyncio
async def test_failed_request_updates_store(
supervisor: TaskSupervisor,
store: InMemoryMessageStateStore,
queue: InMemoryMessageQueue,
) -> None:
"""When the request runner raises, the store should record the failure."""
async def failing_runner(msg: dict) -> dict:
raise ValueError("handler error")
async def notification_runner(msg: dict) -> None:
pass
dispatcher = DefaultMessageDispatcher(
queue=queue,
supervisor=supervisor,
store=store,
request_runner=failing_runner,
notification_runner=notification_runner,
)
dispatcher.start()
await queue.publish(RpcTask(kind=RpcTaskKind.REQUEST, message={"method": "test/fail", "params": None}))
await asyncio.sleep(0.15)
await queue.close()
await dispatcher.stop()
await supervisor.shutdown()
# NOTE: Accessing private state because InMemoryMessageStateStore has no
# public API to query incoming records.
assert len(store._incoming) == 1
assert store._incoming[0].status == "failed"
assert isinstance(store._incoming[0].error, ValueError)
@pytest.mark.asyncio
async def test_multiple_tasks_dispatched(
supervisor: TaskSupervisor,
store: InMemoryMessageStateStore,
queue: InMemoryMessageQueue,
) -> None:
"""Multiple tasks should all be dispatched and processed."""
processed: list[str] = []
async def request_runner(msg: dict) -> dict:
processed.append(msg["method"])
return {}
async def notification_runner(msg: dict) -> None:
processed.append(msg["method"])
dispatcher = DefaultMessageDispatcher(
queue=queue,
supervisor=supervisor,
store=store,
request_runner=request_runner,
notification_runner=notification_runner,
)
dispatcher.start()
await queue.publish(RpcTask(kind=RpcTaskKind.REQUEST, message={"method": "r1"}))
await queue.publish(RpcTask(kind=RpcTaskKind.NOTIFICATION, message={"method": "n1"}))
await queue.publish(RpcTask(kind=RpcTaskKind.REQUEST, message={"method": "r2"}))
await asyncio.sleep(0.15)
await queue.close()
await dispatcher.stop()
await supervisor.shutdown()
assert sorted(processed) == ["n1", "r1", "r2"]