Skip to content

Commit 2152160

Browse files
committed
fixes #290
1 parent 1d49e39 commit 2152160

File tree

2 files changed

+80
-32
lines changed

2 files changed

+80
-32
lines changed

src/main/java/com/aventstack/extentreports/model/Report.java

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
@ToString(includeFieldNames = true)
2626
public class Report implements Serializable, BaseEntity {
2727
private static final long serialVersionUID = -8667496631392333349L;
28-
29-
private final Object obj = new Object();
3028

3129
@Builder.Default
3230
private Date startTime = Calendar.getInstance().getTime();
@@ -47,66 +45,93 @@ public final void refresh() {
4745
authorCtx.getSet().forEach(x -> x.refresh());
4846
categoryCtx.getSet().forEach(x -> x.refresh());
4947
deviceCtx.getSet().forEach(x -> x.refresh());
50-
stats.update(testList);
51-
synchronized (obj) {
48+
synchronized (testList) {
49+
stats.update(testList);
5250
setEndTime(Calendar.getInstance().getTime());
5351
}
5452
}
53+
54+
public void addTest(Test test) {
55+
testList.add(test);
56+
}
57+
58+
public boolean removeTest(Test test) {
59+
synchronized (testList) {
60+
return removeTest(testList, test);
61+
}
62+
}
5563

64+
private boolean removeTest(List<Test> testList, Test test) {
65+
boolean removed = testList.removeIf(x -> x.getId() == test.getId());
66+
if (!removed)
67+
testList.forEach(x -> removeTest(x.getChildren(), test));
68+
return removed;
69+
}
70+
5671
public final void applyOverrideConf() {
57-
Date min = testList.stream()
58-
.map(t -> t.getStartTime())
59-
.min(Date::compareTo)
60-
.get();
61-
Date max = testList.stream()
62-
.map(t -> t.getEndTime())
63-
.max(Date::compareTo)
64-
.get();
65-
synchronized (obj) {
72+
synchronized (testList) {
73+
Date min = testList.stream()
74+
.map(t -> t.getStartTime())
75+
.min(Date::compareTo)
76+
.get();
77+
Date max = testList.stream()
78+
.map(t -> t.getEndTime())
79+
.max(Date::compareTo)
80+
.get();
6681
setStartTime(min);
6782
setEndTime(max);
6883
}
6984
}
7085

7186
public final boolean isBDD() {
72-
return !testList.isEmpty() && testList.stream().allMatch(Test::isBDD);
87+
synchronized (testList) {
88+
return !testList.isEmpty() && testList.stream().allMatch(Test::isBDD);
89+
}
7390
}
7491

7592
public final boolean anyTestHasStatus(Status status) {
76-
return testList.stream()
93+
synchronized (testList) {
94+
return testList.stream()
7795
.anyMatch(x -> x.getStatus() == status);
96+
}
7897
}
7998

8099
public Optional<Test> findTest(List<Test> list, String name) {
81-
Optional<Test> test = list.stream().filter(x -> x.getName().equals(name)).findFirst();
82-
if (!test.isPresent())
83-
for (Test t : list)
84-
return findTest(t.getChildren(), name);
85-
return test;
100+
synchronized (testList) {
101+
Optional<Test> test = list.stream().filter(x -> x.getName().equals(name)).findFirst();
102+
if (!test.isPresent())
103+
for (Test t : list)
104+
return findTest(t.getChildren(), name);
105+
return test;
106+
}
86107
}
87108

88109
public List<ExceptionInfo> aggregateExceptions(List<Test> testList) {
89-
List<ExceptionInfo> list = new ArrayList<>();
90-
for (Test test : testList) {
91-
list.addAll(test.aggregateExceptions());
92-
if (!test.getChildren().isEmpty())
93-
aggregateExceptions(test.getChildren());
110+
synchronized (testList) {
111+
List<ExceptionInfo> list = new ArrayList<>();
112+
for (Test test : testList) {
113+
list.addAll(test.aggregateExceptions());
114+
if (!test.getChildren().isEmpty())
115+
aggregateExceptions(test.getChildren());
116+
}
117+
return list;
94118
}
95-
return list;
96119
}
97120

98121
public final long timeTaken() {
99122
return endTime.getTime() - startTime.getTime();
100123
}
101124

102125
public final Status getStatus() {
103-
List<Status> list = testList
126+
synchronized (testList) {
127+
List<Status> list = testList
104128
.stream()
105129
.map(x -> x.getStatus())
106130
.collect(Collectors.toList());
107-
Status s = Status.max(list);
108-
if (s == Status.SKIP && anyTestHasStatus(Status.PASS))
109-
s = Status.PASS;
110-
return s;
131+
Status s = Status.max(list);
132+
if (s == Status.SKIP && anyTestHasStatus(Status.PASS))
133+
s = Status.PASS;
134+
return s;
135+
}
111136
}
112137
}

src/test/java/com/aventstack/extentreports/entity/ReportStatsConcurrentTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.testng.annotations.Test;
77

88
import com.aventstack.extentreports.ExtentReports;
9+
import com.aventstack.extentreports.ExtentTest;
910

1011
public class ReportStatsConcurrentTest {
1112

@@ -17,12 +18,34 @@ public class ReportStatsConcurrentTest {
1718
* https://github.com/extent-framework/extentreports-java/issues/250
1819
*/
1920
@Test
20-
public void concurrentUpdateFlush() {
21+
public void concurrentAddUpdateFlush() {
2122
ExtentReports extent = new ExtentReports();
2223
IntStream.range(0, 100).parallel().forEach(x -> {
2324
extent.createTest(String.valueOf(x)).pass("");
2425
extent.flush();
2526
});
2627
Assert.assertEquals(100, extent.getReport().getTestList().size());
2728
}
29+
30+
@Test
31+
public void concurrentAddRemoveUpdateFlush() {
32+
ExtentReports extent = new ExtentReports();
33+
IntStream.range(0, 100).parallel().forEach(x -> {
34+
extent.createTest(String.valueOf(x)).pass("");
35+
extent.removeTest(String.valueOf(x));
36+
extent.flush();
37+
});
38+
Assert.assertEquals(0, extent.getReport().getTestList().size());
39+
}
40+
41+
@Test
42+
public void concurrentAddModifyUpdateFlush() {
43+
ExtentReports extent = new ExtentReports();
44+
IntStream.range(0, 100).parallel().forEach(x -> {
45+
ExtentTest test = extent.createTest(String.valueOf(x)).pass("");
46+
test.createNode("Node").pass("");
47+
extent.flush();
48+
});
49+
Assert.assertEquals(100, extent.getReport().getTestList().size());
50+
}
2851
}

0 commit comments

Comments
 (0)