Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit eece8f1

Browse files
committed
Even though we have file locks for multi-process concurrency, we still need to use locking for in-process threads.
1 parent 5e2e3db commit eece8f1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/main/java/net/ossindex/gradle/output/JunitXmlReportWriter.java

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.nio.file.attribute.PosixFilePermission;
1212
import java.util.ArrayList;
1313
import java.util.Set;
14+
import java.util.concurrent.locks.Lock;
15+
import java.util.concurrent.locks.ReentrantLock;
1416

1517
import javax.xml.parsers.DocumentBuilder;
1618
import javax.xml.parsers.DocumentBuilderFactory;
@@ -44,6 +46,12 @@ public class JunitXmlReportWriter
4446

4547
private Long startSeconds = null;
4648

49+
/**
50+
* The DocResource class does a file lock preventing concurrent access from other processes, but we still need
51+
* to prevent concurrent access of the document in *THIS* class.
52+
*/
53+
private static Lock lock = new ReentrantLock();
54+
4755
public void init(String junitReport) {
4856

4957
if (junitReport == null) {
@@ -70,6 +78,7 @@ public void updateJunitReport(String totals,
7078
return;
7179
}
7280

81+
lock.lock();
7382
try (DocResource docResource = new DocResource(pathToReport)) {
7483
Document doc = docResource.getDocument();
7584

@@ -94,20 +103,27 @@ public void updateJunitReport(String totals,
94103
catch (IOException e) {
95104
logger.error("Exception writing log: " + e);
96105
}
106+
finally {
107+
lock.unlock();
108+
}
97109
}
98110

99111
public void writeXmlReport(String pathToReport) throws Exception {
100112
if (skip) {
101113
return;
102114
}
103115

116+
lock.lock();
104117
try (DocResource docResource = new DocResource(pathToReport)) {
105118
Document doc = docResource.getDocument();
106119
String testCount = getTotalOfElementsByName(doc, "testcase").toString();
107120
modifyElementAttribute(doc, "testsuites", 0, "tests", testCount);
108121
String failureCount = getTotalOfElementsByName(doc, "failure").toString();
109122
modifyElementAttribute(doc, "testsuites", 0, "failures", failureCount);
110123
}
124+
finally {
125+
lock.unlock();
126+
}
111127
}
112128

113129
private Element addChildElement(Document doc, Element parent, String name, String data) {

0 commit comments

Comments
 (0)