Search in sources :

Example 81 with BytesStreamOutput

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"));
}
Also used : List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 82 with BytesStreamOutput

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]));
    }
}
Also used : ArrayList(java.util.ArrayList) StreamInput(org.elasticsearch.common.io.stream.StreamInput) InetAddress(java.net.InetAddress) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 83 with BytesStreamOutput

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);
            }
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 84 with BytesStreamOutput

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());
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 85 with BytesStreamOutput

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);
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)222 StreamInput (org.elasticsearch.common.io.stream.StreamInput)147 Test (org.junit.Test)45 CrateUnitTest (io.crate.test.integration.CrateUnitTest)36 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)24 IOException (java.io.IOException)21 BytesArray (org.elasticsearch.common.bytes.BytesArray)21 BytesReference (org.elasticsearch.common.bytes.BytesReference)18 Version (org.elasticsearch.Version)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ArrayList (java.util.ArrayList)12 BytesRef (org.apache.lucene.util.BytesRef)11 Map (java.util.Map)10 UUID (java.util.UUID)9 Symbol (io.crate.analyze.symbol.Symbol)8 HashMap (java.util.HashMap)8 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)7 List (java.util.List)6