Skip to content

test(state): make the state manager tests deterministic and say what they test - #278

Merged
allen0099 merged 1 commit into
masterfrom
test/state-manager-cleanup-186
Sep 27, 2026
Merged

allen0099 merged 1 commit into
masterfrom
test/state-manager-cleanup-186

Conversation

@allen0099

Copy link
Copy Markdown
Owner

Summary

Closes #186 (part of #123). Test-only change to tests/state/test_manager.py.

  • test_consume_expired_state replaced.
    • The old test slept past a 1 s TTL and accepted either InvalidStateError or StateExpiredError. Its docstring said Redis keeps the data after the TTL, which it doesn't.
    • The new test_consume_state_after_backend_ttl_is_invalid moves the memory backend's clock past the TTL, with no sleep, and expects exactly InvalidStateError("Invalid or expired state").
    • The other case, where the entry still exists but expires_at has passed, raises StateExpiredError. test_consume_state_past_wall_clock_expiry_removes_the_entry already covers it deterministically; the new docstring points there.
  • test_get_metadata_with_missing_expiry and test_validate_state_with_missing_expiry now test what their names say.
    • They store an entry with no expires_at.
    • They expect None and False, the documented "otherwise" result of get_state_metadata / validate_state. expires_at is a required StateData field, so the entry fails to decode.
    • They also check that peeking leaves the entry in place.
    • Before, they stored a valid expires_at and repeated the happy path.
  • Removed the unused memory_backend_for_state and redis_backend_for_state fixtures.
  • Dropped the try/finally BackendProxy resets. The autouse setup_default_backend (in tests/conftest.py) sets a fresh MemoryBackend on BackendProxy before every test. The other proxies are reset by reset_proxy_singletons. test_state_manager_accepts_explicit_backend now uses the memory_backend fixture for cleanup.

No library changes. The behaviour for a missing expires_at matches the docs, so no library bug was found.

Verification

  • uv run pytest without 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.
  • Coverage of fastapi_cachex/state/: 100% before and after (statements and branches); manager.py is 112/112 statements and 16/16 branches.
  • Mutation checks (full suite each time):
    • Memory get_and_delete returning expired entries: fails the new test_consume_state_after_backend_ttl_is_invalid, plus the existing memory backend test for that behaviour.
    • StateData.expires_at given a default, so a missing expires_at decodes: fails both rewritten missing-expiry tests, plus the existing test_consume_state_with_missing_expiry.
  • pre-commit --all-files: passed. uv lock --check ok.

CHANGELOG

None; test-only change.

…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
@allen0099 allen0099 added this to the 0.3.8 milestone Sep 26, 2026
@allen0099 allen0099 added the enhancement New feature or request label Sep 26, 2026
@allen0099
allen0099 merged commit 833d10c into master Sep 27, 2026
10 checks passed
@allen0099
allen0099 deleted the test/state-manager-cleanup-186 branch September 27, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests: clean up the state manager tests

1 participant