-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_throttle.py
More file actions
95 lines (68 loc) · 2.71 KB
/
test_throttle.py
File metadata and controls
95 lines (68 loc) · 2.71 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from threading import Lock
import pytest
from cognite.extractorutils.unstable.core.throttle import TaskThrottle
def test_throttle_initialization() -> None:
"""Test throttle initialization with valid and invalid parameters."""
throttle = TaskThrottle(max_concurrent=5)
assert throttle.max_concurrent == 5
with pytest.raises(ValueError, match="max_concurrent must be at least 1"):
TaskThrottle(max_concurrent=0)
with pytest.raises(ValueError, match="max_concurrent must be at least 1"):
TaskThrottle(max_concurrent=-1)
def test_throttle_concurrency_limits() -> None:
max_concurrent = 3
throttle = TaskThrottle(max_concurrent=max_concurrent)
concurrent_count = 0
max_observed = 0
lock = Lock()
def task(task_id: int) -> int:
nonlocal concurrent_count, max_observed
with throttle.lease():
with lock:
concurrent_count += 1
max_observed = max(max_observed, concurrent_count)
time.sleep(0.1)
with lock:
concurrent_count -= 1
return task_id
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(task, i) for i in range(10)]
results = [f.result() for f in as_completed(futures)]
assert len(results) == 10
assert max_observed <= max_concurrent
def test_throttle_serial_execution() -> None:
lock = Lock()
throttle_serial = TaskThrottle(max_concurrent=1)
execution_order = []
def serial_task(task_id: int) -> None:
with throttle_serial.lease():
with lock:
execution_order.append(task_id)
time.sleep(0.05)
with lock:
execution_order.append(task_id)
with ThreadPoolExecutor(max_workers=3) as executor:
futures = [executor.submit(serial_task, i) for i in range(3)]
for f in as_completed(futures):
f.result()
for i in range(0, len(execution_order) - 1, 2):
task_id = execution_order[i]
assert execution_order[i + 1] == task_id
def test_throttle_high_concurrency() -> None:
lock = Lock()
throttle_high = TaskThrottle(max_concurrent=50)
completed = []
def fast_task(task_id: int) -> int:
with throttle_high.lease():
time.sleep(0.01)
with lock:
completed.append(task_id)
return task_id
num_tasks = 100
with ThreadPoolExecutor(max_workers=num_tasks) as executor:
futures = [executor.submit(fast_task, i) for i in range(num_tasks)]
for f in as_completed(futures):
f.result()
assert len(completed) == num_tasks