Skip to content

Commit 4c15c11

Browse files
committed
Test using a threadpool
The threadpool already has a dedicated thread to run scheduler tasks which is not the same as running in the main thread.
1 parent 8a1ed09 commit 4c15c11

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

tests/unit_tests/scheduler_test.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from mmpy_bot import schedule
11+
from mmpy_bot.threadpool import ThreadPool
1112

1213

1314
def test_once():
@@ -78,11 +79,21 @@ def job(modifiable_arg: Dict):
7879

7980
start = time.time()
8081
end = start + 3.5 # We want to wait just over 3 seconds
82+
83+
pool = ThreadPool(num_workers=10)
84+
85+
pool.start_scheduler_thread(trigger_period=1) # in seconds
86+
87+
# Start the pool thread
88+
pool.start()
89+
8190
while time.time() < end:
82-
# Launch job and wait one second
83-
schedule.run_pending()
91+
# Wait until we reach our 3+ second deadline
8492
time.sleep(1)
8593

94+
# Stop the pool and scheduler loop
95+
pool.stop()
96+
8697
# Stop all scheduled jobs
8798
schedule.clear()
8899
# Nothing should happen from this point, even if we sleep another while
@@ -116,11 +127,20 @@ def job(path: str, modifiable_arg: Dict):
116127

117128
start = time.time()
118129
end = start + 3.5 # We want to wait just over 3 seconds
130+
pool = ThreadPool(num_workers=10)
131+
132+
pool.start_scheduler_thread(trigger_period=1) # in seconds
133+
134+
# Start the pool thread
135+
pool.start()
136+
119137
while time.time() < end:
120-
# Launch job and wait one second
121-
schedule.run_pending()
138+
# Wait until we reach our 3+ second deadline
122139
time.sleep(1)
123140

141+
# Stop the pool and scheduler loop
142+
pool.stop()
143+
124144
# Stop all scheduled jobs
125145
schedule.clear()
126146
# Nothing should happen from this point, even if we sleep another while

0 commit comments

Comments
 (0)