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