aboutsummaryrefslogtreecommitdiff
path: root/test/misc/test_utils.py
blob: 660efa50ab6d27dbb7927e123722cc88d6d558b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import time
from pyecsca.misc.utils import TaskExecutor


def run(a, b):
    return a + b


def wait(a, b):
    time.sleep(1)
    return a + b


def test_executor():
    with TaskExecutor(max_workers=2) as pool:
        for i in range(10):
            pool.submit_task(i,
                             run,
                             i, 5)
        for i, future in pool.as_completed():
            res = future.result()
            assert res == i + 5


def test_executor_no_wait():
    with TaskExecutor(max_workers=2) as pool:
        for i in range(2):
            pool.submit_task(i,
                             wait,
                             i, 5)
        futures = list(pool.as_completed(wait=False))
        assert len(futures) == 0