OLD | NEW |
(Empty) | |
| 1 # Copyright 2011 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 |
| 22 """Unit tests for gsutil thread pool.""" |
| 23 |
| 24 import threading |
| 25 |
| 26 import gslib.tests.testcase as testcase |
| 27 import gslib.thread_pool as thread_pool |
| 28 |
| 29 |
| 30 class GsutilThreadPoolTests(testcase.GsUtilUnitTestCase): |
| 31 """gsutil thread pool test suite.""" |
| 32 |
| 33 def _TestThreadPool(self, threads): |
| 34 """Tests pool with specified threads from end to end.""" |
| 35 pool = thread_pool.ThreadPool(threads) |
| 36 |
| 37 self.actual_call_count = 0 |
| 38 expected_call_count = 10000 |
| 39 |
| 40 self.data = xrange(expected_call_count) |
| 41 |
| 42 self.actual_result = 0 |
| 43 expected_result = sum(self.data) |
| 44 |
| 45 stats_lock = threading.Lock() |
| 46 |
| 47 def _Dummy(num): |
| 48 stats_lock.acquire() |
| 49 self.actual_call_count += 1 |
| 50 self.actual_result += num |
| 51 stats_lock.release() |
| 52 |
| 53 for data in xrange(expected_call_count): |
| 54 pool.AddTask(_Dummy, data) |
| 55 |
| 56 pool.WaitCompletion() |
| 57 self.assertEqual(self.actual_call_count, expected_call_count) |
| 58 |
| 59 self.assertEqual(self.actual_result, expected_result) |
| 60 |
| 61 pool.Shutdown() |
| 62 for thread in pool.threads: |
| 63 self.assertFalse(thread.is_alive()) |
| 64 |
| 65 def testSingleThreadPool(self): |
| 66 """Tests thread pool with a single thread.""" |
| 67 self._TestThreadPool(1) |
| 68 |
| 69 def testThirtyThreadPool(self): |
| 70 """Tests thread pool with 30 threads.""" |
| 71 self._TestThreadPool(30) |
| 72 |
| 73 def testThreadPoolExceptionHandler(self): |
| 74 """Tests thread pool with exceptions.""" |
| 75 self.exception_raised = False |
| 76 |
| 77 def _ExceptionHandler(e): |
| 78 """Verify an exception is raised and that it's the correct one.""" |
| 79 self.assertTrue(isinstance(e, TypeError)) |
| 80 self.assertEqual(e[0], 'gsutil') |
| 81 self.exception_raised = True |
| 82 |
| 83 pool = thread_pool.ThreadPool(1, exception_handler=_ExceptionHandler) |
| 84 |
| 85 def _Dummy(): |
| 86 raise TypeError('gsutil') |
| 87 |
| 88 pool.AddTask(_Dummy) |
| 89 pool.WaitCompletion() |
| 90 pool.Shutdown() |
| 91 |
| 92 self.assertTrue(self.exception_raised) |
OLD | NEW |