2020-01-19 15:46:44 +00:00
|
|
|
"""
|
2020-01-19 16:47:11 +00:00
|
|
|
Tests for `tqdm.contrib.concurrent`.
|
2020-01-19 15:46:44 +00:00
|
|
|
"""
|
2021-01-03 22:02:40 +00:00
|
|
|
from pytest import mark, warns
|
2020-12-18 22:55:41 +00:00
|
|
|
|
2020-01-19 15:46:44 +00:00
|
|
|
from tqdm.contrib.concurrent import thread_map, process_map
|
2021-01-03 22:02:40 +00:00
|
|
|
from .tests_tqdm import importorskip, skip, StringIO, closing, TqdmWarning
|
2020-01-19 15:46:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def incr(x):
|
|
|
|
"""Dummy function"""
|
|
|
|
return x + 1
|
|
|
|
|
2020-01-19 16:40:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
def test_thread_map():
|
|
|
|
"""Test contrib.concurrent.thread_map"""
|
|
|
|
with closing(StringIO()) as our_file:
|
|
|
|
a = range(9)
|
|
|
|
b = [i + 1 for i in a]
|
2020-01-19 15:46:44 +00:00
|
|
|
try:
|
2020-10-24 18:36:45 +00:00
|
|
|
assert thread_map(lambda x: x + 1, a, file=our_file) == b
|
|
|
|
except ImportError as err:
|
|
|
|
skip(str(err))
|
|
|
|
assert thread_map(incr, a, file=our_file) == b
|
|
|
|
|
|
|
|
|
|
|
|
def test_process_map():
|
|
|
|
"""Test contrib.concurrent.process_map"""
|
|
|
|
with closing(StringIO()) as our_file:
|
|
|
|
a = range(9)
|
|
|
|
b = [i + 1 for i in a]
|
|
|
|
try:
|
|
|
|
assert process_map(incr, a, file=our_file) == b
|
|
|
|
except ImportError as err:
|
|
|
|
skip(str(err))
|
|
|
|
|
|
|
|
|
2020-12-18 22:55:41 +00:00
|
|
|
@mark.parametrize("iterables,should_warn", [([], False), (['x'], False), ([()], False),
|
|
|
|
(['x', ()], False), (['x' * 1001], True),
|
|
|
|
(['x' * 100, ('x',) * 1001], True)])
|
|
|
|
def test_chunksize_warning(iterables, should_warn):
|
2020-10-24 18:36:45 +00:00
|
|
|
"""Test contrib.concurrent.process_map chunksize warnings"""
|
2020-10-24 18:37:35 +00:00
|
|
|
patch = importorskip("unittest.mock").patch
|
2020-12-18 22:55:41 +00:00
|
|
|
with patch('tqdm.contrib.concurrent._executor_map'):
|
2021-01-03 22:02:40 +00:00
|
|
|
if should_warn:
|
|
|
|
warns(TqdmWarning, process_map, incr, *iterables)
|
|
|
|
else:
|
2020-12-18 22:55:41 +00:00
|
|
|
process_map(incr, *iterables)
|