#!/usr/bin/env python # Copyright (c) 2012 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import unittest import worker_pool class WorkerPoolTest(unittest.TestCase): def test_normal(self): mapper = lambda value: -value with worker_pool.ThreadPool(8) as pool: for i in range(32): pool.add_task(mapper, i) results = pool.join() self.assertEquals(range(-31, 1), sorted(results)) def test_exception(self): class FearsomeException(Exception): pass def mapper(value): raise FearsomeException(value) task_added = False try: with worker_pool.ThreadPool(8) as pool: pool.add_task(mapper, 0) task_added = True pool.join() self.fail() except FearsomeException: self.assertEquals(True, task_added) if __name__ == '__main__': unittest.main()