目录
- 整体流程概览
- Main
- Console
- 总结
从这篇文章往后开始介绍整个框架的运行流程原理。
1.整体流程概览
这里是整个测试框架的大纲流程图,其中主要涉及到四个线程:
1. main – 启动入口
2. Console – 处理命令
3. CommandScheduler – 命令调度
4. InvocationThrad – 执行命令
这个图是整体运行的大纲流程,可以先看下有个大致的认识,等后面看完了每个细节再回来重新捋一遍。
2.main
整个测试框架作为一个java程序,在eclipse中可以直接运行,入口在com.android.tradefed.command包下的Console中:
public static void main(final String[] mainArgs) throws InterruptedException,
ConfigurationException {
Console console = new Console();
startConsole(console, mainArgs);
}
可以看到这里做的事情很简单,在main线程中启动了一个console,那么这个Console到底是什么呢?
protected Console() {
this(getReader());
}
// 构造方法中初始化了一系列变量
Console(ConsoleReader reader) {
super("TfConsole");
mConsoleStartTime = System.currentTimeMillis();
mConsoleReader = reader;
if (reader != null) {
mConsoleReader.addCompletor(
new ConfigCompletor(getConfigurationFactory().getConfigList()));
}
// HelpList初始化
List<String> genericHelp = new LinkedList<String>();
// helpString:放入每个command支持的使用方式
Map<String, String> commandHelp = new LinkedHashMap<String, String>();
// 添加默认支持的命令,使用的就是前面介绍的RegexTrie
addDefaultCommands(mCommandTrie, genericHelp, commandHelp);
// 这个是个空方法,主要是为了方便子类复写添加自己的命令
setCustomCommands(mCommandTrie, genericHelp, commandHelp);
// 生成HelpList
generateHelpListings(mCommandTrie, genericHelp, commandHelp);
}
public static void startConsole(Console console, String[] args) throws InterruptedException,
ConfigurationException {
// 创建GlobalConfiguration
List<String> nonGlobalArgs = GlobalConfiguration.createGlobalConfiguration(args);
console.setArgs(nonGlobalArgs);
// 创建命令调度器
console.setCommandScheduler(GlobalConfiguration.getInstance().getCommandScheduler());
console.setKeyStoreFactory(GlobalConfiguration.getInstance().getKeyStoreFactory());
将console线程设为daemon线程
console.setDaemon(true);
// 启动console线程
console.start();
// Wait for the CommandScheduler to get started before we exit the main thread. See full
// explanation near the top of #run()
// 等待CommandSchedler启动,然后退出
console.awaitScheduler();
}
上面主要作用就是初始化了一些配置,然后启动了console线程,下面来看下这个GlobalConfiguration
public static List<String> createGlobalConfiguration(String[] args)
throws ConfigurationException {
synchronized (sInstanceLock) {
if (sInstance != null) {