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

Close PrintStream properly in TestConfigurationLoader tests #81

Merged
merged 1 commit into from
Apr 24, 2024

Conversation

dnskr
Copy link

@dnskr dnskr commented Apr 21, 2024

The PR fixes the issue in com.facebook.airlift.configuration.TestConfigurationLoader. Tests testLoadsFromFile() and testSystemOverridesFile() open PrintStream and close it only if an Exception occurred.
The tests cannot be completed successfully, because of exception in teardown() method.

The fix closes PrintStream in any way by using try-with-resources statement.

Before

$ mvn -f configuration/pom.xml test
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.facebook.airlift:configuration >-----------------
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
[ERROR] Tests run: 119, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.435 s <<< FAILURE! - in TestSuite
[ERROR] teardown(com.facebook.airlift.configuration.TestConfigurationLoader)  Time elapsed: 0.016 s  <<< FAILURE!
java.nio.file.FileSystemException: C:\Users\dnskr\AppData\Local\Temp\1713707499451-0: failed to delete one or more files; see suppressed exceptions for details
        at com.google.common.io.MoreFiles.throwDeleteFailed(MoreFiles.java:785)
        ...
        Suppressed: java.nio.file.FileSystemException: C:\Users\dnskr\AppData\Local\Temp\1713707499451-0\config13441979576855912152.properties: The process cannot access the file because it is being used by another process.

                at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
                at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
                at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
                at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:270)
                at java.base/sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:105)
                at java.base/java.nio.file.Files.delete(Files.java:1141)
                at com.google.common.io.MoreFiles.deleteRecursivelyInsecure(MoreFiles.java:672)
                at com.google.common.io.MoreFiles.deleteDirectoryContentsInsecure(MoreFiles.java:691)
                at com.google.common.io.MoreFiles.deleteRecursivelyInsecure(MoreFiles.java:665)
                at com.google.common.io.MoreFiles.deleteRecursively(MoreFiles.java:540)
                ... 30 more
[INFO] 
[INFO] Results:
[INFO]
[ERROR] Failures: 
[ERROR]   TestConfigurationLoader.teardown:50 » FileSystem C:\Users\dnskr\AppData\Local\...
[INFO]
[ERROR] Tests run: 119, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.026 s
[INFO] Finished at: 2024-04-21T15:51:40+02:00
[INFO] ------------------------------------------------------------------------

After

$ mvn -f configuration/pom.xml test
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.facebook.airlift:configuration >-----------------
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
[INFO] Tests run: 118, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.487 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO]
[INFO] Tests run: 118, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.806 s
[INFO] Finished at: 2024-04-21T15:53:14+02:00
[INFO] ------------------------------------------------------------------------

@dnskr dnskr requested a review from a team as a code owner April 21, 2024 17:21
@dnskr dnskr requested a review from presto-oss April 21, 2024 17:21
@tdcmeehan tdcmeehan self-assigned this Apr 24, 2024
Copy link

@elharo elharo left a comment

Choose a reason for hiding this comment

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

Eek! PrintStream! This class really shouldn't be used for multiple reasons. Try an OutputStreamWriter instead. I'll approve since this isn' a new problem, but it should be fixed

@tdcmeehan tdcmeehan merged commit 1016fbd into prestodb:master Apr 24, 2024
2 checks passed
@dnskr
Copy link
Author

dnskr commented Apr 24, 2024

@elharo Could you please explain why it should not be used or point me to the place where I can read about it?

BTW, PrintStream is used in 2 classes:

System.setOut(new PrintStream(new LoggingOutputStream(Logger.get("stdout")), true));
System.setErr(new PrintStream(new LoggingOutputStream(Logger.get("stderr")), true));

try (PrintStream out = new PrintStream(new FileOutputStream(file))) {

@elharo
Copy link

elharo commented Apr 25, 2024

Well since you asked :-)

image

It's not all about why you shouldn't use PrintStream, but that is in fact a large part of the inspiration for writing that book.

tldr;

  1. PrintStream doesn't handle line endings correctly, especially for network protocols.
  2. PrintStream doesn't manage character sets correctly across platforms.
  3. PrintStream swallows exceptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants