tests_concurrent.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Tests for `tqdm.contrib.concurrent`.
  3. """
  4. from pytest import warns
  5. from tqdm.contrib.concurrent import process_map, thread_map
  6. from .tests_tqdm import StringIO, TqdmWarning, closing, importorskip, mark, skip
  7. def incr(x):
  8. """Dummy function"""
  9. return x + 1
  10. def test_thread_map():
  11. """Test contrib.concurrent.thread_map"""
  12. with closing(StringIO()) as our_file:
  13. a = range(9)
  14. b = [i + 1 for i in a]
  15. try:
  16. assert thread_map(lambda x: x + 1, a, file=our_file) == b
  17. except ImportError as err:
  18. skip(str(err))
  19. assert thread_map(incr, a, file=our_file) == b
  20. def test_process_map():
  21. """Test contrib.concurrent.process_map"""
  22. with closing(StringIO()) as our_file:
  23. a = range(9)
  24. b = [i + 1 for i in a]
  25. try:
  26. assert process_map(incr, a, file=our_file) == b
  27. except ImportError as err:
  28. skip(str(err))
  29. @mark.parametrize("iterables,should_warn", [([], False), (['x'], False), ([()], False),
  30. (['x', ()], False), (['x' * 1001], True),
  31. (['x' * 100, ('x',) * 1001], True)])
  32. def test_chunksize_warning(iterables, should_warn):
  33. """Test contrib.concurrent.process_map chunksize warnings"""
  34. patch = importorskip('unittest.mock').patch
  35. with patch('tqdm.contrib.concurrent._executor_map'):
  36. if should_warn:
  37. warns(TqdmWarning, process_map, incr, *iterables)
  38. else:
  39. process_map(incr, *iterables)