OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2011 Google Inc. |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 # IN THE SOFTWARE. |
| 23 |
| 24 """Unit tests for gsutil thread pool.""" |
| 25 |
| 26 import sys |
| 27 import threading |
| 28 import thread_pool |
| 29 import unittest |
| 30 |
| 31 |
| 32 class GsutilThreadPoolTests(unittest.TestCase): |
| 33 """gsutil thread pool test suite.""" |
| 34 |
| 35 def GetSuiteDescription(self): |
| 36 return 'gsutil thread pool test suite' |
| 37 |
| 38 @classmethod |
| 39 def SetUpClass(cls): |
| 40 """Creates class level artifacts useful to multiple tests.""" |
| 41 pass |
| 42 |
| 43 @classmethod |
| 44 def TearDownClass(cls): |
| 45 """Cleans up any artifacts created by SetUpClass.""" |
| 46 pass |
| 47 |
| 48 def _TestThreadPool(self, threads): |
| 49 """Tests pool with specified threads from end to end.""" |
| 50 pool = thread_pool.ThreadPool(threads) |
| 51 |
| 52 self.actual_call_count = 0 |
| 53 expected_call_count = 10000 |
| 54 |
| 55 self.data = xrange(expected_call_count) |
| 56 |
| 57 self.actual_result = 0 |
| 58 expected_result = sum(self.data) |
| 59 |
| 60 stats_lock = threading.Lock() |
| 61 |
| 62 def _Dummy(num): |
| 63 stats_lock.acquire() |
| 64 self.actual_call_count += 1 |
| 65 self.actual_result += num |
| 66 stats_lock.release() |
| 67 |
| 68 for data in xrange(expected_call_count): |
| 69 pool.AddTask(_Dummy, data) |
| 70 |
| 71 pool.WaitCompletion() |
| 72 self.assertEqual(self.actual_call_count, expected_call_count) |
| 73 |
| 74 self.assertEqual(self.actual_result, expected_result) |
| 75 |
| 76 pool.Shutdown() |
| 77 for thread in pool.threads: |
| 78 self.assertFalse(thread.is_alive()) |
| 79 |
| 80 def TestSingleThreadPool(self): |
| 81 """Tests thread pool with a single thread.""" |
| 82 self._TestThreadPool(1) |
| 83 |
| 84 def TestThirtyThreadPool(self): |
| 85 """Tests thread pool with 30 threads.""" |
| 86 self._TestThreadPool(30) |
| 87 |
| 88 def TestThreadPoolExceptionHandler(self): |
| 89 """Tests thread pool with exceptions.""" |
| 90 self.exception_raised = False |
| 91 |
| 92 def _ExceptionHandler(e): |
| 93 """Verify an exception is raised and that it's the correct one.""" |
| 94 self.assertTrue(isinstance(e, TypeError)) |
| 95 self.assertEqual(e[0], 'gsutil') |
| 96 self.exception_raised = True |
| 97 |
| 98 pool = thread_pool.ThreadPool(1, exception_handler=_ExceptionHandler) |
| 99 |
| 100 def _Dummy(): |
| 101 raise TypeError('gsutil') |
| 102 |
| 103 pool.AddTask(_Dummy) |
| 104 pool.WaitCompletion() |
| 105 pool.Shutdown() |
| 106 |
| 107 self.assertTrue(self.exception_raised) |
| 108 |
| 109 |
| 110 if __name__ == '__main__': |
| 111 if sys.version_info[:3] < (2, 5, 1): |
| 112 sys.exit('These tests must be run on at least Python 2.5.1\n') |
| 113 test_loader = unittest.TestLoader() |
| 114 test_loader.testMethodPrefix = 'Test' |
| 115 suite = test_loader.loadTestsFromTestCase(GsutilThreadPoolTests) |
| 116 # Seems like there should be a cleaner way to find the test_class. |
| 117 test_class = suite.__getattribute__('_tests')[0] |
| 118 # We call SetUpClass() and TearDownClass() ourselves because we |
| 119 # don't assume the user has Python 2.7 (which supports classmethods |
| 120 # that do it, with camelCase versions of these names). |
| 121 try: |
| 122 print 'Setting up %s...' % test_class.GetSuiteDescription() |
| 123 test_class.SetUpClass() |
| 124 print 'Running %s...' % test_class.GetSuiteDescription() |
| 125 unittest.TextTestRunner(verbosity=2).run(suite) |
| 126 finally: |
| 127 print 'Cleaning up after %s...' % test_class.GetSuiteDescription() |
| 128 test_class.TearDownClass() |
| 129 print '' |
OLD | NEW |