11cb0ef41Sopenharmony_ci#!/usr/bin/env python3 21cb0ef41Sopenharmony_ci# Copyright 2014 the V8 project authors. All rights reserved. 31cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be 41cb0ef41Sopenharmony_ci# found in the LICENSE file. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciimport os 71cb0ef41Sopenharmony_ciimport sys 81cb0ef41Sopenharmony_ciimport unittest 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci# Needed because the test runner contains relative imports. 111cb0ef41Sopenharmony_ciTOOLS_PATH = os.path.dirname( 121cb0ef41Sopenharmony_ci os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 131cb0ef41Sopenharmony_cisys.path.append(TOOLS_PATH) 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cifrom testrunner.local.pool import Pool 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cidef Run(x): 191cb0ef41Sopenharmony_ci if x == 10: 201cb0ef41Sopenharmony_ci raise Exception("Expected exception triggered by test.") 211cb0ef41Sopenharmony_ci return x 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciclass PoolTest(unittest.TestCase): 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci def testNormal(self): 271cb0ef41Sopenharmony_ci results = set() 281cb0ef41Sopenharmony_ci pool = Pool(3) 291cb0ef41Sopenharmony_ci for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): 301cb0ef41Sopenharmony_ci if result.heartbeat: 311cb0ef41Sopenharmony_ci # Any result can be a heartbeat due to timings. 321cb0ef41Sopenharmony_ci continue 331cb0ef41Sopenharmony_ci results.add(result.value) 341cb0ef41Sopenharmony_ci self.assertEqual(set(range(0, 10)), results) 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci def testException(self): 371cb0ef41Sopenharmony_ci results = set() 381cb0ef41Sopenharmony_ci pool = Pool(3) 391cb0ef41Sopenharmony_ci with self.assertRaises(Exception): 401cb0ef41Sopenharmony_ci for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]): 411cb0ef41Sopenharmony_ci if result.heartbeat: 421cb0ef41Sopenharmony_ci # Any result can be a heartbeat due to timings. 431cb0ef41Sopenharmony_ci continue 441cb0ef41Sopenharmony_ci # Item 10 will not appear in results due to an internal exception. 451cb0ef41Sopenharmony_ci results.add(result.value) 461cb0ef41Sopenharmony_ci expect = set(range(0, 12)) 471cb0ef41Sopenharmony_ci expect.remove(10) 481cb0ef41Sopenharmony_ci self.assertEqual(expect, results) 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci def testAdd(self): 511cb0ef41Sopenharmony_ci results = set() 521cb0ef41Sopenharmony_ci pool = Pool(3) 531cb0ef41Sopenharmony_ci for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]): 541cb0ef41Sopenharmony_ci if result.heartbeat: 551cb0ef41Sopenharmony_ci # Any result can be a heartbeat due to timings. 561cb0ef41Sopenharmony_ci continue 571cb0ef41Sopenharmony_ci results.add(result.value) 581cb0ef41Sopenharmony_ci if result.value < 30: 591cb0ef41Sopenharmony_ci pool.add([result.value + 20]) 601cb0ef41Sopenharmony_ci self.assertEqual( 611cb0ef41Sopenharmony_ci set(range(0, 10)) | set(range(20, 30)) | set(range(40, 50)), results) 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ciif __name__ == '__main__': 651cb0ef41Sopenharmony_ci unittest.main() 66