OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 the V8 project authors. All rights reserved. | 2 # Copyright 2014 the V8 project authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import unittest | 6 import unittest |
7 | 7 |
8 from pool import Pool | 8 from pool import Pool |
9 | 9 |
10 def Run(x): | 10 def Run(x): |
11 if x == 10: | 11 if x == 10: |
12 raise Exception("Expected exception triggered by test.") | 12 raise Exception("Expected exception triggered by test.") |
13 return x | 13 return x |
14 | 14 |
15 class PoolTest(unittest.TestCase): | 15 class PoolTest(unittest.TestCase): |
16 def testNormal(self): | 16 def testNormal(self): |
17 results = set() | 17 results = set() |
18 pool = Pool(3) | 18 pool = Pool(3) |
19 for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): | 19 for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): |
20 results.add(result) | 20 results.add(result.value) |
21 self.assertEquals(set(range(0, 10)), results) | 21 self.assertEquals(set(range(0, 10)), results) |
22 | 22 |
23 def testException(self): | 23 def testException(self): |
24 results = set() | 24 results = set() |
25 pool = Pool(3) | 25 pool = Pool(3) |
26 for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]): | 26 for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]): |
27 # Item 10 will not appear in results due to an internal exception. | 27 # Item 10 will not appear in results due to an internal exception. |
28 results.add(result) | 28 results.add(result.value) |
29 expect = set(range(0, 12)) | 29 expect = set(range(0, 12)) |
30 expect.remove(10) | 30 expect.remove(10) |
31 self.assertEquals(expect, results) | 31 self.assertEquals(expect, results) |
32 | 32 |
33 def testAdd(self): | 33 def testAdd(self): |
34 results = set() | 34 results = set() |
35 pool = Pool(3) | 35 pool = Pool(3) |
36 for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): | 36 for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): |
37 results.add(result) | 37 results.add(result.value) |
38 if result < 30: | 38 if result.value < 30: |
39 pool.add([result + 20]) | 39 pool.add([result.value + 20]) |
40 self.assertEquals(set(range(0, 10) + range(20, 30) + range(40, 50)), | 40 self.assertEquals(set(range(0, 10) + range(20, 30) + range(40, 50)), |
41 results) | 41 results) |
OLD | NEW |