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 TestResults | |
11 | |
12 | |
13 def _ShardedTestRunnable(test): | |
14 """Standalone function needed by multiprocessing.Pool.""" | |
15 return test.RunTests() | |
16 | |
17 | |
18 def SetTestsContainer(tests_container): | |
19 """Sets tests container. | |
20 | |
21 multiprocessing.Queue can't be pickled across processes, so we need to set | |
22 this as a 'global', per process, via multiprocessing.Pool. | |
23 """ | |
24 BaseTestSharder.tests_container = tests_container | |
25 | |
26 | |
27 class BaseTestSharder(object): | |
Paweł Hajdan Jr.
2011/10/21 09:12:58
We have a sharding supervisor written by Charles L
michaelbai
2011/10/21 21:08:41
I didn't get the reply from him yet. I removed thi
| |
28 """Base class for sharding tests across multiple devices. | |
29 | |
30 Args: | |
Nirnimesh
2011/10/21 08:16:15
should go in __init__
| |
31 attached_devices: A list of attached devices. | |
32 """ | |
33 # See more in SetTestsContainer. | |
34 tests_container = None | |
35 | |
36 def __init__(self, attached_devices): | |
37 self.attached_devices = attached_devices | |
Nirnimesh
2011/10/21 08:16:15
non-public member vars should have _ prefix
| |
38 self.retries = 1 | |
39 self.tests = [] | |
40 | |
41 def CreateShardedTestRunner(self, device, index): | |
42 """Factory function to create a suite-specific test runner. | |
43 | |
44 Args: | |
45 device: Device serial where this shard will run | |
46 index: Index of this device in the pool. | |
47 | |
48 Returns: | |
49 An object of BaseTestRunner type (that can provide a "RunTests()" method). | |
50 """ | |
51 pass | |
52 | |
53 def SetupSharding(self, tests): | |
54 """Called before starting the shards.""" | |
55 pass | |
56 | |
57 def OnTestsCompleted(self, test_runners, test_results): | |
58 """Notifies that we completed the tests.""" | |
59 pass | |
60 | |
61 def RunShardedTests(self): | |
62 """Runs the tests in all connected devices. | |
63 | |
64 Returns: | |
65 A TestResults object. | |
66 """ | |
67 logging.warning('*' * 80) | |
Nirnimesh
2011/10/21 08:16:15
Why warning?
maybe debug or info?
| |
68 logging.warning('Sharding in ' + str(len(self.attached_devices)) + | |
69 ' devices.') | |
70 logging.warning('Note that the output is not synchronized.') | |
71 logging.warning('Look for the "Final result" banner in the end.') | |
72 logging.warning('*' * 80) | |
73 final_results = TestResults() | |
74 for retry in xrange(self.retries): | |
75 logging.warning('Try %d of %d' % (retry + 1, self.retries)) | |
76 self.SetupSharding(self.tests) | |
77 test_runners = [] | |
78 for index, device in enumerate(self.attached_devices): | |
79 logging.warning('*' * 80) | |
80 logging.warning('Creating shard %d for %s' % (index, device)) | |
81 logging.warning('*' * 80) | |
82 test_runner = self.CreateShardedTestRunner(device, index) | |
83 test_runners += [test_runner] | |
84 logging.warning('Starting...') | |
85 pool = multiprocessing.Pool(len(self.attached_devices), | |
86 SetTestsContainer, | |
87 [BaseTestSharder.tests_container]) | |
88 results_lists = pool.map(_ShardedTestRunnable, test_runners) | |
89 test_results = TestResults.FromTestResults(results_lists) | |
90 if retry == self.retries - 1: | |
91 all_passed = final_results.ok + test_results.ok | |
92 final_results = test_results | |
93 final_results.ok = all_passed | |
94 break | |
95 else: | |
96 final_results.ok += test_results.ok | |
97 self.tests = [] | |
98 for t in test_results.GetAllBroken(): | |
99 self.tests += [t.name] | |
100 if not self.tests: | |
101 break | |
102 self.OnTestsCompleted(test_runners, final_results) | |
103 return final_results | |
OLD | NEW |