• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 unittest
7
8from pool import Pool
9
10def Run(x):
11  if x == 10:
12    raise Exception("Expected exception triggered by test.")
13  return x
14
15class 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.value)
21    self.assertEquals(set(range(0, 10)), results)
22
23  def testException(self):
24    results = set()
25    pool = Pool(3)
26    with self.assertRaises(Exception):
27      for result in pool.imap_unordered(Run, [[x] for x in range(0, 12)]):
28        # Item 10 will not appear in results due to an internal exception.
29        results.add(result.value)
30    expect = set(range(0, 12))
31    expect.remove(10)
32    self.assertEquals(expect, results)
33
34  def testAdd(self):
35    results = set()
36    pool = Pool(3)
37    for result in pool.imap_unordered(Run, [[x] for x in range(0, 10)]):
38      results.add(result.value)
39      if result.value < 30:
40        pool.add([result.value + 20])
41    self.assertEquals(set(range(0, 10) + range(20, 30) + range(40, 50)),
42                      results)
43