Skip to content

#800: fix last block of stream being ignored if size < 8k - #802

Merged
vlsi merged 5 commits into
pgjdbc:masterfrom
slmsbrhgn:master
Apr 17, 2017
Merged

vlsi merged 5 commits into
pgjdbc:masterfrom
slmsbrhgn:master

Conversation

@slmsbrhgn

Copy link
Copy Markdown
Contributor

Fixes #800 (data truncated at the end of the stream, while using setCharacterStream)

off += remaining;
len -= remaining;
if (len == 0) {
if (len >= 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This condition would effectively exit from the loop at the first iteration, so it will hurt performance.

// System.out.println(String.format("Read: %4d Start: %3d End: %3d", read, buffer[0], buffer[read - 1]));
total += read;
if (read == BLOCK) {
assertEquals("Byte values are not correct", buffer[0], buffer[read % BLOCK]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please refrain from asserting multiple assorted things in a single testcase.

It is hard to understand what this test actually examines

@codecov-io

codecov-io commented Apr 5, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #802 into master will increase coverage by 0.04%.
The diff coverage is 0%.

@@             Coverage Diff             @@
##             master    #802      +/-   ##
===========================================
+ Coverage     65.26%   65.3%   +0.04%     
- Complexity     3517    3525       +8     
===========================================
  Files           165     166       +1     
  Lines         15217   15230      +13     
  Branches       2465    2468       +3     
===========================================
+ Hits           9931    9946      +15     
+ Misses         4099    4097       -2     
  Partials       1187    1187


// initialize with values (0, 1, ... 127, 0, 1, ...)
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i & 0x7F);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the purpose of this initialization?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was used to check if the read blocks were contiguous (ie last block ended with value 33, next one should start with 34), but is not needed anymore (it only makes sense with custom charBufferSize in ReaderInputStream). Will remove it.

int read;
int total = 0;

while ((read = r.read(buffer, 0, BLOCK)) > -1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you write the same test without a loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can replace it with N calls to read (in this case 6). I don't think that using a single (bigger) read would fail the test when the problem occurs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you make a test that uses just two read calls?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure

final byte[] buffer = new byte[BLOCK];

InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(data));
ReaderInputStream r = new ReaderInputStream(isr);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will the test still be valid if ReaderInputStream.DEFAULT_CHAR_BUFFER_SIZE is changed for some reason?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. Although ideally the value would be read from ReaderInputStream instead of being hardcoded there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you think it makes sense to make ReaderInputStream.DEFAULT_CHAR_BUFFER_SIZE public (or having a getter) and make the test using that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

DEFAULT_CHAR_BUFFER_SIZE should be private, and it should not be accessed/visible from external point of view.

@vlsi vlsi added this to the 42.1.0 milestone Apr 16, 2017
@vlsi
vlsi merged commit 77cace4 into pgjdbc:master Apr 17, 2017
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