| 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.Shutdown() | |
| 57 self.assertEqual(self.actual_call_count, expected_call_count) | |
| 58 self.assertEqual(self.actual_result, expected_result) | |
| 59 | |
| 60 for thread in pool.threads: | |
| 61 self.assertFalse(thread.is_alive()) | |
| 62 | |
| 63 def testSingleThreadPool(self): | |
| 64 """Tests thread pool with a single thread.""" | |
| 65 self._TestThreadPool(1) | |
| 66 | |
| 67 def testThirtyThreadPool(self): | |
| 68 """Tests thread pool with 30 threads.""" | |
| 69 self._TestThreadPool(30) | |
| 70 | |
| 71 def testThreadPoolExceptionHandler(self): | |
| 72 """Tests thread pool with exceptions.""" | |
| 73 self.exception_raised = False | |
| 74 | |
| 75 def _ExceptionHandler(e): | |
| 76 """Verify an exception is raised and that it's the correct one.""" | |
| 77 self.assertTrue(isinstance(e, TypeError)) | |
| 78 self.assertEqual(e[0], 'gsutil') | |
| 79 self.exception_raised = True | |
| 80 | |
| 81 pool = thread_pool.ThreadPool(1, exception_handler=_ExceptionHandler) | |
| 82 | |
| 83 def _Dummy(): | |
| 84 raise TypeError('gsutil') | |
| 85 | |
| 86 pool.AddTask(_Dummy) | |
| 87 pool.Shutdown() | |
| 88 | |
| 89 self.assertTrue(self.exception_raised) | |
| OLD | NEW |