File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 3
3
from datetime import datetime
4
4
from pathlib import Path
5
5
from typing import Dict
6
+ from unittest .mock import Mock
6
7
7
8
import pytest
8
9
@@ -31,6 +32,37 @@ def job(path: str):
31
32
assert float (file .readline ()) - 2 == pytest .approx (start , abs = 0.3 )
32
33
33
34
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
+
34
66
def test_recurring_thread ():
35
67
def job (modifiable_arg : Dict ):
36
68
# Modify the variable, which should be shared with the main thread.
You can’t perform that action at this time.
0 commit comments