Skip to content

[IO-782] SequenceReader should close readers when its close method is called#391

Closed
digiovinazzo wants to merge 2 commits intoapache:masterfrom
digiovinazzo:IO-782-SequenceReader-should-close-readers-when-its-close-method-is-called
Closed

[IO-782] SequenceReader should close readers when its close method is called#391
digiovinazzo wants to merge 2 commits intoapache:masterfrom
digiovinazzo:IO-782-SequenceReader-should-close-readers-when-its-close-method-is-called

Conversation

@digiovinazzo
Copy link

@codecov-commenter
Copy link

Codecov Report

Merging #391 (17f7689) into master (c1d9f7f) will decrease coverage by 0.09%.
The diff coverage is 71.42%.

@@             Coverage Diff              @@
##             master     #391      +/-   ##
============================================
- Coverage     86.18%   86.08%   -0.10%     
+ Complexity     3214     3213       -1     
============================================
  Files           215      215              
  Lines          7490     7503      +13     
  Branches        906      908       +2     
============================================
+ Hits           6455     6459       +4     
- Misses          791      797       +6     
- Partials        244      247       +3     
Impacted Files Coverage Δ
...va/org/apache/commons/io/input/SequenceReader.java 89.36% <71.42%> (-7.70%) ⬇️
.../apache/commons/io/input/ReadAheadInputStream.java 68.53% <0.00%> (-3.38%) ⬇️
.../main/java/org/apache/commons/io/input/Tailer.java 86.06% <0.00%> (+0.49%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@digiovinazzo digiovinazzo force-pushed the IO-782-SequenceReader-should-close-readers-when-its-close-method-is-called branch 2 times, most recently from 6e357a9 to bbd7a98 Compare October 5, 2022 20:06
Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

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

I don't think I agree with the premise of this PR: the class did not allocate the resources, so it has no business closing them, it does not own these resources.
What does the community think?

@digiovinazzo digiovinazzo force-pushed the IO-782-SequenceReader-should-close-readers-when-its-close-method-is-called branch from bbd7a98 to 22d2bdc Compare October 7, 2022 16:54
@digiovinazzo
Copy link
Author

I checked for example BufferedReader and it does close the Reader passed in the constructor.
Moreover even the old java.io.SequenceInputStream closes its InputStreams.

@garydgregory
Copy link
Member

@digiovinazzo
Thank you for the clarification, this PR seems to make sense then :-)

assertEquals('A', sequenceReader.read());
assertEquals(EOF, sequenceReader.read());
} catch (IOException e) {
fail("No IOException expected");
Copy link
Member

Choose a reason for hiding this comment

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

Why not just let the exception percolate to fail the test?

Copy link
Author

Choose a reason for hiding this comment

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

That's another option, it was just to have a meaningful message on fail

Copy link
Member

Choose a reason for hiding this comment

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

Simpler is better:

        try (SequenceReader sequenceReader = new SequenceReader(reader1, emptyReader)) {
            assertEquals('A', sequenceReader.read());
            assertEquals(EOF, sequenceReader.read());
        } finally {
            assertTrue(closeEmptyReader.get());
            assertTrue(closeReader1.get());
        }

@digiovinazzo digiovinazzo force-pushed the IO-782-SequenceReader-should-close-readers-when-its-close-method-is-called branch from 22d2bdc to 33da613 Compare October 7, 2022 20:09
@garydgregory garydgregory changed the title IO-782 sequence reader should close readers when its close method is called [IO-782] SequenceReader should close readers when its close method is called Oct 9, 2022
try {
reader.close();
} catch (IOException e) {
ioExceptionList.add(e);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about the semantics here, which are completely different from SequenceInputStream. For good or bad, I think it would be better for the closing logic of this class to match SequenceInputStream.


private Reader reader;
private Iterator<? extends Reader> readers;
private Iterable<? extends Reader> readersIterable;
Copy link
Member

Choose a reason for hiding this comment

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

No need to change the name, it makes the PR noisier.

final AtomicBoolean closeEmptyReader = new AtomicBoolean(false);
final Reader emptyReader = new Reader() {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

Use final where you can.


final AtomicBoolean closeReader1 = new AtomicBoolean(false);
final Reader reader1 = new Reader() {
private final char[] content = new char[]{'A'};
Copy link
Member

Choose a reason for hiding this comment

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

Use abbreviated notation, IOW ... = {'A'};

private int position = 0;

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

Use final where you can.

}
};

final AtomicBoolean closeReader1 = new AtomicBoolean(false);
Copy link
Member

Choose a reason for hiding this comment

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

No need to initialize an AtomicBoolean to false, that's the default, IOW -> new AtomicBoolean();

assertEquals('A', sequenceReader.read());
assertEquals(EOF, sequenceReader.read());
} catch (IOException e) {
fail("No IOException expected");
Copy link
Member

Choose a reason for hiding this comment

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

Simpler is better:

        try (SequenceReader sequenceReader = new SequenceReader(reader1, emptyReader)) {
            assertEquals('A', sequenceReader.read());
            assertEquals(EOF, sequenceReader.read());
        } finally {
            assertTrue(closeEmptyReader.get());
            assertTrue(closeReader1.get());
        }

@garydgregory
Copy link
Member

The issue is now resolved in git master by making the implementation closer to what SequenceInputStream does. Please verify your use case and close. TY!

@garydgregory
Copy link
Member

Closing per my previous comment and no feedback in 4 days.

@digiovinazzo
Copy link
Author

I was offline last week, @garydgregory thanks for taking care of this!

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