@Test public void testBytesStreamInput() throws IOException { byte stuff[] = new byte[] {0, 1, 2, 3}; BytesRef stuffRef = new BytesRef(stuff, 2, 2); BytesArray stuffArray = new BytesArray(stuffRef); BytesStreamInput input = new BytesStreamInput(stuffArray); assertEquals(2, input.read()); assertEquals(3, input.read()); assertEquals(-1, input.read()); input.close(); }
@Test public void testSimpleStreams() throws Exception { BytesStreamOutput out = new BytesStreamOutput(); out.writeBoolean(false); out.writeByte((byte) 1); out.writeShort((short) -1); out.writeInt(-1); out.writeVInt(2); out.writeLong(-3); out.writeVLong(4); out.writeFloat(1.1f); out.writeDouble(2.2); out.writeString("hello"); out.writeString("goodbye"); BytesStreamInput in = new BytesStreamInput(out.bytes().toBytes(), false); assertThat(in.readBoolean(), equalTo(false)); assertThat(in.readByte(), equalTo((byte) 1)); assertThat(in.readShort(), equalTo((short) -1)); assertThat(in.readInt(), equalTo(-1)); assertThat(in.readVInt(), equalTo(2)); assertThat(in.readLong(), equalTo((long) -3)); assertThat(in.readVLong(), equalTo((long) 4)); assertThat((double) in.readFloat(), closeTo(1.1, 0.0001)); assertThat(in.readDouble(), closeTo(2.2, 0.0001)); assertThat(in.readString(), equalTo("hello")); assertThat(in.readString(), equalTo("goodbye")); }
@Test public void testSerialization() throws IOException { MultiGetRequest multiGetRequest = new MultiGetRequest(); if (randomBoolean()) { multiGetRequest.preference(randomAsciiOfLength(randomIntBetween(1, 10))); } if (randomBoolean()) { multiGetRequest.realtime(false); } if (randomBoolean()) { multiGetRequest.refresh(true); } multiGetRequest.ignoreErrorsOnGeneratedFields(randomBoolean()); MultiGetShardRequest multiGetShardRequest = new MultiGetShardRequest(multiGetRequest, "index", 0); int numItems = iterations(10, 30); for (int i = 0; i < numItems; i++) { MultiGetRequest.Item item = new MultiGetRequest.Item( "alias-" + randomAsciiOfLength(randomIntBetween(1, 10)), "type", "id-" + i); if (randomBoolean()) { int numFields = randomIntBetween(1, 5); String[] fields = new String[numFields]; for (int j = 0; j < fields.length; j++) { fields[j] = randomAsciiOfLength(randomIntBetween(1, 10)); } item.fields(fields); } if (randomBoolean()) { item.version(randomIntBetween(1, Integer.MAX_VALUE)); item.versionType(randomFrom(VersionType.values())); } if (randomBoolean()) { item.fetchSourceContext(new FetchSourceContext(randomBoolean())); } multiGetShardRequest.add(0, item); } BytesStreamOutput out = new BytesStreamOutput(); out.setVersion(randomVersion()); multiGetShardRequest.writeTo(out); BytesStreamInput in = new BytesStreamInput(out.bytes()); in.setVersion(out.getVersion()); MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest(); multiGetShardRequest2.readFrom(in); assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index())); assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference())); assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime())); assertThat(multiGetShardRequest2.refresh(), equalTo(multiGetShardRequest.refresh())); if (in.getVersion().onOrAfter(Version.V_1_4_0_Beta)) { assertThat( multiGetShardRequest2.ignoreErrorsOnGeneratedFields(), equalTo(multiGetShardRequest.ignoreErrorsOnGeneratedFields())); } else { assertThat(multiGetShardRequest2.ignoreErrorsOnGeneratedFields(), equalTo(false)); } assertThat(multiGetShardRequest2.items.size(), equalTo(multiGetShardRequest.items.size())); for (int i = 0; i < multiGetShardRequest2.items.size(); i++) { MultiGetRequest.Item item = multiGetShardRequest.items.get(i); MultiGetRequest.Item item2 = multiGetShardRequest2.items.get(i); if (in.getVersion().onOrAfter(Version.V_1_4_0_Beta)) { assertThat(item2.index(), equalTo(item.index())); } else { // before 1.4 we have only one index, the concrete one assertThat(item2.index(), equalTo(multiGetShardRequest.index())); } assertThat(item2.type(), equalTo(item.type())); assertThat(item2.id(), equalTo(item.id())); assertThat(item2.fields(), equalTo(item.fields())); assertThat(item2.version(), equalTo(item.version())); assertThat(item2.versionType(), equalTo(item.versionType())); assertThat(item2.fetchSourceContext(), equalTo(item.fetchSourceContext())); } if (in.getVersion().onOrAfter(Version.V_1_4_0_Beta)) { // we don't serialize the original index before 1.4, it'll get the concrete one assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices())); assertThat( multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions())); } }