-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_threadpool.py
More file actions
111 lines (92 loc) · 3.2 KB
/
test_threadpool.py
File metadata and controls
111 lines (92 loc) · 3.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import unittest
import threading
import stackless
import stacklesslib.threadpool
import stacklesslib.errors
import time
class TestCallOnThread(unittest.TestCase):
def test_different_thread(self):
r = []
def func():
r.append(threading.current_thread().ident)
stacklesslib.threadpool.call_on_thread(func)
self.assertNotEqual(threading.current_thread().ident, r[0])
def test_result(self):
def func():
return "hello"
r = stacklesslib.threadpool.call_on_thread(func)
self.assertEqual(r, "hello")
def test_args(self):
def func(a, b):
return a + b
r = stacklesslib.threadpool.call_on_thread(func, ("hello", "dolly"))
self.assertEqual(r, "hellodolly")
def test_kwargs(self):
def func(a, b="", c=""):
return a + b + c
r = stacklesslib.threadpool.call_on_thread(func, ("hello", " "), {"c" : "dude"})
self.assertEqual(r, "hello dude")
def test_timeout(self):
def func():
time.sleep(0.01)
self.assertRaises(stacklesslib.errors.TimeoutError, stacklesslib.threadpool.call_on_thread, func, timeout=0.001)
def test_on_abandoned_not_called(self):
def func():
pass
c = []
def orp():
c.append(True)
stacklesslib.threadpool.call_on_thread(func, on_abandoned=orp)
self.assertEqual(c, [])
def test_on_abandoned_called(self):
c = []
def func():
c.append(1)
time.sleep(0.01)
def orp():
c.append(2)
def task():
stacklesslib.threadpool.call_on_thread(func, on_abandoned=orp)
t = stackless.tasklet(task)()
# wait until call is in progress
while not c:
stackless.schedule()
time.sleep(0.001)
t.kill()
self.assertEqual(c, [1, 2])
def test_pool(self):
class pool(stacklesslib.threadpool.DummyThreadPool):
def start_thread(self, target):
self.started = True
return super(pool, self).start_thread(target)
p = pool()
self.assertFalse(hasattr(p, "started"))
stacklesslib.threadpool.call_on_thread(lambda:None, pool=p)
self.assertTrue(p.started)
class TestSimpleThreadPool(unittest.TestCase):
def test_max_threads(self):
p = stacklesslib.threadpool.SimpleThreadPool(3)
c = [0] # jobs done
n = [0] # active counter
m = [0] # max counter
l = threading.Lock()
def func():
with l:
n[0] += 1
if n[0] > m[0]:
m[0] = n[0]
time.sleep(0.01)
with l:
n[0] -= 1
c[0] += 1
for i in range(10):
p.submit(func)
while c[0] < 10:
time.sleep(0.001)
p.shutdown()
self.assertEqual(n[0], 0)
self.assertEqual(m[0], 3)
from .support import load_tests
if __name__ == "__main__":
logging.basicConfig(level=logging.ERROR)
unittest.main()