Cts框架解析(6)-任务的运行

前两篇讲了任务的加入和9大项配置,这篇讲任务的运行。


任务的运行


任务的运行在CommandScheduler的run方法中,所以删除全部的断点,在run方法中打上断点,重新启动启动debug:




先看while循环以下的第一行代码


ExecutableCommand cmd = dequeueConfigCommand();

private ExecutableCommand dequeueConfigCommand() {
        try {
            // poll for a command, rather than block indefinitely, to handle shutdown case
            return mCommandQueue.poll(getCommandPollTimeMs(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            CLog.i("Waiting for command interrupted");
        }
        return null;
    }

从队列中取出第一个对象。假设队列中没有元素那就返回null。返回后,while中会推断假设为null的话,就会结束再次调用


ExecutableCommand cmd = dequeueConfigCommand();


直到cmd不为null。

所以在


IDeviceSelection options = cmd.getConfiguration().getDeviceRequirements();


打上断点,按F8,程序会在cmd不为null时进入到上面的代码,停下来。




首先得到系统设备要求,依据该要求,推断是否有满足要求的设备并分配该设备。



原生的 要求就一个S/N号,ddms会依据该SN号分配一个IDevice,然后cts依据IDevice包装成ITestDevice对象返回。然后依据ITestDevice,IDeviceManager和ExecutableCommand開始真正的启动任务。

我们先不急于讨论运行的过程。而是继续向下看:


addInvocationThread(invThread);
if (cmd.isLoopMode()) {
    addNewExecCommandToQueue(cmd.getCommandTracker());
}

先将正在运行的线程存到set中,然后将该命令再次放入任务队列中。这种目的是为了能循环运行该任务。假设依据sn号没有分配成功ITestDevice,则会再次尝试一次,假设在规定时间内还没找到设备则放弃。

最后做一些tearDown操作。然后将case运行过程中的log保存到文件。就算运行完了。


如今回头看一下运行任务的线程中是怎样操作的:


private InvocationThread startInvocation(IDeviceManager manager, ITestDevice device,
            ExecutableCommand cmd) {
        final String invocationName = String.format("Invocation-%s", device.getSerialNumber());
        InvocationThread invocationThread = new InvocationThread(invocationName, manager, device,
                cmd);
        invocationThread.start();
        return invocationThread;
    }

InvocationThread为CommandScheduler私有内部类。继承与线程,这就属于线程里启动线程。所以直接看InvocationThread的run方法就ok了。在run方法里调用了ITestInvocation的invoke方法:




传入的对象分别为ITestDevice、IConfiguration、IRescheduler。前两个已经涉及到,最后一个IRescheduler是什么?自己看吧,也没啥意义。这样任务的前期的工作都已经搞定了。程序转到了TestInvocation的invoke方法。放到下一篇文章来讲,由于它非常重要,相当于一个调度室的作用。


总结


写了6篇,总结一些cts的过程。


任务的開始点是接受命令行中的參数。假设推断为运行cts任务,那么会在Console.run方法中启动加入命令任务的线程ArgRunnable<CaptureList>和运行任务的线程CommandScheduler。加入任务线程ArgRunnable<CaptureList>会调用CommandScheduler对象方法addCommand来向ConditionPriorityBlockingQueue<ExecutableCommand>(採用先进先出的队列)这个队列中加入。而运行任务线程CommandScheduler会通过调用自己的私有方法dequeueConfigCommand来从

ConditionPriorityBlockingQueue<ExecutableCommand>去取可运行的任务,每次取第一个。

然后调用InvocationThread这个线程去运行任务。

InvocationThread这个线程调用TestInvocation.invoke方法去运行任务。


明确了么。事实上非常easy。大哭







posted @ 2017-05-15 18:40  wzjhoutai  阅读(354)  评论(0编辑  收藏  举报