forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_repeating_task.py
More file actions
58 lines (53 loc) · 1.44 KB
/
Copy pathtest_repeating_task.py
File metadata and controls
58 lines (53 loc) · 1.44 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
from ldclient.impl.repeating_task import RepeatingTask
from queue import Empty, Queue
from threading import Event
import time
def test_task_does_not_start_when_created():
signal = Event()
task = RepeatingTask(0.01, 0, lambda: signal.set())
try:
signal_was_set = signal.wait(0.1)
assert signal_was_set == False
finally:
task.stop()
def test_task_executes_until_stopped():
queue = Queue()
task = RepeatingTask(0.1, 0, lambda: queue.put(time.time()))
try:
last = None
task.start()
for _ in range(3):
t = queue.get(True, 1)
if last is not None:
assert (time.time() - last) >= 0.05
last = t
finally:
task.stop()
stopped_time = time.time()
no_more_items = False
for _ in range(2):
try:
t = queue.get(False)
assert t <= stopped_time
except Empty:
no_more_items = True
assert no_more_items == True
def test_task_can_be_stopped_from_within_the_task():
counter = 0
stopped = Event()
task = None
def do_task():
nonlocal counter
counter += 1
if counter >= 2:
task.stop()
stopped.set()
task = RepeatingTask(0.01, 0, do_task)
try:
task.start()
assert stopped.wait(0.1) == True
assert counter == 2
time.sleep(0.1)
assert counter == 2
finally:
task.stop()