OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Dispatches tests, either sharding or replicating them. | 5 """Dispatches tests, either sharding or replicating them. |
6 | 6 |
7 Performs the following steps: | 7 Performs the following steps: |
8 * Create a test collection factory, using the given tests | 8 * Create a test collection factory, using the given tests |
9 - If sharding: test collection factory returns the same shared test collection | 9 - If sharding: test collection factory returns the same shared test collection |
10 to all test runners | 10 to all test runners |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 runners: A list of TestRunner objects. | 323 runners: A list of TestRunner objects. |
324 timeout: Watchdog timeout in seconds, defaults to the default timeout. | 324 timeout: Watchdog timeout in seconds, defaults to the default timeout. |
325 """ | 325 """ |
326 threads = reraiser_thread.ReraiserThreadGroup( | 326 threads = reraiser_thread.ReraiserThreadGroup( |
327 [reraiser_thread.ReraiserThread(r.TearDown, name=r.device_serial[-4:]) | 327 [reraiser_thread.ReraiserThread(r.TearDown, name=r.device_serial[-4:]) |
328 for r in runners]) | 328 for r in runners]) |
329 threads.StartAll() | 329 threads.StartAll() |
330 threads.JoinAll(watchdog_timer.WatchdogTimer(timeout)) | 330 threads.JoinAll(watchdog_timer.WatchdogTimer(timeout)) |
331 | 331 |
332 | 332 |
| 333 def ApplyMaxPerRun(tests, max_per_run): |
| 334 """Rearrange the tests so that no group contains more than max_per_run tests. |
| 335 |
| 336 Args: |
| 337 tests: |
| 338 max_per_run: |
| 339 |
| 340 Returns: |
| 341 A list of tests with no more than max_per_run per run. |
| 342 """ |
| 343 tests_expanded = [] |
| 344 for test_group in tests: |
| 345 if type(test_group) != str: |
| 346 # Do not split test objects which are not strings. |
| 347 tests_expanded.append(test_group) |
| 348 else: |
| 349 test_split = test_group.split(':') |
| 350 for i in range(0, len(test_split), max_per_run): |
| 351 tests_expanded.append(':'.join(test_split[i:i+max_per_run])) |
| 352 return tests_expanded |
| 353 |
| 354 |
333 def RunTests(tests, runner_factory, devices, shard=True, | 355 def RunTests(tests, runner_factory, devices, shard=True, |
334 test_timeout=DEFAULT_TIMEOUT, setup_timeout=DEFAULT_TIMEOUT, | 356 test_timeout=DEFAULT_TIMEOUT, setup_timeout=DEFAULT_TIMEOUT, |
335 num_retries=2): | 357 num_retries=2, max_per_run=256): |
336 """Run all tests on attached devices, retrying tests that don't pass. | 358 """Run all tests on attached devices, retrying tests that don't pass. |
337 | 359 |
338 Args: | 360 Args: |
339 tests: List of tests to run. | 361 tests: List of tests to run. |
340 runner_factory: Callable that takes a device and index and returns a | 362 runner_factory: Callable that takes a device and index and returns a |
341 TestRunner object. | 363 TestRunner object. |
342 devices: List of attached devices. | 364 devices: List of attached devices. |
343 shard: True if we should shard, False if we should replicate tests. | 365 shard: True if we should shard, False if we should replicate tests. |
344 - Sharding tests will distribute tests across all test runners through a | 366 - Sharding tests will distribute tests across all test runners through a |
345 shared test collection. | 367 shared test collection. |
346 - Replicating tests will copy all tests to each test runner through a | 368 - Replicating tests will copy all tests to each test runner through a |
347 unique test collection for each test runner. | 369 unique test collection for each test runner. |
348 test_timeout: Watchdog timeout in seconds for running tests. | 370 test_timeout: Watchdog timeout in seconds for running tests. |
349 setup_timeout: Watchdog timeout in seconds for creating and cleaning up | 371 setup_timeout: Watchdog timeout in seconds for creating and cleaning up |
350 test runners. | 372 test runners. |
351 num_retries: Number of retries for a test. | 373 num_retries: Number of retries for a test. |
| 374 max_per_run: Maximum number of tests to run in any group. |
352 | 375 |
353 Returns: | 376 Returns: |
354 A tuple of (base_test_result.TestRunResults object, exit code). | 377 A tuple of (base_test_result.TestRunResults object, exit code). |
355 """ | 378 """ |
356 if not tests: | 379 if not tests: |
357 logging.critical('No tests to run.') | 380 logging.critical('No tests to run.') |
358 return (base_test_result.TestRunResults(), constants.ERROR_EXIT_CODE) | 381 return (base_test_result.TestRunResults(), constants.ERROR_EXIT_CODE) |
359 | 382 |
| 383 tests_expanded = ApplyMaxPerRun(tests, max_per_run) |
360 if shard: | 384 if shard: |
361 # Generate a shared _TestCollection object for all test runners, so they | 385 # Generate a shared _TestCollection object for all test runners, so they |
362 # draw from a common pool of tests. | 386 # draw from a common pool of tests. |
363 shared_test_collection = _TestCollection([_Test(t) for t in tests]) | 387 shared_test_collection = _TestCollection([_Test(t) for t in tests_expanded]) |
364 test_collection_factory = lambda: shared_test_collection | 388 test_collection_factory = lambda: shared_test_collection |
365 tag_results_with_device = False | 389 tag_results_with_device = False |
366 log_string = 'sharded across devices' | 390 log_string = 'sharded across devices' |
367 else: | 391 else: |
368 # Generate a unique _TestCollection object for each test runner, but use | 392 # Generate a unique _TestCollection object for each test runner, but use |
369 # the same set of tests. | 393 # the same set of tests. |
370 test_collection_factory = lambda: _TestCollection([_Test(t) for t in tests]) | 394 test_collection_factory = lambda: _TestCollection( |
| 395 [_Test(t) for t in tests_expanded]) |
371 tag_results_with_device = True | 396 tag_results_with_device = True |
372 log_string = 'replicated on each device' | 397 log_string = 'replicated on each device' |
373 | 398 |
374 logging.info('Will run %d tests (%s): %s', len(tests), log_string, str(tests)) | 399 logging.info('Will run %d tests (%s): %s', |
| 400 len(tests_expanded), log_string, str(tests_expanded)) |
375 runners = _CreateRunners(runner_factory, devices, setup_timeout) | 401 runners = _CreateRunners(runner_factory, devices, setup_timeout) |
376 try: | 402 try: |
377 return _RunAllTests(runners, test_collection_factory, | 403 return _RunAllTests(runners, test_collection_factory, |
378 num_retries, test_timeout, tag_results_with_device) | 404 num_retries, test_timeout, tag_results_with_device) |
379 finally: | 405 finally: |
380 try: | 406 try: |
381 _TearDownRunners(runners, setup_timeout) | 407 _TearDownRunners(runners, setup_timeout) |
382 except (device_errors.DeviceUnreachableError, | 408 except (device_errors.DeviceUnreachableError, |
383 # TODO(jbudorick) Remove this once the underlying implementations | 409 # TODO(jbudorick) Remove this once the underlying implementations |
384 # for the above are switched or wrapped. | 410 # for the above are switched or wrapped. |
385 android_commands.errors.DeviceUnresponsiveError) as e: | 411 android_commands.errors.DeviceUnresponsiveError) as e: |
386 logging.warning('Device unresponsive during TearDown: [%s]', e) | 412 logging.warning('Device unresponsive during TearDown: [%s]', e) |
387 except Exception as e: | 413 except Exception as e: |
388 logging.error('Unexpected exception caught during TearDown: %s' % str(e)) | 414 logging.error('Unexpected exception caught during TearDown: %s' % str(e)) |
OLD | NEW |