Home | History | Annotate | Download | only in local
      1 #!/usr/bin/env python
      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
      4 # found in the LICENSE file.
      5 
      6 import unittest
      7 
      8 from pool import Pool
      9 
     10 def Run(x):
     11   if x == 10:
     12     raise Exception("Expected exception triggered by test.")
     13   return x
     14 
     15 class PoolTest(unittest.TestCase):
     16   def testNormal(self):
     17     results = set()
     18     pool = Pool(3)
     19     for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]):
     20       results.add(result)
     21     self.assertEquals(set(range(0, 10)), results)
     22 
     23   def testException(self):
     24     results = set()
     25     pool = Pool(3)
     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.
     28       results.add(result)
     29     expect = set(range(0, 12))
     30     expect.remove(10)
     31     self.assertEquals(expect, results)
     32 
     33   def testAdd(self):
     34     results = set()
     35     pool = Pool(3)
     36     for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]):
     37       results.add(result)
     38       if result < 30:
     39         pool.add([result + 20])
     40     self.assertEquals(set(range(0, 10) + range(20, 30) + range(40, 50)),
     41                       results)
     42