Skip to content

Commit f3f3fd5

Browse files
committed
Sync on the map.
1 parent fa59738 commit f3f3fd5

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/main/java/com/oracle/simpletool/SimpleCodeCoverageInstrument.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,19 @@ private void ensurePrintCoverage(final Env env) {
173173
*/
174174
private void printResults(final Env env) {
175175
final PrintStream printStream = new PrintStream(env.out());
176-
for (Source source : sourceToNotYetCoveredSections.keySet()) {
177-
final String path = source.getPath();
178-
final int lineCount = source.getLineCount();
179-
final List<Integer> notYetCoveredLineNumbers = notYetCoveredLineNumbers(source);
180-
final int notYetCoveredSize = notYetCoveredLineNumbers.size();
181-
double coveredPercentage = 100 * ((double) lineCount - notYetCoveredSize) / lineCount;
182-
printStream.println("==");
183-
printStream.println("Coverage of " + path + " is " + String.format("%.2f%%", coveredPercentage));
184-
printStream.println("Lines not covered by execution:");
185-
for (Integer notYetCoveredLineNumber : notYetCoveredLineNumbers) {
186-
printStream.println(notYetCoveredLineNumber + " " + source.getCharacters(notYetCoveredLineNumber));
176+
synchronized (sourceToNotYetCoveredSections) {
177+
for (Source source : sourceToNotYetCoveredSections.keySet()) {
178+
final String path = source.getPath();
179+
final int lineCount = source.getLineCount();
180+
final List<Integer> notYetCoveredLineNumbers = notYetCoveredLineNumbers(source);
181+
final int notYetCoveredSize = notYetCoveredLineNumbers.size();
182+
double coveredPercentage = 100 * ((double) lineCount - notYetCoveredSize) / lineCount;
183+
printStream.println("==");
184+
printStream.println("Coverage of " + path + " is " + String.format("%.2f%%", coveredPercentage));
185+
printStream.println("Lines not covered by execution:");
186+
for (Integer notYetCoveredLineNumber : notYetCoveredLineNumbers) {
187+
printStream.println(notYetCoveredLineNumber + " " + source.getCharacters(notYetCoveredLineNumber));
188+
}
187189
}
188190
}
189191
}
@@ -194,17 +196,19 @@ private void printResults(final Env env) {
194196
* code in the given {@link Source}
195197
*/
196198
public List<Integer> notYetCoveredLineNumbers(final Source source) {
197-
Set<SourceSection> sections = sourceToNotYetCoveredSections.get(source);
198-
Set<Integer> linesNotCovered = new HashSet<>();
199-
for (SourceSection ss : sections) {
200-
for (int i = ss.getStartLine(); i <= ss.getEndLine(); i++) {
201-
linesNotCovered.add(i);
199+
synchronized (sourceToNotYetCoveredSections) {
200+
Set<SourceSection> sections = sourceToNotYetCoveredSections.get(source);
201+
Set<Integer> linesNotCovered = new HashSet<>();
202+
for (SourceSection ss : sections) {
203+
for (int i = ss.getStartLine(); i <= ss.getEndLine(); i++) {
204+
linesNotCovered.add(i);
205+
}
202206
}
207+
List<Integer> sortedLines = new ArrayList(linesNotCovered.size());
208+
sortedLines.addAll(linesNotCovered);
209+
sortedLines.sort(Integer::compare);
210+
return sortedLines;
203211
}
204-
List<Integer> sortedLines = new ArrayList(linesNotCovered.size());
205-
sortedLines.addAll(linesNotCovered);
206-
sortedLines.sort(Integer::compare);
207-
return sortedLines;
208212
}
209213

210214
/**
@@ -248,9 +252,11 @@ public void onLoad(LoadSourceSectionEvent event) {
248252
final Source source = sourceSection.getSource();
249253
// TODO: This should not be necesery becuase of the filter. Bug!
250254
if (!source.isInternal()) {
251-
sourceToNotYetCoveredSections.computeIfAbsent(source, (Source s) -> {
252-
return new HashSet<>();
253-
}).add(sourceSection);
255+
synchronized (sourceToNotYetCoveredSections) {
256+
sourceToNotYetCoveredSections.computeIfAbsent(source, (Source s) -> {
257+
return new HashSet<>();
258+
}).add(sourceSection);
259+
}
254260
}
255261
}
256262
}
@@ -344,7 +350,9 @@ public void onReturnValue(VirtualFrame vFrame, Object result) {
344350
final Source source = instrumentedSourceSection.getSource();
345351
// TODO: This should not be necesery becuase of the filter. Bug!
346352
if (!source.isInternal()) {
347-
sourceToNotYetCoveredSections.get(source).remove(instrumentedSourceSection);
353+
synchronized (sourceToNotYetCoveredSections) {
354+
sourceToNotYetCoveredSections.get(source).remove(instrumentedSourceSection);
355+
}
348356
}
349357
}
350358
}

0 commit comments

Comments
 (0)