Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: build/android/pylib/base/shard.py

Issue 12278020: [Android] Re-write the gtest TestRunner and introduce a new generic sharder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments and rebased Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2013 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 """Implements test sharding logic."""
6
7 import logging
8 import sys
9 import threading
10
11 from pylib import android_commands
12 from pylib import forwarder
13
14 import test_result
15
16
17 class _Worker(threading.Thread):
18 """Runs tests from the test_queue using the given runner in a separate thread.
19
20 Places results in the results_list.
21 """
22 def __init__(self, runner, test_queue, out_results, out_retry):
23 """Initializes the worker.
24
25 Args:
26 runner: A TestRunner object used to run the tests.
27 test_queue: A list from which to get tests to run.
28 out_results: A list to add TestResults to.
29 out_retry: A list to add tests to retry.
30 """
31 super(_Worker, self).__init__()
32 self.daemon = True
33 self._exc_info = None
34 self._runner = runner
35 self._test_queue = test_queue
36 self._out_results = out_results
37 self._out_retry = out_retry
38
39 def run(self):
40 """Run tests from the queue until it is empty, storing results.
41
42 Overriden from threading.Thread, runs in a separate thread.
43 """
44 try:
45 while True:
46 test = self._test_queue.pop()
47 result, retry = self._runner.Run(test)
48 self._out_results.append(result)
49 if retry:
50 self._out_retry.append(retry)
51 except IndexError:
52 pass
53 except:
54 self._exc_info = sys.exc_info()
55 raise
56
57 def Reraise(self):
58 """Reraise an exception raised in the thread."""
59 if self._exc_info:
60 raise self._exc_info[0], self._exc_info[1], self._exc_info[2]
61
62
63 def _RunAllTests(runners, tests):
64 """Run all tests using the given TestRunners.
65
66 Args:
67 runners: a list of TestRunner objects.
68 tests: a list of Tests to run using the given TestRunners.
69
70 Returns:
71 Tuple: (list of TestResults, list of tests to retry)
72 """
73 tests_queue = list(tests)
74 workers = []
75 results = []
76 retry = []
77 for r in runners:
78 worker = _Worker(r, tests_queue, results, retry)
79 worker.start()
80 workers.append(worker)
81 while workers:
82 for w in workers[:]:
83 # Allow the main thread to periodically check for keyboard interrupts.
84 w.join(0.1)
85 if not w.isAlive():
86 w.Reraise()
87 workers.remove(w)
88 return (results, retry)
89
90
91 def _CreateRunners(runner_factory, devices):
92 """Creates a test runner for each device.
frankf 2013/02/19 23:57:26 Expand to include throwing out devices.
craigdh 2013/02/20 00:05:25 Done.
93
94 Args:
95 runner_factory: callable that takes a device and returns a TestRunner.
96 devices: list of device serial numbers as strings.
97
98 Returns:
99 A list of TestRunner objects.
100 """
101 test_runners = []
102 for index, device in enumerate(devices):
103 logging.warning('*' * 80)
104 logging.warning('Creating shard %d for %s', index, device)
105 logging.warning('*' * 80)
106 try:
107 test_runners.append(runner_factory(device))
108 except android_commands.errors.DeviceUnresponsiveError as e:
109 logging.critical('****Failed to create a shard: [%s]', e)
frankf 2013/02/19 23:57:26 This is warning not critical.
craigdh 2013/02/20 00:05:25 Done.
110 return test_runners
111
112
113 def ShardAndRunTests(
114 runner_factory, devices, tests, build_type='Debug', retries=3,
frankf 2013/02/19 23:57:26 retries -> tries to avoid off-by-1.
craigdh 2013/02/20 00:05:25 Done.
115 get_attached_devices_callable=android_commands.GetAttachedDevices):
116 """Run all tests on attached devices, retrying tests that don't pass.
117
118 Args:
119 runner_factory: callable that takes a device and returns a TestRunner.
120 devices: list of attached device serial numbers as strings.
121 tests: list of tests to run.
122 build_type: either 'Debug' or 'Release'.
123 retries: number of retries before accepting failure.
124 get_attached_devices_callable: only override default value for testing.
125
126 Returns:
127 A test_result.TestResults object.
128 """
129 final_results = test_result.TestResults()
130 results = test_result.TestResults()
131 forwarder.Forwarder.KillHost(build_type)
132 tries = 0
133 while tests:
134 devices = set(devices).intersection(get_attached_devices_callable())
135 if not devices or tries > retries:
136 results.ok = final_results.ok
137 final_results = results
138 break
139 tries += 1
140 runners = _CreateRunners(runner_factory, devices)
141 try:
142 results_list, tests = _RunAllTests(runners, tests)
143 results = test_result.TestResults.FromTestResults(results_list)
144 except android_commands.errors.DeviceUnresponsiveError as e:
145 logging.critical('****Failed to run test: [%s]', e)
frankf 2013/02/19 23:57:26 warning not critical.
craigdh 2013/02/20 00:05:25 Done.
146 continue
147 final_results.ok += results.ok
frankf 2013/02/19 23:57:26 Move to end of try.
craigdh 2013/02/20 00:05:25 Done.
148 forwarder.Forwarder.KillHost(build_type)
149 return final_results
OLDNEW
« no previous file with comments | « build/android/pylib/base/new_base_test_runner.py ('k') | build/android/pylib/base/shard_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698