1# Copyright 2016 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
5import threading
6import unittest
7
8from devil.android import device_errors
9from devil.android import device_utils
10
11_devices_lock = threading.Lock()
12_devices_condition = threading.Condition(_devices_lock)
13_devices = set()
14
15
16def PrepareDevices(*_args):
17
18  raw_devices = device_utils.DeviceUtils.HealthyDevices()
19  live_devices = []
20  for d in raw_devices:
21    try:
22      d.WaitUntilFullyBooted(timeout=5, retries=0)
23      live_devices.append(str(d))
24    except (device_errors.CommandFailedError, device_errors.CommandTimeoutError,
25            device_errors.DeviceUnreachableError):
26      pass
27  with _devices_lock:
28    _devices.update(set(live_devices))
29
30  if not _devices:
31    raise Exception('No live devices attached.')
32
33
34class DeviceTestCase(unittest.TestCase):
35  def __init__(self, *args, **kwargs):
36    super(DeviceTestCase, self).__init__(*args, **kwargs)
37    self.serial = None
38
39  #override
40  def setUp(self):
41    super(DeviceTestCase, self).setUp()
42    with _devices_lock:
43      while not _devices:
44        _devices_condition.wait(5)
45      self.serial = _devices.pop()
46
47  #override
48  def tearDown(self):
49    super(DeviceTestCase, self).tearDown()
50    with _devices_lock:
51      _devices.add(self.serial)
52      _devices_condition.notify()
53