Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_locked_lock(self):
"""Test that the ``FakeLock`` class works as expected.
"""
with patch.object(self.adb, '_adb_lock', FakeLock()):
with _acquire(self.adb._adb_lock):
with self.assertRaises(LockNotAcquiredException):
with _acquire(self.adb._adb_lock):
pass
with _acquire(self.adb._adb_lock) as acquired:
self.assertTrue(acquired)
def test_adb_shell_fail(self):
"""Test when an ADB shell command is not sent because the device is unavailable.
"""
self.assertFalse(self.adb.available)
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
self.assertIsNone(self.adb.shell("TEST"))
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(self.adb.connect())
with patch.object(self.adb, '_adb_lock', LockedLock()):
with self.assertRaises(LockNotAcquiredException):
self.adb.shell("TEST")
with self.assertRaises(LockNotAcquiredException):
self.adb.shell("TEST2")
async def test_adb_screencap_lock_not_acquired(self):
"""Test when an ADB screencap command fails because the ADB lock could not be acquired.
"""
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertEqual(await self.adb.shell("TEST"), "TEST")
with async_patchers.patch_shell(PNG_IMAGE)[self.PATCH_KEY], patch.object(self.adb, '_adb_lock', AsyncLockedLock()):
with patch('{}.AsyncLockedLock.release'.format(__name__)) as release:
with self.assertRaises(LockNotAcquiredException):
await self.adb.screencap()
release.assert_not_called()
async def test_fail_timeout(self):
lock = AsyncTimedLock()
with self.assertRaises(LockNotAcquiredException):
async with _acquire(lock, 0.1):
pass #self.assertTrue(False)
def _adb_exception_catcher(self, *args, **kwargs):
"""Call an ADB-related method and catch exceptions."""
if not self.available and not override_available:
return None
try:
return func(self, *args, **kwargs)
except LockNotAcquiredException:
# If the ADB lock could not be acquired, skip this command
return
except self.exceptions as err:
_LOGGER.error(
"Failed to execute an ADB command. ADB connection re-"
"establishing attempt in the next update. Error: %s",
err,
)
self.aftv.adb_close()
self._available = False # pylint: disable=protected-access
return None
def test_update_lock_not_acquired(self):
"""Test that the state does not get updated when a `LockNotAcquiredException` is raised."""
patch_key = "server"
with patchers.patch_connect(True)[patch_key], patchers.patch_shell("")[patch_key]:
aftv = setup(
"HOST", 5555, adb_server_ip="ADB_SERVER_IP", device_class="androidtv"
)
self.aftv = AndroidTVDevice(aftv, "Fake Android TV", {}, True, None, None)
with patchers.patch_shell("")[patch_key]:
self.aftv.update()
assert self.aftv.state == STATE_OFF
with patch("androidtv.androidtv.androidtv_sync.AndroidTVSync.update", side_effect=LockNotAcquiredException):
with patchers.patch_shell("1")[patch_key]:
self.aftv.update()
assert self.aftv.state == STATE_OFF
with patchers.patch_shell("1")[patch_key]:
self.aftv.update()
assert self.aftv.state == STATE_STANDBY
Yields
------
acquired : bool
Whether or not the lock was acquired
Raises
------
LockNotAcquiredException
Raised if the lock was not acquired
"""
try:
acquired = lock.acquire(**LOCK_KWARGS)
if not acquired:
raise LockNotAcquiredException
yield acquired
finally:
if acquired:
lock.release()
# ADB connection attempt failed
await self.close()
self._available = False
return False
except Exception as exc: # pylint: disable=broad-except
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to %s:%d. %s: %s", self.host, self.port, exc.__class__.__name__, exc)
# ADB connection attempt failed
await self.close()
self._available = False
return False
except LockNotAcquiredException:
_LOGGER.warning("Couldn't connect to %s:%d because adb-shell lock not acquired.", self.host, self.port)
await self.close()
self._available = False
return False