test_misc.py 970 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Test misc utilities."""
  2. import asyncio
  3. import time
  4. import pytest
  5. from reflex.utils.misc import run_in_thread
  6. async def test_run_in_thread():
  7. """Test that run_in_thread runs a function in a separate thread."""
  8. def simple_function():
  9. return 42
  10. result = await run_in_thread(simple_function)
  11. assert result == 42
  12. def slow_function():
  13. time.sleep(0.1)
  14. return "completed"
  15. result = await run_in_thread(slow_function, timeout=0.5)
  16. assert result == "completed"
  17. async def async_function():
  18. return 42
  19. with pytest.raises(ValueError):
  20. await run_in_thread(async_function)
  21. async def test_run_in_thread_timeout():
  22. """Test that run_in_thread raises TimeoutError when timeout is exceeded."""
  23. def very_slow_function():
  24. time.sleep(0.5)
  25. return "should not reach here"
  26. with pytest.raises(asyncio.TimeoutError):
  27. await run_in_thread(very_slow_function, timeout=0.1)