[Test] make sure we test writeTo(Channel) in BytesReference
also introduce proper randomization of content in the bytes
This commit is contained in:
parent
15ff3df243
commit
cd94af2c9e
|
@ -19,20 +19,26 @@
|
|||
|
||||
package org.elasticsearch.common.bytes;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.util.BigArrays;
|
||||
import org.elasticsearch.common.util.ByteArray;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
||||
|
@ -82,13 +88,11 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
|
||||
if (slice.hasArray()) {
|
||||
assertEquals(sliceOffset, slice.arrayOffset());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
try {
|
||||
slice.arrayOffset();
|
||||
fail("expected IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ise) {
|
||||
} catch (IllegalStateException ise) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
@ -134,8 +138,7 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
try {
|
||||
si.readByte();
|
||||
fail("expected EOF");
|
||||
}
|
||||
catch (EOFException eof) {
|
||||
} catch (EOFException eof) {
|
||||
// yay
|
||||
}
|
||||
|
||||
|
@ -144,8 +147,7 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
try {
|
||||
si.readBytes(targetBuf, 0, length * 2);
|
||||
fail("expected IndexOutOfBoundsException: le > stream.length");
|
||||
}
|
||||
catch (IndexOutOfBoundsException ioob) {
|
||||
} catch (IndexOutOfBoundsException ioob) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
@ -242,7 +244,7 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
assertArrayEquals(sliceBytes, Arrays.copyOfRange(buffer, offset, offset + sliceLength));
|
||||
}
|
||||
|
||||
public void testWriteTo() throws IOException {
|
||||
public void testWriteToOutputStream() throws IOException {
|
||||
int length = randomIntBetween(10, PAGE_SIZE * 4);
|
||||
BytesReference pbr = getRandomizedPagedBytesReference(length);
|
||||
BytesStreamOutput out = new BytesStreamOutput();
|
||||
|
@ -252,7 +254,17 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
out.close();
|
||||
}
|
||||
|
||||
public void testSliceWriteTo() throws IOException {
|
||||
public void testWriteToChannel() throws IOException {
|
||||
int length = randomIntBetween(10, PAGE_SIZE * 4);
|
||||
BytesReference pbr = getRandomizedPagedBytesReference(length);
|
||||
File tFile = newTempFile();
|
||||
RandomAccessFile file = new RandomAccessFile(tFile, "rw");
|
||||
pbr.writeTo(file.getChannel());
|
||||
assertEquals(pbr.length(), file.length());
|
||||
assertArrayEquals(pbr.toBytes(), Streams.copyToByteArray(tFile));
|
||||
}
|
||||
|
||||
public void testSliceWriteToOutputStream() throws IOException {
|
||||
int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 5));
|
||||
BytesReference pbr = getRandomizedPagedBytesReference(length);
|
||||
int sliceOffset = randomIntBetween(1, length / 2);
|
||||
|
@ -265,6 +277,19 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
sliceOut.close();
|
||||
}
|
||||
|
||||
public void testSliceWriteToChannel() throws IOException {
|
||||
int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 5));
|
||||
BytesReference pbr = getRandomizedPagedBytesReference(length);
|
||||
int sliceOffset = randomIntBetween(1, length / 2);
|
||||
int sliceLength = length - sliceOffset;
|
||||
BytesReference slice = pbr.slice(sliceOffset, sliceLength);
|
||||
File tFile = newTempFile();
|
||||
RandomAccessFile file = new RandomAccessFile(tFile, "rw");
|
||||
slice.writeTo(file.getChannel());
|
||||
assertEquals(slice.length(), file.length());
|
||||
assertArrayEquals(slice.toBytes(), Streams.copyToByteArray(tFile));
|
||||
}
|
||||
|
||||
public void testToBytes() {
|
||||
int[] sizes = {0, randomInt(PAGE_SIZE), PAGE_SIZE, randomIntBetween(2, PAGE_SIZE * randomIntBetween(2, 5))};
|
||||
|
||||
|
@ -275,8 +300,7 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
// verify that toBytes() is cheap for small payloads
|
||||
if (sizes[i] <= PAGE_SIZE) {
|
||||
assertSame(bytes, pbr.toBytes());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assertNotSame(bytes, pbr.toBytes());
|
||||
}
|
||||
}
|
||||
|
@ -389,13 +413,11 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
assertNotNull(array);
|
||||
assertEquals(sizes[i], array.length);
|
||||
assertSame(array, pbr.array());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
try {
|
||||
pbr.array();
|
||||
fail("expected IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException isx) {
|
||||
} catch (IllegalStateException isx) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
@ -407,13 +429,11 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
BytesReference pbr = getRandomizedPagedBytesReference(length);
|
||||
if (pbr.hasArray()) {
|
||||
assertEquals(0, pbr.arrayOffset());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
try {
|
||||
pbr.arrayOffset();
|
||||
fail("expected IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ise) {
|
||||
} catch (IllegalStateException ise) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
@ -427,13 +447,11 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
BytesReference slice = pbr.slice(sliceOffset, sliceLength);
|
||||
if (slice.hasArray()) {
|
||||
assertEquals(sliceOffset, slice.arrayOffset());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
try {
|
||||
slice.arrayOffset();
|
||||
fail("expected IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ise) {
|
||||
} catch (IllegalStateException ise) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
@ -539,7 +557,20 @@ public class PagedBytesReferenceTest extends ElasticsearchTestCase {
|
|||
}
|
||||
|
||||
private BytesReference getRandomizedPagedBytesReference(int length) {
|
||||
return new PagedBytesReference(bigarrays, bigarrays.newByteArray(length, false), length);
|
||||
// we know bytes stream output always creates a paged bytes reference, we use it to create randomized content
|
||||
ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
|
||||
try {
|
||||
for (int i = 0; i < length; i++) {
|
||||
out.writeByte((byte) getRandom().nextInt(1 << 8));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
fail("should not happen " + e.getMessage());
|
||||
}
|
||||
assertThat(out.size(), Matchers.equalTo(length));
|
||||
BytesReference ref = out.bytes();
|
||||
assertThat(ref.length(), Matchers.equalTo(length));
|
||||
assertThat(ref, Matchers.instanceOf(PagedBytesReference.class));
|
||||
return ref;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue