Skip to content

Commit

Permalink
Fix GCS Mock Broken Handling of some Blobs (#50666)
Browse files Browse the repository at this point in the history
* Fix GCS Mock Broken Handling of some Blobs

We were incorrectly handling blobs starting in `\r\n` which broke
tests randomly when blobs started on these.

Relates #49429
  • Loading branch information
original-brownbear authored Jan 6, 2020
1 parent c2a3eda commit 08ae313
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public byte[] toByteArray() {
return buf;
}
};
boolean skippedEmptyLine = false;
while ((read = in.read()) != -1) {
out.reset();
boolean markAndContinue = false;
Expand All @@ -290,7 +291,7 @@ public byte[] toByteArray() {
} while ((read = in.read()) != -1);
final String bucketPrefix = "{\"bucket\":";
final String start = new String(out.toByteArray(), 0, Math.min(out.size(), bucketPrefix.length()), UTF_8);
if (start.length() == 0 || start.equals("\r\n") || start.startsWith("--")
if ((skippedEmptyLine == false && start.length() == 0) || start.startsWith("--")
|| start.toLowerCase(Locale.ROOT).startsWith("content")) {
markAndContinue = true;
} else if (start.startsWith(bucketPrefix)) {
Expand All @@ -302,6 +303,7 @@ public byte[] toByteArray() {
}
}
if (markAndContinue) {
skippedEmptyLine = start.length() == 0;
in.mark(Integer.MAX_VALUE);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public void testWriteRead() throws IOException {
byte[] buffer = new byte[scaledRandomIntBetween(1, data.length - target.length())];
int offset = scaledRandomIntBetween(0, buffer.length - 1);
int read = stream.read(buffer, offset, buffer.length - offset);
target.append(new BytesRef(buffer, offset, read));
if (read >= 0) {
target.append(new BytesRef(buffer, offset, read));
} else {
fail("Expected [" + (data.length - target.length()) + "] more bytes to be readable but reached EOF");
}
}
assertEquals(data.length, target.length());
assertArrayEquals(data, Arrays.copyOfRange(target.bytes(), 0, target.length()));
Expand Down

0 comments on commit 08ae313

Please sign in to comment.