-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
130 lines (90 loc) · 3.6 KB
/
Copy pathtest.py
File metadata and controls
130 lines (90 loc) · 3.6 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from pool import PyPool
import time
import unittest
import threading
import os
class TestPool(unittest.TestCase):
def setUp(self):
# Create an example Pool, using a callback to print returned data & no error handling.
self.pool = PyPool(iteration=True, tags={
'test': 1,
'ignore': 1
}, callback=lambda r: print('Returned:', r, ', Pending:', self.pool.pending))
self.count = 0
def test_async(self):
""" Concurrency should properly work """
start = time.time()
self.pool.adjust('test2', 10)
self.pool.ingest([2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 'test2', time.sleep, [])
self.pool.join() # Wait for all ingesting & subprocess running to be complete.
# Should take ~5 seconds, since they should all run at once.
self.assertLess(time.time() - start, 15, 'Took longer than expected to run test - concurrency may be broken')
def test_adjust(self):
""" Adjust should properly add/remove generic slots """
pool = self.pool
pool.adjust('test', 10)
pool.adjust(None, 0)
pool.adjust('test', 5, use_general_slots=True)
self.assertEqual(pool.get_tags()['test'], 5, 'Incorrect slots set for test!')
self.assertEqual(pool.get_tags()[None], 5, 'Incorrect slots adjusted for general!')
pool.adjust('test', 1, use_general_slots=True)
self.assertEqual(pool.get_tags()['test'], 1, 'Incorrect slots set for test!')
self.assertEqual(pool.get_tags()[None], 9, 'Incorrect slots adjusted for general!')
self.assertEqual(pool.get_tags()['ignore'], 1, 'Ignored pool should not have been touched!')
self.assertEqual(pool._total, 11, msg='Incorrect total count was left after adjustments.')
print(pool)
with self.assertRaises(Exception, msg='Failed to raise Error on invalid pool size increase!'):
pool.adjust('test', 11, use_general_slots=True)
def test_stop(self):
""" Stop should correctly exit """
start = time.time()
self.pool.adjust('test', 1)
self.pool.adjust(None, 0)
self.pool.ingest([60, 60, 60], 'test', time.sleep, [])
self.pool.stop()
print('Stop completed.')
self.assertLess(time.time() - start, 15, 'Took longer than expected to run test - concurrency may be broken')
def test_iter(self):
""" The iterator should work """
self.pool.ingest([1, 2, 3, 4, 5, 6, 7, 8], 'test', fnc)
self.pool.callback(None)
count = 0
for r in self.pool:
count += r
self.assertEqual(count, 36, 'Did not get all results back from iterator!')
def test_callback(self):
""" The callback method should work """
def cb(val):
self.count += val
def err(e):
self.count += 100
self.pool.callback(cb=cb)
self.pool.on_error(err)
for i in range(4):
self.pool.put('test', fnc, [i])
self.pool.put('test', fnc, []) # Trigger an error, which will be caught and increment counter by 100.
self.pool.join()
self.assertEqual(self.count, 106, 'Callback/Error was not triggered enough times!')
def test_single_callback(self):
""" Individual tasks should support custom callbacks """
def cb(val):
self.count += val
def err(e):
self.count += 100
self.pool.callback(cb=None) # Clear any built-in handlers for custom callback test.
self.pool.on_error(None)
for i in range(3):
self.pool.put('test', fnc, [i], callback=cb)
self.pool.put('test', fnc, [], error=err) # Trigger an error, which will be caught and increment counter by 100
self.pool.join()
self.assertEqual(self.count, 103, 'Custom callback/error was not triggered enough times!')
def fnc(num):
return num
def timeout():
time.sleep(60)
print('Timed out!')
os._exit(103)
timeout_thread = threading.Thread(target=timeout, daemon=True)
timeout_thread.start()
if __name__ == "__main__":
unittest.main()