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

[Core, TestNg] Clean up stream closing #1175

Merged
merged 1 commit into from
Jul 9, 2017
Merged

Conversation

mpkorstanje
Copy link
Contributor

Summary

Refactored the code around streams so it follows these rules:

  • Streams should be closed by their owners
  • A stream is owned by its creator or when provided as a constructor argument
  • Stream ownership should be the smallest possible unit. The method, class, process, system.
  • System.out and System.error may never be closed

Fixes #1108

How Has This Been Tested?

Checked the code for all occurrences of .close(),System.out, System.error, new NiceAppendable, new .*OutputStream and new .*Writerin non-test code.

Automated tests should be passing.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@@ -65,7 +65,7 @@ public NiceAppendable println(CharSequence csq) {
public void close() {
try {
tryFlush();
if (out instanceof Closeable && out != System.out && out != System.err) {
if (out instanceof Closeable) {
Copy link
Contributor Author

@mpkorstanje mpkorstanje Jul 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the responsibility of TestNGCucumberRunner. It knows it is passing ownership of System.out

@@ -119,11 +120,11 @@ public void receive(TestRunFinished event) {
};

public HTMLFormatter(URL htmlReportDir) {
this.htmlReportDir = htmlReportDir;
this(htmlReportDir, createJsOut(htmlReportDir));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idiomatic java.


private static void writeStreamToURL(InputStream in, URL url) {
OutputStream out = createReportFileOutputStream(url);

Copy link
Contributor Author

@mpkorstanje mpkorstanje Jul 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimizes the ownership scope of the output stream

} catch (IOException e) {
throw new CucumberException("Unable to write to report file item: ", e);
} finally {
closeQuietly(out);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idiomatic java. For lack of being able to use try-with-resources

}
}

private void writeBytesAndClose(byte[] buf, OutputStream out) {
private static void writeBytesToURL(byte[] buf, URL url) throws CucumberException {
OutputStream reportFileOutputStream = createReportFileOutputStream(url);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimizes the ownership scope of the output stream

  Refactored the code around streams so it follows these rules:
   - Streams should be closed by their owners
   - A stream is owned by its creator or when provided as a constructor argument
   - Stream ownership should be the smallest possible unit. The method, class, process, system.
   - System.out and System.error may never be closed

 Fixes #1108
} catch (IOException ignored) {
// go gentle into that good night
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3rd duplication but I'd rather wait for java7 try-with-resources

@brasmusson
Copy link
Contributor

Yes, this looks like a better solution than the one I did in 51f0c9b.

@mpkorstanje mpkorstanje merged commit cea10a1 into master Jul 9, 2017
@brasmusson brasmusson deleted the stream-closing branch July 9, 2017 19:19
@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
@mpkorstanje mpkorstanje restored the stream-closing branch October 4, 2019 18:20
@mpkorstanje mpkorstanje deleted the stream-closing branch October 4, 2019 18:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HtmlFormatter doesn't close Stream for embedded image.
2 participants