Ensures that exceptions in async_mode=True don't hang the program and are re-raised back to the caller.
()
| 142 | |
| 143 | |
| 144 | def 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~}} |
| 158 | You are a helpful assistant. |
| 159 | {{~/system}} |
| 160 | |
| 161 | {{#user~}} |
| 162 | What is your name? |
| 163 | {{~/user}} |
| 164 | |
| 165 | {{#assistant~}} |
| 166 | Hello 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() |
nothing calls this directly
no test coverage detected