MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / FakeAsyncEvents

Class FakeAsyncEvents

tests/lib/tools/test_session_runner.py:190–250  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

188
189
190class FakeAsyncEvents:
191 def __init__(
192 self,
193 *,
194 streams: list[_FakeStream | BaseException] | None = None,
195 stream_events: list[_StubEvent] | None = None,
196 list_events: list[_StubEvent] | None = None,
197 list_events_per_call: list[list[_StubEvent | BaseException]] | None = None,
198 list_raises: BaseException | None = None,
199 send_failures: Sequence[BaseException | None] | None = None,
200 ) -> None:
201 if streams is not None:
202 self._streams: list[_FakeStream | BaseException] = list(streams)
203 elif stream_events is not None:
204 self._streams = [_FakeStream(stream_events)]
205 else:
206 self._streams = [_FakeStream([])]
207 self._list_events = list(list_events or [])
208 # When set, each `list()` call consumes the next entry (falling back
209 # to `list_events` once exhausted) so reconnect tests can script a
210 # different history per reconcile pass. An exception in an entry is raised at that point.
211 self._list_events_per_call = [list(evs) for evs in (list_events_per_call or [])]
212 self._list_raises = list_raises
213 self._send_failures: list[BaseException | None] = list(send_failures or [])
214 self.send_calls: list[dict[str, Any]] = []
215 self.stream_calls: int = 0
216 self.stream_headers: list[Any] = []
217 self.list_headers: list[Any] = []
218
219 async def stream(self, _session_id: str, *, extra_headers: Any = None) -> _FakeStream:
220 self.stream_calls += 1
221 self.stream_headers.append(extra_headers)
222 if not self._streams:
223 return _FakeStream([]) # block forever; mirrors a fresh connection
224 nxt = self._streams.pop(0)
225 if isinstance(nxt, BaseException):
226 raise nxt
227 return nxt
228
229 def list(self, _session_id: str, *, limit: int = 1000, extra_headers: Any = None) -> Any: # noqa: ARG002
230 list_raises = self._list_raises
231 self.list_headers.append(extra_headers)
232 list_events = self._list_events_per_call.pop(0) if self._list_events_per_call else self._list_events
233
234 async def _gen() -> Any:
235 for ev in list_events:
236 if isinstance(ev, BaseException):
237 raise ev
238 yield ev
239 if list_raises is not None:
240 raise list_raises
241
242 return _gen()
243
244 async def send(self, session_id: str, *, events: list[Any], extra_headers: Any = None) -> None:
245 idx = len(self.send_calls)
246 self.send_calls.append({"session_id": session_id, "events": events, "extra_headers": extra_headers})
247 if idx < len(self._send_failures):

Calls

no outgoing calls