Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit ccf02a2

Browse files
committed
增加方法运行超时机制
1 parent 4e42155 commit ccf02a2

1 file changed

Lines changed: 68 additions & 4 deletions

File tree

src/main/java/org/suren/autotest/web/framework/core/suite/SuiteRunner.java

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
import java.util.Enumeration;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.concurrent.ExecutionException;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Executors;
24+
import java.util.concurrent.Future;
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.concurrent.TimeoutException;
2127

2228
import org.dom4j.DocumentException;
2329
import org.slf4j.Logger;
@@ -32,6 +38,7 @@
3238
import org.suren.autotest.web.framework.settings.SettingUtil;
3339
import org.suren.autotest.web.framework.settings.SuiteParser;
3440
import org.suren.autotest.web.framework.util.StringUtils;
41+
import org.suren.autotest.web.framework.util.ThreadUtil;
3542
import org.suren.autotest.web.framework.validation.Validation;
3643
import org.xml.sax.SAXException;
3744

@@ -66,6 +73,7 @@ public static void main(String[] args) throws NoSuchFieldException, SecurityExce
6673
}
6774

6875
private ProgressInfo<String> progressInfo;
76+
private ExecutorService executor = Executors.newSingleThreadExecutor();
6977

7078
public SuiteRunner()
7179
{
@@ -413,22 +421,78 @@ else if(!(pageObj instanceof Page))
413421
catch (NoSuchFieldException e)
414422
{
415423
e.printStackTrace();
424+
425+
throw new RuntimeException(String.format("Can not found field [%s] from class [%].", field, targetPage));
416426
}
417427

418-
Thread.sleep(action.getBeforeSleep());
428+
//防止一个任务长期执行
429+
PerformAction performAction = new PerformAction(action, pageField,
430+
targetPage, settingUtil, progressInfo);
431+
Future<?> future = executor.submit(performAction);
432+
433+
try
434+
{
435+
future.get(1, TimeUnit.MINUTES);
436+
}
437+
catch (ExecutionException | TimeoutException e)
438+
{
439+
e.printStackTrace();
440+
441+
future.cancel(true);
442+
}
443+
}
444+
}
445+
446+
private class PerformAction implements Runnable
447+
{
448+
private SuiteAction action;
449+
private Field pageField;
450+
private Page targetPage;
451+
private SettingUtil settingUtil;
452+
private ProgressInfo<String> progressInfo;
453+
454+
private PerformAction(SuiteAction action, Field pageField, Page targetPage,
455+
SettingUtil settingUtil, ProgressInfo<String> progressInfo)
456+
{
457+
this.action = action;
458+
this.pageField = pageField;
459+
this.targetPage = targetPage;
460+
this.settingUtil = settingUtil;
461+
this.progressInfo = progressInfo;
462+
}
463+
464+
@Override
465+
public void run()
466+
{
467+
if(action.getBeforeSleep() > 0)
468+
{
469+
ThreadUtil.silentSleep(action.getBeforeSleep());
470+
}
419471

420472
int repeat = action.getRepeat();
421473

422474
for(int i = 0; i < repeat; i++)
423475
{
424-
// settingUtil.initPageData(targetPage, 1);
476+
String actionResult;
425477

426-
String actionResult = performAction(action, pageField, targetPage, settingUtil);
478+
try
479+
{
480+
actionResult = performAction(action, pageField, targetPage, settingUtil);
481+
}
482+
catch (IllegalArgumentException | IllegalAccessException e)
483+
{
484+
e.printStackTrace();
485+
486+
actionResult = e.getLocalizedMessage();
487+
}
427488

428489
progressInfo.setInfo(String.format("Action result : %s.", actionResult));
429490
}
430491

431-
Thread.sleep(action.getAfterSleep());
492+
if(action.getAfterSleep() > 0)
493+
{
494+
ThreadUtil.silentSleep(action.getAfterSleep());
495+
}
432496
}
433497
}
434498

0 commit comments

Comments
 (0)