OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium 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 |
| 6 |
| 7 import logging |
| 8 import multiprocessing |
| 9 |
| 10 from test_result import * |
| 11 |
| 12 |
| 13 def _ShardedTestRunnable(test): |
| 14 """Standalone function needed by multiprocessing.Pool.""" |
| 15 log_format = '[' + test.device + '] # %(asctime)-15s: %(message)s' |
| 16 if logging.getLogger().handlers: |
| 17 logging.getLogger().handlers[0].setFormatter(logging.Formatter(log_format)) |
| 18 else: |
| 19 logging.basicConfig(format=log_format) |
| 20 return test.Run() |
| 21 |
| 22 |
| 23 def SetTestsContainer(tests_container): |
| 24 """Sets tests container. |
| 25 |
| 26 multiprocessing.Queue can't be pickled across processes, so we need to set |
| 27 this as a 'global', per process, via multiprocessing.Pool. |
| 28 """ |
| 29 BaseTestSharder.tests_container = tests_container |
| 30 |
| 31 |
| 32 class BaseTestSharder(object): |
| 33 """Base class for sharding tests across multiple devices. |
| 34 |
| 35 Args: |
| 36 attached_devices: A list of attached devices. |
| 37 """ |
| 38 # See more in SetTestsContainer. |
| 39 tests_container = None |
| 40 |
| 41 def __init__(self, attached_devices): |
| 42 self.attached_devices = attached_devices |
| 43 self.retries = 1 |
| 44 self.tests = [] |
| 45 |
| 46 def CreateShardedTestRunner(self, device, index): |
| 47 """Factory function to create a suite-specific test runner. |
| 48 |
| 49 Args: |
| 50 device: Device serial where this shard will run |
| 51 index: Index of this device in the pool. |
| 52 |
| 53 Returns: |
| 54 An object of BaseTestRunner type (that can provide a "Run()" method). |
| 55 """ |
| 56 pass |
| 57 |
| 58 def SetupSharding(self, tests): |
| 59 """Called before starting the shards.""" |
| 60 pass |
| 61 |
| 62 def OnTestsCompleted(self, test_runners, test_results): |
| 63 """Notifies that we completed the tests.""" |
| 64 pass |
| 65 |
| 66 def RunShardedTests(self): |
| 67 """Runs the tests in all connected devices. |
| 68 |
| 69 Returns: |
| 70 A TestResults object. |
| 71 """ |
| 72 logging.warning('*' * 80) |
| 73 logging.warning('Sharding in ' + str(len(self.attached_devices)) + |
| 74 ' devices.') |
| 75 logging.warning('Note that the output is not synchronized.') |
| 76 logging.warning('Look for the "Final result" banner in the end.') |
| 77 logging.warning('*' * 80) |
| 78 final_results = TestResults() |
| 79 for retry in xrange(self.retries): |
| 80 logging.warning('Try %d of %d' % (retry + 1, self.retries)) |
| 81 self.SetupSharding(self.tests) |
| 82 test_runners = [] |
| 83 for index, device in enumerate(self.attached_devices): |
| 84 logging.warning('*' * 80) |
| 85 logging.warning('Creating shard %d for %s' % (index, device)) |
| 86 logging.warning('*' * 80) |
| 87 test_runner = self.CreateShardedTestRunner(device, index) |
| 88 test_runners += [test_runner] |
| 89 logging.warning('Starting...') |
| 90 pool = multiprocessing.Pool(len(self.attached_devices), |
| 91 SetTestsContainer, |
| 92 [BaseTestSharder.tests_container]) |
| 93 results_lists = pool.map(_ShardedTestRunnable, test_runners) |
| 94 test_results = TestResults.FromTestResults(results_lists) |
| 95 if retry == self.retries - 1: |
| 96 all_passed = final_results.ok + test_results.ok |
| 97 final_results = test_results |
| 98 final_results.ok = all_passed |
| 99 break |
| 100 else: |
| 101 final_results.ok += test_results.ok |
| 102 self.tests = [] |
| 103 for t in test_results.GetAllBroken(): |
| 104 self.tests += [t.name] |
| 105 if not self.tests: |
| 106 break |
| 107 self.OnTestsCompleted(test_runners, final_results) |
| 108 return final_results |
OLD | NEW |