Assorted fixes for bugs in the PagedBytesReference tests, plus a typo

where the wrong variable was used for a calculation.

Closes #5549
This commit is contained in:
Holger Hoffstätte 2014-03-26 14:07:22 +01:00
parent 196e3c3602
commit 280d4bfd28
2 changed files with 20 additions and 10 deletions

View File

@ -374,7 +374,7 @@ public final class PagedBytesReference implements BytesReference {
// a remaining fragment < pagesize needs at least one buffer // a remaining fragment < pagesize needs at least one buffer
numBuffers += (pages == 0) ? 1 : pages; numBuffers += (pages == 0) ? 1 : pages;
// a remainder that is not a multiple of pagesize also needs an extra buffer // a remainder that is not a multiple of pagesize also needs an extra buffer
numBuffers += (pages > 0 && numBuffers % PAGE_SIZE > 0) ? 1 : 0; numBuffers += (pages > 0 && numBytes % PAGE_SIZE > 0) ? 1 : 0;
return numBuffers; return numBuffers;
} }

View File

@ -57,7 +57,7 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
@Test @Test
public void testGet() { public void testGet() {
int length = randomInt(PAGE_SIZE * 3); int length = randomIntBetween(1, PAGE_SIZE * 3);
BytesReference pbr = getRandomizedPagedBytesReference(length); BytesReference pbr = getRandomizedPagedBytesReference(length);
int sliceOffset = randomIntBetween(0, length / 2); int sliceOffset = randomIntBetween(0, length / 2);
int sliceLength = Math.max(1, length - sliceOffset - 1); int sliceLength = Math.max(1, length - sliceOffset - 1);
@ -79,7 +79,7 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
int length = randomInt(PAGE_SIZE * 3); int length = randomInt(PAGE_SIZE * 3);
BytesReference pbr = getRandomizedPagedBytesReference(length); BytesReference pbr = getRandomizedPagedBytesReference(length);
int sliceOffset = randomIntBetween(0, length / 2); int sliceOffset = randomIntBetween(0, length / 2);
int sliceLength = Math.max(1, length - sliceOffset - 1); int sliceLength = Math.max(0, length - sliceOffset - 1);
BytesReference slice = pbr.slice(sliceOffset, sliceLength); BytesReference slice = pbr.slice(sliceOffset, sliceLength);
assertEquals(sliceLength, slice.length()); assertEquals(sliceLength, slice.length());
@ -119,9 +119,12 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
assertEquals(origBuf[0], targetBuf[0]); assertEquals(origBuf[0], targetBuf[0]);
si.reset(); si.reset();
// read an int // read a few few bytes as ints
int i = si.read(); int bytesToRead = randomIntBetween(1, length/2);
assertFalse(i == 0); for (int i = 0; i < bytesToRead; i++) {
int b = si.read();
assertEquals(pbr.get(i), b);
}
si.reset(); si.reset();
// bulk-read all // bulk-read all
@ -233,7 +236,11 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
} }
public void testToBytesArrayMaterializedPages() { public void testToBytesArrayMaterializedPages() {
int length = randomIntBetween(PAGE_SIZE, PAGE_SIZE * randomIntBetween(2,5)); // we need a length != (n * pagesize) to avoid page sharing at boundaries
int length = 0;
while ((length % PAGE_SIZE) == 0) {
length = randomIntBetween(PAGE_SIZE, PAGE_SIZE * randomIntBetween(2,5));
}
BytesReference pbr = getRandomizedPagedBytesReference(length); BytesReference pbr = getRandomizedPagedBytesReference(length);
BytesArray ba = pbr.toBytesArray(); BytesArray ba = pbr.toBytesArray();
BytesArray ba2 = pbr.toBytesArray(); BytesArray ba2 = pbr.toBytesArray();
@ -463,9 +470,12 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
BytesReference slice2 = pbr.slice(sliceFrom, sliceLength); BytesReference slice2 = pbr.slice(sliceFrom, sliceLength);
assertArrayEquals(slice1.toBytes(), slice2.toBytes()); assertArrayEquals(slice1.toBytes(), slice2.toBytes());
// test a slice with same offset but different length // test a slice with same offset but different length,
BytesReference slice3 = pbr.slice(sliceFrom, sliceLength / 2); // unless randomized testing gave us a 0-length slice.
assertFalse(Arrays.equals(slice1.toBytes(), slice3.toBytes())); if (sliceLength > 0) {
BytesReference slice3 = pbr.slice(sliceFrom, sliceLength / 2);
assertFalse(Arrays.equals(slice1.toBytes(), slice3.toBytes()));
}
} }
private BytesReference getRandomizedPagedBytesReference(int length) { private BytesReference getRandomizedPagedBytesReference(int length) {