Skip to content

Commit 98e7560

Browse files
committed
add unit test & fix some linter stuff
1 parent cba9133 commit 98e7560

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed
Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Workerpool test module."""
2+
# pylint: disable=no-self-use,too-few-public-methods,missing-docstring
23
import time
34
import threading
45
from splitio.tasks.util import workerpool
@@ -10,44 +11,63 @@ class WorkerPoolTests(object):
1011
def test_normal_operation(self, mocker):
1112
"""Test normal opeation works properly."""
1213
worker_func = mocker.Mock()
13-
wp = workerpool.WorkerPool(10, worker_func)
14-
wp.start()
15-
for x in range(0, 100):
16-
wp.submit_work(str(x))
14+
wpool = workerpool.WorkerPool(10, worker_func)
15+
wpool.start()
16+
for num in range(0, 100):
17+
wpool.submit_work(str(num))
1718

1819
stop_event = threading.Event()
19-
wp.stop(stop_event)
20+
wpool.stop(stop_event)
2021
stop_event.wait(5)
2122
assert stop_event.is_set()
2223

2324
calls = worker_func.mock_calls
24-
for x in range(0, 100):
25-
assert mocker.call(str(x)) in calls
25+
for num in range(0, 100):
26+
assert mocker.call(str(num)) in calls
2627

27-
def test_failure_in_message_doesnt_breal(self, mocker):
28+
def test_fail_in_msg_doesnt_break(self):
2829
"""Test that if a message cannot be parsed it is ignored and others are processed."""
29-
class Worker:
30+
class Worker(object): #pylint: disable=
3031
def __init__(self):
31-
self._worked = set()
32+
self.worked = set()
3233

33-
def do_work(self, w):
34-
if w == '55':
34+
def do_work(self, work):
35+
if work == '55':
3536
raise Exception('something')
36-
self._worked.add(w)
37+
self.worked.add(work)
3738

3839
worker = Worker()
39-
wp = workerpool.WorkerPool(50, worker.do_work)
40-
wp.start()
41-
for x in range(0, 100):
42-
wp.submit_work(str(x))
40+
wpool = workerpool.WorkerPool(50, worker.do_work)
41+
wpool.start()
42+
for num in range(0, 100):
43+
wpool.submit_work(str(num))
4344

4445
stop_event = threading.Event()
45-
wp.stop(stop_event)
46+
wpool.stop(stop_event)
4647
stop_event.wait(5)
4748
assert stop_event.is_set()
4849

49-
for x in range(0, 100):
50-
if x != 55:
51-
assert str(x) in worker._worked
50+
for num in range(0, 100):
51+
if num != 55:
52+
assert str(num) in worker.worked
5253
else:
53-
assert str(x) not in worker._worked
54+
assert str(num) not in worker.worked
55+
56+
def test_msg_acked_after_processed(self):
57+
"""Test that events are only set after all the work in the pipeline is done."""
58+
class Worker(object):
59+
def __init__(self):
60+
self.worked = set()
61+
62+
def do_work(self, work):
63+
self.worked.add(work)
64+
time.sleep(0.02) # will wait 2 seconds in total for 100 elements
65+
66+
worker = Worker()
67+
wpool = workerpool.WorkerPool(50, worker.do_work)
68+
wpool.start()
69+
for num in range(0, 100):
70+
wpool.submit_work(str(num))
71+
72+
wpool.wait_for_completion()
73+
assert len(worker.worked) == 100

0 commit comments

Comments
 (0)