test(state): make the state manager tests deterministic and say what they test - #278
Merged
Merged
Conversation
…they test - Replace test_consume_expired_state, which slept past a 1 s TTL and accepted either InvalidStateError or StateExpiredError, with a memory backend test that moves the backend clock past the TTL and expects InvalidStateError. The expires_at-passed case (StateExpiredError) is already covered by test_consume_state_past_wall_clock_expiry_removes_the_entry. - test_get_metadata_with_missing_expiry and test_validate_state_with_missing_expiry now store an entry without expires_at and expect None / False, instead of repeating the happy path. - Remove the unused memory_backend_for_state and redis_backend_for_state fixtures. - Drop try/finally BackendProxy resets that setup_default_backend already handles. Closes #186
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #186 (part of #123). Test-only change to
tests/state/test_manager.py.test_consume_expired_statereplaced.InvalidStateErrororStateExpiredError. Its docstring said Redis keeps the data after the TTL, which it doesn't.test_consume_state_after_backend_ttl_is_invalidmoves the memory backend's clock past the TTL, with no sleep, and expects exactlyInvalidStateError("Invalid or expired state").expires_athas passed, raisesStateExpiredError.test_consume_state_past_wall_clock_expiry_removes_the_entryalready covers it deterministically; the new docstring points there.test_get_metadata_with_missing_expiryandtest_validate_state_with_missing_expirynow test what their names say.expires_at.NoneandFalse, the documented "otherwise" result ofget_state_metadata/validate_state.expires_atis a requiredStateDatafield, so the entry fails to decode.expires_atand repeated the happy path.memory_backend_for_stateandredis_backend_for_statefixtures.try/finallyBackendProxyresets. The autousesetup_default_backend(intests/conftest.py) sets a freshMemoryBackendonBackendProxybefore every test. The other proxies are reset byreset_proxy_singletons.test_state_manager_accepts_explicit_backendnow uses thememory_backendfixture for cleanup.No library changes. The behaviour for a missing
expires_atmatches the docs, so no library bug was found.Verification
uv run pytestwithout live servers: 814 passed, 190 skipped. Before: 814 passed, 191 skipped; the removed Redis variant of the flaky test accounts for the difference. Runtime is about 24.6 s, down from 27.4 s.fastapi_cachex/state/: 100% before and after (statements and branches);manager.pyis 112/112 statements and 16/16 branches.get_and_deletereturning expired entries: fails the newtest_consume_state_after_backend_ttl_is_invalid, plus the existing memory backend test for that behaviour.StateData.expires_atgiven a default, so a missingexpires_atdecodes: fails both rewritten missing-expiry tests, plus the existingtest_consume_state_with_missing_expiry.--all-files: passed.uv lock --checkok.CHANGELOG
None; test-only change.