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, 25 device_errors.CommandTimeoutError, 26 device_errors.DeviceUnreachableError): 27 pass 28 with _devices_lock: 29 _devices.update(set(live_devices)) 30 31 if not _devices: 32 raise Exception('No live devices attached.') 33 34 35class DeviceTestCase(unittest.TestCase): 36 37 def __init__(self, *args, **kwargs): 38 super(DeviceTestCase, self).__init__(*args, **kwargs) 39 self.serial = None 40 41 #override 42 def setUp(self): 43 super(DeviceTestCase, self).setUp() 44 with _devices_lock: 45 while not _devices: 46 _devices_condition.wait(5) 47 self.serial = _devices.pop() 48 49 #override 50 def tearDown(self): 51 super(DeviceTestCase, self).tearDown() 52 with _devices_lock: 53 _devices.add(self.serial) 54 _devices_condition.notify() 55