| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Unit tests for StructuredOutputRequest grammar-future polling.""" |
| 4 | |
| 5 | from concurrent.futures import Future |
| 6 | |
| 7 | from vllm.sampling_params import StructuredOutputsParams |
| 8 | from vllm.v1.structured_output.request import StructuredOutputRequest |
| 9 | |
| 10 | |
| 11 | def _request_with_future(fut: Future) -> StructuredOutputRequest: |
| 12 | req = StructuredOutputRequest(params=StructuredOutputsParams(regex="a+")) |
| 13 | req.grammar = fut |
| 14 | return req |
| 15 | |
| 16 | |
| 17 | def test_pending_grammar_future_is_not_ready(): |
| 18 | req = _request_with_future(Future()) |
| 19 | assert not req.is_grammar_ready |
| 20 | assert req.grammar is None |
| 21 | |
| 22 | |
| 23 | def test_grammar_compile_raising_timeout_error_is_a_failure(): |
| 24 | """A compile that raised TimeoutError must be treated as a failed compile, |
| 25 | not as still-compiling (#53130).""" |
| 26 | fut: Future = Future() |
| 27 | fut.set_exception(TimeoutError("compiler timed out")) |
| 28 | req = _request_with_future(fut) |
| 29 | assert req.is_grammar_ready |
| 30 | assert isinstance(req.grammar, TimeoutError) |