MCPcopy Create free account
hub / github.com/dot-agent/nextpy / test_async_mode_exceptions

Function test_async_mode_exceptions

nextpy/ai/tests/engine/test_program.py:144–191  ·  view source on GitHub ↗

Ensures that exceptions in async_mode=True don't hang the program and are re-raised back to the caller.

()

Source from the content-addressed store, hash-verified

142
143
144def test_async_mode_exceptions():
145 """Ensures that exceptions in async_mode=True don't hang the program and are
146 re-raised back to the caller.
147 """
148 import asyncio
149
150 loop = asyncio.new_event_loop()
151
152 engine.llm = get_llm("openai:gpt-3.5-turbo")
153
154 async def call_async():
155 program = engine(
156 """
157{{#system~}}
158You are a helpful assistant.
159{{~/system}}
160
161{{#user~}}
162What is your name?
163{{~/user}}
164
165{{#assistant~}}
166Hello my name is {{gen 'name' temperature=0 max_tokens=5}}.
167{{~/assistant}}
168""",
169 async_mode=True,
170 )
171
172 return await program()
173
174 task = loop.create_task(call_async())
175 completed_tasks, _ = loop.run_until_complete(asyncio.wait([task], timeout=5.0))
176
177 try:
178 assert len(completed_tasks) == 1, "The task did not complete before timeout"
179 finally:
180 task.cancel()
181 loop.run_until_complete(
182 asyncio.sleep(0)
183 ) # give the loop a chance to cancel the tasks
184
185 completed_task = list(completed_tasks)[0]
186
187 assert isinstance(
188 completed_task.exception(), AssertionError
189 ), "Expect the exception to be propagated"
190
191 loop.close()

Callers

nothing calls this directly

Calls 4

get_llmFunction · 0.85
call_asyncFunction · 0.85
lenFunction · 0.85
closeMethod · 0.80

Tested by

no test coverage detected