Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Ensure file is closed after reading lines #12990

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -84,16 +85,18 @@ private int measureLogEntryTimeDistance(String startFragment,
AtomicInteger startTime = new AtomicInteger();
AtomicInteger endTime = new AtomicInteger();
try {
Files.lines(getLogPath()).forEach(line -> {
Matcher matcherFirst = startPattern.matcher(line);
if (matcherFirst.matches() && startTime.get() == 0) {
startTime.set(Integer.parseInt(matcherFirst.group(1)));
}
Matcher matcherEnd = endPattern.matcher(line);
if (matcherEnd.matches()) {
endTime.set(Integer.parseInt(matcherEnd.group(1)));
}
});
try (Stream<String> lines = Files.lines(getLogPath())) {
mcollovati marked this conversation as resolved.
Show resolved Hide resolved
lines.forEach(line -> {
Matcher matcherFirst = startPattern.matcher(line);
if (matcherFirst.matches() && startTime.get() == 0) {
startTime.set(Integer.parseInt(matcherFirst.group(1)));
}
Matcher matcherEnd = endPattern.matcher(line);
if (matcherEnd.matches()) {
endTime.set(Integer.parseInt(matcherEnd.group(1)));
}
});
}
if (startTime.get() == 0 && failIfNotFound) {
throw new RuntimeException("No match: " + startFragment);
}
Expand Down