-
Notifications
You must be signed in to change notification settings - Fork 4
Reduce memory pressure in FormattingTestReporter #737
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
Changes from 3 commits
907c7fa
c9fbe99
0d7eae0
4aaf483
5054374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,8 +22,8 @@ | |||||||||||||
| import com.palantir.witchcraft.java.logging.format.LogParser; | ||||||||||||||
| import java.io.File; | ||||||||||||||
| import java.io.IOException; | ||||||||||||||
| import java.io.StringWriter; | ||||||||||||||
| import java.io.Writer; | ||||||||||||||
| import java.util.function.BiConsumer; | ||||||||||||||
| import org.gradle.api.Action; | ||||||||||||||
| import org.gradle.api.internal.tasks.testing.junit.result.TestClassResult; | ||||||||||||||
| import org.gradle.api.internal.tasks.testing.junit.result.TestResultsProvider; | ||||||||||||||
|
|
@@ -61,6 +61,21 @@ private static final class FormattingTestResultsProvider implements TestResultsP | |||||||||||||
| writer.write("\n"); | ||||||||||||||
| } | ||||||||||||||
| : Writable.NOP)); | ||||||||||||||
| private static final BiConsumer<String, Writer> LINE_PROCESSOR = (line, outputWriter) -> { | ||||||||||||||
| try { | ||||||||||||||
| PARSER.tryParse(line) | ||||||||||||||
| .orElseGet(() -> out -> { | ||||||||||||||
| try { | ||||||||||||||
| out.write(line); | ||||||||||||||
| } catch (IOException e) { | ||||||||||||||
| throw new RuntimeException(e); | ||||||||||||||
|
ash211 marked this conversation as resolved.
Outdated
|
||||||||||||||
| } | ||||||||||||||
| }) | ||||||||||||||
| .write(outputWriter); | ||||||||||||||
| } catch (IOException e) { | ||||||||||||||
| throw new RuntimeException(e); | ||||||||||||||
|
ash211 marked this conversation as resolved.
Outdated
|
||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| private final TestResultsProvider delegate; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -69,30 +84,58 @@ private static final class FormattingTestResultsProvider implements TestResultsP | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Override | ||||||||||||||
| @SuppressWarnings("StringSplitter") | ||||||||||||||
| public void writeAllOutput(long classId, TestOutputEvent.Destination destination, Writer writer) { | ||||||||||||||
| if (destination == TestOutputEvent.Destination.StdErr | ||||||||||||||
| || destination == TestOutputEvent.Destination.StdOut) { | ||||||||||||||
| StringWriter stringWriter = new StringWriter(); | ||||||||||||||
| delegate.writeAllOutput(classId, destination, stringWriter); | ||||||||||||||
| String contents = stringWriter.toString(); | ||||||||||||||
| for (String line : contents.split("\n")) { | ||||||||||||||
| try { | ||||||||||||||
| PARSER.tryParse(line) | ||||||||||||||
| .orElseGet(() -> out -> { | ||||||||||||||
| out.write(line); | ||||||||||||||
| out.write("\n"); | ||||||||||||||
| }) | ||||||||||||||
| .write(writer); | ||||||||||||||
| } catch (IOException e) { | ||||||||||||||
| throw new RuntimeException(e); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| delegate.writeAllOutput(classId, destination, new LineProcessingWriter(writer, LINE_PROCESSOR)); | ||||||||||||||
| } else { | ||||||||||||||
| delegate.writeAllOutput(classId, destination, writer); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private static class LineProcessingWriter extends Writer { | ||||||||||||||
| private final Writer delegate; | ||||||||||||||
| private final BiConsumer<String, Writer> lineProcessor; | ||||||||||||||
| private final StringBuilder lineBuffer = new StringBuilder(); | ||||||||||||||
|
|
||||||||||||||
| LineProcessingWriter(Writer delegate, BiConsumer<String, Writer> lineProcessor) { | ||||||||||||||
| this.delegate = delegate; | ||||||||||||||
| this.lineProcessor = lineProcessor; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Override | ||||||||||||||
| public void write(char[] cbuf, int off, int len) throws IOException { | ||||||||||||||
| for (int i = off; i < off + len; i++) { | ||||||||||||||
| char ch = cbuf[i]; | ||||||||||||||
| if (ch == '\n') { | ||||||||||||||
| processLine(); | ||||||||||||||
| } else { | ||||||||||||||
| lineBuffer.append(ch); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Override | ||||||||||||||
| public void flush() throws IOException { | ||||||||||||||
| delegate.flush(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Override | ||||||||||||||
| public void close() throws IOException { | ||||||||||||||
| if (!lineBuffer.isEmpty()) { | ||||||||||||||
| processLine(); | ||||||||||||||
| } | ||||||||||||||
| delegate.close(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private void processLine() throws IOException { | ||||||||||||||
| String line = lineBuffer.toString(); | ||||||||||||||
| lineProcessor.accept(line, delegate); | ||||||||||||||
| delegate.write("\n"); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to synchronize on the writer to ensure this is written atomically?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we're going to make this LineProcessingWriter thread-safe, then I think we need to protect the lineBuffer from concurrent writes, and maybe then add I'm not sure we need it to be thread-safe though... even multiple calls to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||
| lineBuffer.setLength(0); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Override | ||||||||||||||
| public void writeNonTestOutput(long classId, TestOutputEvent.Destination destination, Writer writer) { | ||||||||||||||
| delegate.writeNonTestOutput(classId, destination, writer); | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.