aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Desprez <jdesprez@google.com>2016-07-05 08:42:11 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2016-07-05 08:42:11 +0000
commite8555ba4cd68a59ee0a3871dfb8d7b74e5b6f3f9 (patch)
tree938e4012a1c96e23f23ec0c1bd98b9832624faaa
parent8f9195f188896ceba98a4c6ed15a5665236db771 (diff)
parentcc92e1ee1d152c2b1c0410b7c5eedb858961326f (diff)
downloadtradefederation-e8555ba4cd68a59ee0a3871dfb8d7b74e5b6f3f9.tar.gz
Merge "Speed up some unit tests"
-rw-r--r--src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java10
-rw-r--r--tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java15
-rw-r--r--tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java19
3 files changed, 32 insertions, 12 deletions
diff --git a/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java b/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java
index e0076aa14..96ec06979 100644
--- a/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java
+++ b/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java
@@ -23,6 +23,7 @@ import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.util.CommandResult;
import com.android.tradefed.util.CommandStatus;
import com.android.tradefed.util.FileUtil;
+import com.android.tradefed.util.IRunUtil;
import com.android.tradefed.util.RunUtil;
import com.android.tradefed.util.ZipUtil;
@@ -565,7 +566,7 @@ public class FastbootDeviceFlasher implements IDeviceFlasher {
attempts++;
CLog.w("Could not find version for '%s'. Output '%s', retrying.",
imageName, queryOutput);
- RunUtil.getDefault().sleep(RETRY_SLEEP * (attempts - 1)
+ getRunUtil().sleep(RETRY_SLEEP * (attempts - 1)
+ new Random(System.currentTimeMillis()).nextInt(RETRY_SLEEP));
continue;
}
@@ -575,6 +576,13 @@ public class FastbootDeviceFlasher implements IDeviceFlasher {
}
/**
+ * Exposed for testing.
+ */
+ protected IRunUtil getRunUtil() {
+ return RunUtil.getDefault();
+ }
+
+ /**
* Helper method to execute fastboot command.
*
* @param device the {@link ITestDevice} to execute command on
diff --git a/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java b/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java
index cc8637736..953e86e7a 100644
--- a/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java
+++ b/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java
@@ -39,6 +39,9 @@ import java.util.Map;
public class ConfigurationFactoryTest extends TestCase {
private ConfigurationFactory mFactory;
+ // Create a real instance for tests that checks the content of our configs. making it static to
+ // reduce the runtime of reloading the config thanks to the caching of configurations.
+ private static ConfigurationFactory mRealFactory = new ConfigurationFactory();
/** the test config name that is built into this jar */
private static final String TEST_CONFIG = "test-config";
@@ -63,20 +66,18 @@ public class ConfigurationFactoryTest extends TestCase {
* Sanity test to ensure all config names on classpath are loadable
*/
public void testLoadAllConfigs() throws ConfigurationException {
- ConfigurationFactory cf = new ConfigurationFactory();
// we dry-run the templates otherwise it will always fail.
- cf.loadAllConfigs(false);
- assertTrue(cf.getMapConfig().size() > 0);
+ mRealFactory.loadAllConfigs(false);
+ assertTrue(mRealFactory.getMapConfig().size() > 0);
}
/**
* Sanity test to ensure all configs on classpath can be fully loaded and parsed
*/
public void testLoadAndPrintAllConfigs() throws ConfigurationException {
- ConfigurationFactory cf = new ConfigurationFactory();
// Printing the help involves more checks since it tries to resolve the config objects.
- cf.loadAndPrintAllConfigs();
- assertTrue(cf.getMapConfig().size() > 0);
+ mRealFactory.loadAndPrintAllConfigs();
+ assertTrue(mRealFactory.getMapConfig().size() > 0);
}
/**
@@ -316,7 +317,7 @@ public class ConfigurationFactoryTest extends TestCase {
public void testPrintHelp() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream mockPrintStream = new PrintStream(outputStream);
- new ConfigurationFactory().printHelp(mockPrintStream);
+ mRealFactory.printHelp(mockPrintStream);
// verify all the instrument config names are present
final String usageString = outputStream.toString();
assertTrue(usageString.contains("instrument"));
diff --git a/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java b/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java
index 3e29c9317..7d2700861 100644
--- a/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java
+++ b/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java
@@ -23,6 +23,7 @@ import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.targetprep.IDeviceFlasher.UserDataFlashOption;
import com.android.tradefed.util.CommandResult;
import com.android.tradefed.util.CommandStatus;
+import com.android.tradefed.util.IRunUtil;
import junit.framework.TestCase;
@@ -45,6 +46,7 @@ public class FastbootDeviceFlasherTest extends TestCase {
private IDeviceBuildInfo mMockBuildInfo;
private IFlashingResourcesRetriever mMockRetriever;
private IFlashingResourcesParser mMockParser;
+ private IRunUtil mMockRunUtil;
/**
* {@inheritDoc}
@@ -62,6 +64,7 @@ public class FastbootDeviceFlasherTest extends TestCase {
mMockBuildInfo.setUserDataImageFile(new File(TEST_STRING), "0");
mMockRetriever = EasyMock.createNiceMock(IFlashingResourcesRetriever.class);
mMockParser = EasyMock.createNiceMock(IFlashingResourcesParser.class);
+ mMockRunUtil = EasyMock.createMock(IRunUtil.class);
mFlasher = new FastbootDeviceFlasher() {
@Override
@@ -69,6 +72,10 @@ public class FastbootDeviceFlasherTest extends TestCase {
IDeviceBuildInfo localBuild) {
return mMockParser;
}
+ @Override
+ protected IRunUtil getRunUtil() {
+ return mMockRunUtil;
+ }
};
mFlasher.setFlashingResourcesRetriever(mMockRetriever);
mFlasher.setUserDataFlashOption(UserDataFlashOption.RETAIN);
@@ -120,9 +127,10 @@ public class FastbootDeviceFlasherTest extends TestCase {
fastbootResult.setStdout("");
EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-bootloader")).
andReturn(fastbootResult);
- EasyMock.replay(mMockDevice);
+ EasyMock.replay(mMockDevice, mMockRunUtil);
String actualVersion = mFlasher.getImageVersion(mMockDevice, "bootloader");
assertEquals("1.0.1", actualVersion);
+ EasyMock.verify(mMockDevice, mMockRunUtil);
}
/**
@@ -146,9 +154,12 @@ public class FastbootDeviceFlasherTest extends TestCase {
andReturn(fastbootInValidResult);
EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-baseband")).
andReturn(fastbootValidResult);
+ mMockRunUtil.sleep(EasyMock.anyLong());
+ EasyMock.expectLastCall();
- EasyMock.replay(mMockDevice);
+ EasyMock.replay(mMockDevice, mMockRunUtil);
String actualVersion = mFlasher.getImageVersion(mMockDevice, "baseband");
+ EasyMock.verify(mMockDevice, mMockRunUtil);
assertEquals("1.0.1", actualVersion);
}
@@ -165,7 +176,7 @@ public class FastbootDeviceFlasherTest extends TestCase {
// expect a 'flash radio' command
setFastbootFlashExpectations(mockDevice, "radio");
mockDevice.rebootIntoBootloader();
- EasyMock.replay(mockDevice);
+ EasyMock.replay(mockDevice, mMockRunUtil);
FastbootDeviceFlasher flasher = getFlasherWithParserData(
String.format("require version-baseband=%s", newBasebandVersion));
@@ -173,7 +184,7 @@ public class FastbootDeviceFlasherTest extends TestCase {
IDeviceBuildInfo build = new DeviceBuildInfo("1234", "target", "build-name");
build.setBasebandImage(new File("tmp"), newBasebandVersion);
flasher.checkAndFlashBaseband(mockDevice, build);
- EasyMock.verify(mockDevice);
+ EasyMock.verify(mockDevice, mMockRunUtil);
}
/**