清单 1. queuemgr.xml 中的 check-queue 函数
<function name="check-queue">
<sequence>
<block name="'Queue Manager'">
<sequence>
<stafcmd name="'Check Queue'">
<location>'local'</location>
<service>'FS'</service>
<request>'LIST DIRECTORY %s TYPE F SORTBYNAME' % queueDir</request>
</stafcmd>
<script>dirList = STAFResult</script>
<script>taskCount = len(dirList)</script>
<script>
if (taskCount != 0): # skip an empty list
taskParam = dirList[0] # get first entry
else:
taskParam = "empty"
</script>
<if expr=" taskParam != 'empty'">
<if expr=" lastTaskParam != taskParam">
<sequence>
<if expr="TestRunInProgress[0] == 0">
<script>
TestRunInProgress[0] = 1
TestRunStartTime[0] = now()
</script>
</if>
<script>TestRunCtr[0] = TestRunCtr[0] + 1</script>
<script> lastTaskParam = taskParam </script>
<script> lastTaskParamJobID = taskParamJobID </script>
<log>'QueueMgr: Executing taskParam %s' %(taskParam)</log>
<stafcmd name="'Executing Task: %s' % taskParam">
<location>'local'</location>
<service>'STAX'</service>
<request>'EXECUTE FILE %s CLEARLOGS JOBNAME %s SCRIPT " taskParam='%s'" WAIT
%s' %(TestExecuteFile, taskParam, taskParam,gblTestExecutionTimeoutMS)</request>
</stafcmd>
<script> taskParamJobID = STAFResult</script>
<log>'QueueMgr: taskParam %s JobID = %s' %( taskParam, taskParamJobID)</log>
<if expr="RC != STAFRC.Ok">
<sequence>
<log>'QueueMgr: Execute Test with Param File %s failed - RC: %s STAFResult: %s'
%( taskParam,RC,STAFResult)</log>
<terminate block="'main'"/>
</sequence>
</if>
</sequence>
<else>
<sequence>
<log>'QueueMgr: *** TERMINATING Queue Manager *** Task File %s is the same as the
last one' %( taskParam)</log>
<log>'QueueMgr: Please check the completion status of Test Record %s Job ID %s
and Re-Start the Queue Manager' %( lastTaskParam, lastTaskParamJobID)</log>
<terminate block="'main'"/>
</sequence>
</else>
</if>
<else>
<sequence>
<if expr="TestRunInProgress[0] == 1">
<sequence>
<script>
TestRunInProgress[0] = 0
TestRunStopTime[0] = now()
testTime = (TestRunStopTime[0] - TestRunStartTime[0])
testHrs = int((testTime) / 3600)
testMin = int((testTime - (testHrs * 3600)) / 60)
testSec = int((testTime - (testHrs * 3600)) - (testMin * 60))
TestRunCompletionDateTime = strftime('%a %b %d %Y %H:%M:%S', localtime())
</script>
<log>'All tests have been run; the testqueue is empty'</log>
<log>'Test Run Completed: %s' % TestRunCompletionDateTime</log>
<log>'%d Test Tasks Were Completed In %d Hrs %d Min %d Sec'
% (TestRunCtr[0],testHrs,testMin,testSec)</log>
<script>TestRunCtr[0] = 0</script>
<terminate block="'main'"/>
</sequence>
</if>
<block name="'Queue Check Delay'">
<stafcmd name="'Checking TestQueue Every 5000 Sec' % gblCheckQueueDelayTimeSec">
<location>'local'</location>
<service>'DELAY'</service>
<request>'DELAY 5000'</request>
</stafcmd>
</block>
</sequence>
</else>
</if>
<block name="'Queue Process Delay'">
<stafcmd name="'Checking TestQueue 5000 Sec After Last Task'">
<location>'local'</location>
<service>'DELAY'</service>
<request>'DELAY 5000'</request>
</stafcmd>
</block>
</sequence>
</block>
</sequence>
</function>
代码中第一个延时等待是对空任务队列的查看周期。测试执行人员根据个人习惯有时候希望先启动任务队列执行任务,再生成测试任务参数文件。这时查看队列的循环不能在第一次遇到队列为空时就退出,而是要不断反复查看任务队列。如果在循环之间不加延时,会造成进程对 CPU 开销过大,影响其他进程。第二个延时是前后两个测试任务执行之间的延时。这是为了让刚结束的测试进程有足够时间释放各种资源和执行多个文件操作,如日志文件和测试参数文件的转移。
每次循环中获得测试任务文件列表时要判断第一个任务参数文件是否在上次循环中已经尝试执行过。若是,则上次任务肯定没有正常结束,否则应该将该任务文件转移到任务结果文件夹中。这时应该中止任务队列处理进程,提示测试执行人员查看 STAX 日志排除错误。
生成任务参数文件并启动任务队列执行的 PHP 代码如下。该部分代码在测试者提交了图 3 所示的测试任务配置表单后执行。
文章来源于领测软件测试网 https://www.ltesting.net/