|
18 | 18 | import java.util.Enumeration; |
19 | 19 | import java.util.List; |
20 | 20 | 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; |
21 | 27 |
|
22 | 28 | import org.dom4j.DocumentException; |
23 | 29 | import org.slf4j.Logger; |
|
32 | 38 | import org.suren.autotest.web.framework.settings.SettingUtil; |
33 | 39 | import org.suren.autotest.web.framework.settings.SuiteParser; |
34 | 40 | import org.suren.autotest.web.framework.util.StringUtils; |
| 41 | +import org.suren.autotest.web.framework.util.ThreadUtil; |
35 | 42 | import org.suren.autotest.web.framework.validation.Validation; |
36 | 43 | import org.xml.sax.SAXException; |
37 | 44 |
|
@@ -66,6 +73,7 @@ public static void main(String[] args) throws NoSuchFieldException, SecurityExce |
66 | 73 | } |
67 | 74 |
|
68 | 75 | private ProgressInfo<String> progressInfo; |
| 76 | + private ExecutorService executor = Executors.newSingleThreadExecutor(); |
69 | 77 |
|
70 | 78 | public SuiteRunner() |
71 | 79 | { |
@@ -413,22 +421,78 @@ else if(!(pageObj instanceof Page)) |
413 | 421 | catch (NoSuchFieldException e) |
414 | 422 | { |
415 | 423 | e.printStackTrace(); |
| 424 | + |
| 425 | + throw new RuntimeException(String.format("Can not found field [%s] from class [%].", field, targetPage)); |
416 | 426 | } |
417 | 427 |
|
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 | + } |
419 | 471 |
|
420 | 472 | int repeat = action.getRepeat(); |
421 | 473 |
|
422 | 474 | for(int i = 0; i < repeat; i++) |
423 | 475 | { |
424 | | -// settingUtil.initPageData(targetPage, 1); |
| 476 | + String actionResult; |
425 | 477 |
|
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 | + } |
427 | 488 |
|
428 | 489 | progressInfo.setInfo(String.format("Action result : %s.", actionResult)); |
429 | 490 | } |
430 | 491 |
|
431 | | - Thread.sleep(action.getAfterSleep()); |
| 492 | + if(action.getAfterSleep() > 0) |
| 493 | + { |
| 494 | + ThreadUtil.silentSleep(action.getAfterSleep()); |
| 495 | + } |
432 | 496 | } |
433 | 497 | } |
434 | 498 |
|
|
0 commit comments