-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyReRunConfig.java
79 lines (70 loc) · 2.77 KB
/
MyReRunConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.demoqa.util.listeners;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.testng.IConfigurable;
import org.testng.IConfigureCallBack;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
public class MyReRunConfig implements IConfigurable {
private static final int MAX_RETRIES = 2;
private static final int MAX_WAIT_TIME = 60; // 120 segundos
@Override
public void run(IConfigureCallBack callBack, ITestResult testResult) {
callBack.runConfigurationMethod(testResult);
if (testResult.getThrowable() != null) {
for (int i = 0; i < MAX_RETRIES; i++) {
System.err.println("Retrying " + parseType(testResult) + " method : " +
testResult.getMethod().getQualifiedName());
Future<?> future = scheduleRetry(callBack, testResult);
try {
future.get(MAX_WAIT_TIME, TimeUnit.SECONDS);
} catch (TimeoutException e) {
System.err.println("Retry timed out. Cancelling retry.");
future.cancel(true);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
private Future<?> scheduleRetry(IConfigureCallBack callBack, ITestResult testResult) {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
return executorService.schedule(() -> callBack.runConfigurationMethod(testResult), MAX_WAIT_TIME,
TimeUnit.SECONDS);
}
private static String parseType(ITestResult testResult) {
ITestNGMethod method = testResult.getMethod();
if (method.isBeforeSuiteConfiguration()) {
return "@BeforeSuite";
}
if (method.isAfterSuiteConfiguration()) {
return "@AfterSuite";
}
if (method.isBeforeTestConfiguration()) {
return "@BeforeTest";
}
if (method.isAfterTestConfiguration()) {
return "@AfterTest";
}
if (method.isBeforeGroupsConfiguration()) {
return "@BeforeGroup";
}
if (method.isAfterGroupsConfiguration()) {
return "@AfterGroup";
}
if (method.isBeforeClassConfiguration()) {
return "@BeforeClass";
}
if (method.isAfterClassConfiguration()) {
return "@AfterClass";
}
if (method.isBeforeMethodConfiguration()) {
return "@BeforeMethod";
}
return "@AfterMethod";
}
}