Skip to content

Commit 3050ca3

Browse files
committed
fixes #250
1 parent b814f87 commit 3050ca3

File tree

4 files changed

+62
-29
lines changed

4 files changed

+62
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ deploycmd.txt
4040

4141
#
4242
pom.nexus.xml
43+
*.DS_Store

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
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();
2830

2931
@Builder.Default
3032
private Date startTime = Calendar.getInstance().getTime();
@@ -46,7 +48,7 @@ public final void refresh() {
4648
categoryCtx.getSet().forEach(x -> x.refresh());
4749
deviceCtx.getSet().forEach(x -> x.refresh());
4850
stats.update(testList);
49-
synchronized (this) {
51+
synchronized (obj) {
5052
setEndTime(Calendar.getInstance().getTime());
5153
}
5254
}
@@ -60,7 +62,7 @@ public final void applyOverrideConf() {
6062
.map(t -> t.getEndTime())
6163
.max(Date::compareTo)
6264
.get();
63-
synchronized (this) {
65+
synchronized (obj) {
6466
setStartTime(min);
6567
setEndTime(max);
6668
}

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,35 @@ public final void update(final List<Test> testList) {
3838
if (testList == null || testList.isEmpty())
3939
return;
4040

41-
update(testList, parent, parentPercentage);
42-
43-
// level 1, for BDD, this would also include Scenario and excludes
44-
// ScenarioOutline
45-
List<Test> children = testList.stream()
46-
.flatMap(x -> x.getChildren().stream())
47-
.filter(x -> x.getBddType() != ScenarioOutline.class)
48-
.collect(Collectors.toList());
49-
List<Test> scenarios = testList.stream()
50-
.flatMap(x -> x.getChildren().stream())
51-
.flatMap(x -> x.getChildren().stream())
52-
.filter(x -> x.getBddType() == Scenario.class)
53-
.collect(Collectors.toList());
54-
children.addAll(scenarios);
55-
update(children, child, childPercentage);
56-
57-
// level 2, for BDD, this only includes Steps
58-
List<Test> grandChildren = children.stream()
59-
.flatMap(x -> x.getChildren().stream())
60-
.filter(x -> x.getBddType() != Scenario.class)
61-
.collect(Collectors.toList());
62-
update(grandChildren, grandchild, grandchildPercentage);
63-
64-
List<Log> logs = testList.stream().flatMap(x -> x.getLogs().stream()).collect(Collectors.toList());
65-
logs.addAll(children.stream().flatMap(x -> x.getLogs().stream()).collect(Collectors.toList()));
66-
logs.addAll(grandChildren.stream().flatMap(x -> x.getLogs().stream()).collect(Collectors.toList()));
67-
update(logs, log, logPercentage);
41+
synchronized (testList) {
42+
update(testList, parent, parentPercentage);
43+
44+
// level 1, for BDD, this would also include Scenario and excludes
45+
// ScenarioOutline
46+
List<Test> children = testList.stream()
47+
.flatMap(x -> x.getChildren().stream())
48+
.filter(x -> x.getBddType() != ScenarioOutline.class)
49+
.collect(Collectors.toList());
50+
List<Test> scenarios = testList.stream()
51+
.flatMap(x -> x.getChildren().stream())
52+
.flatMap(x -> x.getChildren().stream())
53+
.filter(x -> x.getBddType() == Scenario.class)
54+
.collect(Collectors.toList());
55+
children.addAll(scenarios);
56+
update(children, child, childPercentage);
57+
58+
// level 2, for BDD, this only includes Steps
59+
List<Test> grandChildren = children.stream()
60+
.flatMap(x -> x.getChildren().stream())
61+
.filter(x -> x.getBddType() != Scenario.class)
62+
.collect(Collectors.toList());
63+
update(grandChildren, grandchild, grandchildPercentage);
64+
65+
List<Log> logs = testList.stream().flatMap(x -> x.getLogs().stream()).collect(Collectors.toList());
66+
logs.addAll(children.stream().flatMap(x -> x.getLogs().stream()).collect(Collectors.toList()));
67+
logs.addAll(grandChildren.stream().flatMap(x -> x.getLogs().stream()).collect(Collectors.toList()));
68+
update(logs, log, logPercentage);
69+
}
6870
}
6971

7072
private final void update(final List<? extends RunResult> list, final Map<Status, Long> countMap,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.aventstack.extentreports.entity;
2+
3+
import java.util.stream.IntStream;
4+
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
8+
import com.aventstack.extentreports.ExtentReports;
9+
10+
public class ReportStatsConcurrentTest {
11+
12+
/**
13+
* Fix for ConcurrentModificationException caused due to iterating over testList
14+
* vector in ReportStats::update
15+
*
16+
* Issue details:
17+
* https://github.com/extent-framework/extentreports-java/issues/250
18+
*/
19+
@Test
20+
public void concurrentUpdateFlush() {
21+
ExtentReports extent = new ExtentReports();
22+
IntStream.range(0, 100).parallel().forEach(x -> {
23+
extent.createTest(String.valueOf(x)).pass("");
24+
extent.flush();
25+
});
26+
Assert.assertEquals(100, extent.getReport().getTestList().size());
27+
}
28+
}

0 commit comments

Comments
 (0)