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

Fix incorrect trace output upon file closing #132

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/main/java/org/kohsuke/file_leak_detector/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ private static synchronized void put(Object _this, Record r) {
public static synchronized void close(Object _this) {
Record r = TABLE.remove(_this);
if (r != null && TRACE != null && !tracing) {
if (r instanceof FileRecord) {
r = new FileRecord(((FileRecord) r).file);
}
tracing = true;
r.dump("Closed ", TRACE);
tracing = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -245,8 +247,8 @@ public void openCloseFilesNewByteChannelRead() throws Exception {
assertNull("File record for file=" + tempFile + " not removed", findFileRecord(tempFile));

String traceOutput = output.toString();
assertTrue(traceOutput.contains("Opened " + tempFile));
assertTrue(traceOutput.contains("Closed " + tempFile));
assertContainsAdjacentLines(traceOutput, "Opened " + tempFile, "java.base/java.nio.file.Files.newByteChannel(");
assertContainsAdjacentLines(traceOutput, "Closed " + tempFile, "java.base/java.nio.channels.spi.AbstractInterruptibleChannel.close(");
}

@Test
Expand All @@ -271,8 +273,8 @@ public void openCloseFileLines() throws Exception {
assertNull("File record for file=" + tempFile + " not removed", findFileRecord(tempFile));

String traceOutput = output.toString();
assertThat(traceOutput, containsString("Opened " + tempFile));
assertThat(traceOutput, containsString("Closed " + tempFile));
assertContainsAdjacentLines(traceOutput, "Opened " + tempFile, "java.base/java.nio.channels.FileChannel.open(");
assertContainsAdjacentLines(traceOutput, "Closed " + tempFile, "java.base/java.nio.channels.spi.AbstractInterruptibleChannel.close(");
}

private static FileRecord findFileRecord(File file) {
Expand All @@ -286,4 +288,20 @@ private static FileRecord findFileRecord(File file) {
}
return null;
}

private static void assertContainsAdjacentLines(String output, String thisLineContent, String nextLineContent) throws IOException {
List<String> lines = IOUtils.readLines(IOUtils.toInputStream(output));
int index = findIndexOf(lines, thisLineContent);
assertTrue(index != -1);
assertThat(lines.get(index + 1), containsString(nextLineContent));
}

private static int findIndexOf(List<String> lines, String target) {
for (int i = 0; i < lines.size(); ++i) {
if (lines.get(i).contains(target)) {
return i;
}
}
return -1;
}
}