Merge "Don't be overly aggressive on USB reset invocation"
diff --git a/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecovery.java b/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecovery.java
index 3cddff9..c683e27 100644
--- a/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecovery.java
+++ b/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecovery.java
@@ -18,12 +18,27 @@
import com.android.tradefed.config.OptionClass;
import com.android.tradefed.device.IManagedTestDevice;
+import java.util.HashSet;
+import java.util.Set;
+
/** Allow to trigger a command to reset the USB of a device */
@OptionClass(alias = "usb-reset-recovery")
public class UsbResetRunConfigRecovery extends RunConfigDeviceRecovery {
+ private Set<String> mLastInvoked = new HashSet<>();
+
@Override
public boolean shouldSkip(IManagedTestDevice device) {
- return device.isStateBootloaderOrFastbootd();
+ boolean res = device.isStateBootloaderOrFastbootd();
+ if (!res) {
+ String serial = device.getSerialNumber();
+ // Avoid running the same device twice in row of recovery request to give a chance to
+ // others recovery to start too.
+ if (!mLastInvoked.add(serial)) {
+ mLastInvoked.remove(serial);
+ return true;
+ }
+ }
+ return res;
}
}
diff --git a/tests/src/com/android/tradefed/UnitTests.java b/tests/src/com/android/tradefed/UnitTests.java
index 101caa8..c32e831 100644
--- a/tests/src/com/android/tradefed/UnitTests.java
+++ b/tests/src/com/android/tradefed/UnitTests.java
@@ -122,6 +122,7 @@
import com.android.tradefed.device.recovery.BatteryUnavailableDeviceRecoveryTest;
import com.android.tradefed.device.recovery.RunConfigDeviceRecoveryTest;
import com.android.tradefed.device.recovery.UsbResetMultiDeviceRecoveryTest;
+import com.android.tradefed.device.recovery.UsbResetRunConfigRecoveryTest;
import com.android.tradefed.guice.InvocationScopeTest;
import com.android.tradefed.host.LocalHostResourceManagerTest;
import com.android.tradefed.host.gcs.GCSHostResourceManagerTest;
@@ -542,6 +543,7 @@
BatteryUnavailableDeviceRecoveryTest.class,
RunConfigDeviceRecoveryTest.class,
UsbResetMultiDeviceRecoveryTest.class,
+ UsbResetRunConfigRecoveryTest.class,
// Guice
InvocationScopeTest.class,
diff --git a/tests/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecoveryTest.java b/tests/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecoveryTest.java
new file mode 100644
index 0000000..d7447fa
--- /dev/null
+++ b/tests/src/com/android/tradefed/device/recovery/UsbResetRunConfigRecoveryTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tradefed.device.recovery;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+
+import com.android.tradefed.device.IManagedTestDevice;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.Mockito;
+
+/** Unit tests for {@link UsbResetRunConfigRecovery}. */
+@RunWith(JUnit4.class)
+public class UsbResetRunConfigRecoveryTest {
+
+ private UsbResetRunConfigRecovery mUsbReset = new UsbResetRunConfigRecovery();
+
+ /**
+ * Test that should skip can only return false once, it should attempt the usb reset twice in a
+ * row for the same device.
+ */
+ @Test
+ public void testShouldSkip() {
+ IManagedTestDevice mockDevice = Mockito.mock(IManagedTestDevice.class);
+ doReturn("serial").when(mockDevice).getSerialNumber();
+ doReturn(false).when(mockDevice).isStateBootloaderOrFastbootd();
+ boolean res = mUsbReset.shouldSkip(mockDevice);
+ assertFalse(res);
+ res = mUsbReset.shouldSkip(mockDevice);
+ assertTrue(res);
+ res = mUsbReset.shouldSkip(mockDevice);
+ assertFalse(res);
+ }
+}