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