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

Class _FakeStream

tests/lib/tools/test_session_runner.py:148–187  ·  view source on GitHub ↗

Stand-in for the AsyncStream returned by `events.stream()`. Yields scripted events in order; once exhausted, blocks forever (the real stream stays open until a network event closes it). If `raise_after` is set, raises `raise_with` after producing that many events — used to exercise

Source from the content-addressed store, hash-verified

146
147
148class _FakeStream:
149 """Stand-in for the AsyncStream returned by `events.stream()`.
150
151 Yields scripted events in order; once exhausted, blocks forever (the real
152 stream stays open until a network event closes it). If `raise_after` is
153 set, raises `raise_with` after producing that many events — used to
154 exercise the reconnect-with-backoff path.
155 """
156
157 def __init__(
158 self,
159 events: list[_StubEvent],
160 *,
161 raise_after: int | None = None,
162 raise_with: BaseException | None = None,
163 ) -> None:
164 self._events = events
165 self._raise_after = raise_after
166 self._raise_with = raise_with
167
168 async def __aenter__(self) -> _FakeStream:
169 return self
170
171 async def __aexit__(self, *exc: object) -> None:
172 return None
173
174 def __aiter__(self) -> Any:
175 return self._gen()
176
177 async def _gen(self) -> Any:
178 for i, ev in enumerate(self._events):
179 yield ev
180 # Yield control so the dispatch task can pick up the event we just
181 # produced before we run on to the next one.
182 await asyncio.sleep(0)
183 if self._raise_after is not None and i + 1 == self._raise_after:
184 assert self._raise_with is not None
185 raise self._raise_with
186 # Keep the connection "open" until cancelled.
187 await asyncio.Event().wait()
188
189
190class FakeAsyncEvents:

Calls

no outgoing calls