test_memory_leaks.py 939 B

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import annotations
  2. import gc
  3. import pytest
  4. from prompt_toolkit.shortcuts.prompt import PromptSession
  5. def _count_prompt_session_instances() -> int:
  6. # Run full GC collection first.
  7. gc.collect()
  8. # Count number of remaining referenced `PromptSession` instances.
  9. objects = gc.get_objects()
  10. return len([obj for obj in objects if isinstance(obj, PromptSession)])
  11. # Fails in GitHub CI, probably due to GC differences.
  12. @pytest.mark.xfail(reason="Memory leak testing fails in GitHub CI.")
  13. def test_prompt_session_memory_leak() -> None:
  14. before_count = _count_prompt_session_instances()
  15. # Somehow in CI/CD, the before_count is > 0
  16. assert before_count == 0
  17. p = PromptSession()
  18. after_count = _count_prompt_session_instances()
  19. assert after_count == before_count + 1
  20. del p
  21. after_delete_count = _count_prompt_session_instances()
  22. assert after_delete_count == before_count