use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class ThreadContextTests method testSerialize.
public void testSerialize() throws IOException {
Settings build = Settings.builder().put("request.headers.default", "1").build();
ThreadContext threadContext = new ThreadContext(build);
threadContext.putHeader("foo", "bar");
threadContext.putTransient("ctx.foo", 1);
threadContext.addResponseHeader("Warning", "123456");
if (rarely()) {
threadContext.addResponseHeader("Warning", "123456");
}
threadContext.addResponseHeader("Warning", "234567");
BytesStreamOutput out = new BytesStreamOutput();
threadContext.writeTo(out);
try (ThreadContext.StoredContext ctx = threadContext.stashContext()) {
assertNull(threadContext.getHeader("foo"));
assertNull(threadContext.getTransient("ctx.foo"));
assertTrue(threadContext.getResponseHeaders().isEmpty());
assertEquals("1", threadContext.getHeader("default"));
threadContext.readHeaders(out.bytes().streamInput());
assertEquals("bar", threadContext.getHeader("foo"));
assertNull(threadContext.getTransient("ctx.foo"));
final Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();
final List<String> warnings = responseHeaders.get("Warning");
assertThat(responseHeaders.keySet(), hasSize(1));
assertThat(warnings, hasSize(2));
assertThat(warnings, hasItem(equalTo("123456")));
assertThat(warnings, hasItem(equalTo("234567")));
}
assertEquals("bar", threadContext.getHeader("foo"));
assertEquals(Integer.valueOf(1), threadContext.getTransient("ctx.foo"));
assertEquals("1", threadContext.getHeader("default"));
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class BoundTransportAddressTests method testSerialization.
public void testSerialization() throws Exception {
InetAddress[] inetAddresses = InetAddress.getAllByName("0.0.0.0");
List<TransportAddress> transportAddressList = new ArrayList<>();
for (InetAddress address : inetAddresses) {
transportAddressList.add(new TransportAddress(address, randomIntBetween(9200, 9299)));
}
final BoundTransportAddress transportAddress = new BoundTransportAddress(transportAddressList.toArray(new TransportAddress[0]), transportAddressList.get(0));
assertThat(transportAddress.boundAddresses().length, equalTo(transportAddressList.size()));
// serialize
BytesStreamOutput streamOutput = new BytesStreamOutput();
transportAddress.writeTo(streamOutput);
StreamInput in = streamOutput.bytes().streamInput();
BoundTransportAddress serializedAddress;
if (randomBoolean()) {
serializedAddress = BoundTransportAddress.readBoundTransportAddress(in);
} else {
serializedAddress = new BoundTransportAddress();
serializedAddress.readFrom(in);
}
assertThat(serializedAddress, not(sameInstance(transportAddress)));
assertThat(serializedAddress.boundAddresses().length, equalTo(transportAddress.boundAddresses().length));
assertThat(serializedAddress.publishAddress(), equalTo(transportAddress.publishAddress()));
TransportAddress[] serializedBoundAddresses = serializedAddress.boundAddresses();
TransportAddress[] boundAddresses = transportAddress.boundAddresses();
for (int i = 0; i < serializedBoundAddresses.length; i++) {
assertThat(serializedBoundAddresses[i], equalTo(boundAddresses[i]));
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class ByteSizeUnitTests method testSerialization.
public void testSerialization() throws IOException {
for (ByteSizeUnit unit : ByteSizeUnit.values()) {
try (BytesStreamOutput out = new BytesStreamOutput()) {
unit.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
ByteSizeUnit deserialized = ByteSizeUnit.readFrom(in);
assertEquals(unit, deserialized);
}
}
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class ByteSizeValueTests method testSerialization.
public void testSerialization() throws IOException {
ByteSizeValue byteSizeValue = new ByteSizeValue(randomNonNegativeLong(), randomFrom(ByteSizeUnit.values()));
try (BytesStreamOutput out = new BytesStreamOutput()) {
byteSizeValue.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
ByteSizeValue deserializedByteSizeValue = new ByteSizeValue(in);
assertEquals(byteSizeValue.getBytes(), deserializedByteSizeValue.getBytes());
}
}
}
use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.
the class FuzzinessTests method doSerializeRoundtrip.
private static Fuzziness doSerializeRoundtrip(Fuzziness in) throws IOException {
BytesStreamOutput output = new BytesStreamOutput();
in.writeTo(output);
StreamInput streamInput = output.bytes().streamInput();
return new Fuzziness(streamInput);
}
Aggregations