1#!/usr/bin/env python3 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 6import os 7import sys 8import unittest 9 10# Needed because the test runner contains relative imports. 11TOOLS_PATH = os.path.dirname( 12 os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 13sys.path.append(TOOLS_PATH) 14 15from testrunner.local.pool import Pool 16 17 18def Run(x): 19 if x == 10: 20 raise Exception("Expected exception triggered by test.") 21 return x 22 23 24class PoolTest(unittest.TestCase): 25 26 def testNormal(self): 27 results = set() 28 pool = Pool(3) 29 for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): 30 if result.heartbeat: 31 # Any result can be a heartbeat due to timings. 32 continue 33 results.add(result.value) 34 self.assertEqual(set(range(0, 10)), results) 35 36 def testException(self): 37 results = set() 38 pool = Pool(3) 39 with self.assertRaises(Exception): 40 for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]): 41 if result.heartbeat: 42 # Any result can be a heartbeat due to timings. 43 continue 44 # Item 10 will not appear in results due to an internal exception. 45 results.add(result.value) 46 expect = set(range(0, 12)) 47 expect.remove(10) 48 self.assertEqual(expect, results) 49 50 def testAdd(self): 51 results = set() 52 pool = Pool(3) 53 for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): 54 if result.heartbeat: 55 # Any result can be a heartbeat due to timings. 56 continue 57 results.add(result.value) 58 if result.value < 30: 59 pool.add([result.value + 20]) 60 self.assertEqual( 61 set(range(0, 10)) | set(range(20, 30)) | set(range(40, 50)), results) 62 63 64if __name__ == '__main__': 65 unittest.main() 66