Skip to content

Commit 8a1ed09

Browse files
committed
Add tests to capture abnormal consecutive execution of scheduled tasks
1 parent 9fcdcbd commit 8a1ed09

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/unit_tests/scheduler_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime
44
from pathlib import Path
55
from typing import Dict
6+
from unittest.mock import Mock
67

78
import pytest
89

@@ -31,6 +32,37 @@ def job(path: str):
3132
assert float(file.readline()) - 2 == pytest.approx(start, abs=0.3)
3233

3334

35+
def test_once_single_call():
36+
mock = Mock()
37+
mock.side_effect = lambda: time.sleep(0.2)
38+
39+
schedule.once().do(mock)
40+
41+
for _ in range(10):
42+
schedule.run_pending()
43+
time.sleep(0.05)
44+
45+
mock.assert_called_once()
46+
47+
48+
def test_recurring_single_call():
49+
mock = Mock()
50+
mock.side_effect = lambda: time.sleep(0.2)
51+
52+
schedule.every(2).seconds.do(mock)
53+
54+
# Wait 2 seconds so we can run the task once
55+
time.sleep(2)
56+
57+
# This loop corresponds to 0.1 seconds of total time and while there will
58+
# be 10 calls to run_pending() the mock function should only run once
59+
for _ in range(10):
60+
schedule.run_pending()
61+
time.sleep(0.01)
62+
63+
mock.assert_called_once()
64+
65+
3466
def test_recurring_thread():
3567
def job(modifiable_arg: Dict):
3668
# Modify the variable, which should be shared with the main thread.

0 commit comments

Comments
 (0)