2022-03-17 08:29:34 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-05-14 13:30:57 +00:00
|
|
|
# Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2014-05-14 13:30:57 +00:00
|
|
|
import unittest
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
# Needed because the test runner contains relative imports.
|
2022-03-29 10:29:45 +00:00
|
|
|
TOOLS_PATH = os.path.dirname(
|
|
|
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2018-02-20 17:23:37 +00:00
|
|
|
sys.path.append(TOOLS_PATH)
|
|
|
|
|
|
|
|
from testrunner.local.pool import Pool
|
2014-05-14 13:30:57 +00:00
|
|
|
|
2022-03-29 10:29:45 +00:00
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
def Run(x):
|
|
|
|
if x == 10:
|
|
|
|
raise Exception("Expected exception triggered by test.")
|
|
|
|
return x
|
|
|
|
|
2022-03-29 10:29:45 +00:00
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
class PoolTest(unittest.TestCase):
|
2022-03-29 10:29:45 +00:00
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
def testNormal(self):
|
|
|
|
results = set()
|
|
|
|
pool = Pool(3)
|
|
|
|
for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]):
|
2018-02-20 17:23:37 +00:00
|
|
|
if result.heartbeat:
|
|
|
|
# Any result can be a heartbeat due to timings.
|
|
|
|
continue
|
2015-04-08 09:53:35 +00:00
|
|
|
results.add(result.value)
|
2022-03-17 08:29:34 +00:00
|
|
|
self.assertEqual(set(range(0, 10)), results)
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
def testException(self):
|
|
|
|
results = set()
|
|
|
|
pool = Pool(3)
|
2017-02-07 13:56:20 +00:00
|
|
|
with self.assertRaises(Exception):
|
|
|
|
for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]):
|
2018-02-20 17:23:37 +00:00
|
|
|
if result.heartbeat:
|
|
|
|
# Any result can be a heartbeat due to timings.
|
|
|
|
continue
|
2017-02-07 13:56:20 +00:00
|
|
|
# Item 10 will not appear in results due to an internal exception.
|
|
|
|
results.add(result.value)
|
2014-05-14 13:30:57 +00:00
|
|
|
expect = set(range(0, 12))
|
|
|
|
expect.remove(10)
|
2022-03-17 08:29:34 +00:00
|
|
|
self.assertEqual(expect, results)
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
def testAdd(self):
|
|
|
|
results = set()
|
|
|
|
pool = Pool(3)
|
|
|
|
for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]):
|
2018-02-20 17:23:37 +00:00
|
|
|
if result.heartbeat:
|
|
|
|
# Any result can be a heartbeat due to timings.
|
|
|
|
continue
|
2015-04-08 09:53:35 +00:00
|
|
|
results.add(result.value)
|
|
|
|
if result.value < 30:
|
|
|
|
pool.add([result.value + 20])
|
2022-03-17 08:29:34 +00:00
|
|
|
self.assertEqual(
|
2022-03-29 10:29:45 +00:00
|
|
|
set(range(0, 10)) | set(range(20, 30)) | set(range(40, 50)), results)
|
2018-01-31 10:21:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-03-29 10:29:45 +00:00
|
|
|
unittest.main()
|