Skip to main content

test_concurrently.py

1"""
2Tests for `concurrently`.
3"""
5import random
6import time
8from concurrently import concurrently
11def double(x: int) -> int:
12 """
13 Double a number, but with a random delay.
14 """
15 time.sleep(random.random() / 100)
16 return x * 2
19def test_handles_iterator() -> None:
20 """
21 Check the input is processed correctly when the input is an iterator.
22 """
23 result = set(concurrently(handler=double, inputs=range(10)))
25 assert result == {
26 (0, 0),
27 (1, 2),
28 (2, 4),
29 (3, 6),
30 (4, 8),
31 (5, 10),
32 (6, 12),
33 (7, 14),
34 (8, 16),
35 (9, 18),
36 }
39def test_handles_list() -> None:
40 """
41 Check the input is processed correctly when the input is a list.
42 """
43 result = set(concurrently(handler=double, inputs=[1, 3, 5, 7, 9, 11, 13]))
45 assert result == {(1, 2), (3, 6), (5, 10), (7, 14), (9, 18), (11, 22), (13, 26)}