public void testWriteStreamableList() throws IOException {
    final int size = randomIntBetween(0, 5);
    final List<TestStreamable> expected = new ArrayList<>(size);

    for (int i = 0; i < size; ++i) {
      expected.add(new TestStreamable(randomBoolean()));
    }

    final BytesStreamOutput out = new BytesStreamOutput();
    out.writeStreamableList(expected);

    final StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));

    final List<TestStreamable> loaded = in.readStreamableList(TestStreamable::new);

    assertThat(loaded, hasSize(expected.size()));

    for (int i = 0; i < expected.size(); ++i) {
      assertEquals(expected.get(i).value, loaded.get(i).value);
    }

    assertEquals(0, in.available());

    in.close();
    out.close();
  }
  public void testNamedWriteableUnknownNamedWriteable() throws IOException {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
    namedWriteableRegistry.register(
        BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new);
    BytesStreamOutput out = new BytesStreamOutput();
    out.writeNamedWriteable(
        new NamedWriteable() {
          @Override
          public String getWriteableName() {
            return "unknown";
          }

          @Override
          public void writeTo(StreamOutput out) throws IOException {}
        });
    StreamInput in =
        new NamedWriteableAwareStreamInput(
            StreamInput.wrap(BytesReference.toBytes(out.bytes())), namedWriteableRegistry);
    try {
      // no named writeable registered with given name under test category, can write but cannot
      // read it back
      in.readNamedWriteable(BaseNamedWriteable.class);
      fail("read should have failed");
    } catch (IllegalArgumentException e) {
      assertThat(
          e.getMessage(),
          equalTo("unknown named writeable [" + BaseNamedWriteable.class.getName() + "][unknown]"));
    }
  }
 public void testWriteableReaderReturnsWrongName() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   NamedWriteableRegistry namedWriteableRegistry =
       new NamedWriteableRegistry(
           Collections.singletonList(
               new NamedWriteableRegistry.Entry(
                   BaseNamedWriteable.class,
                   TestNamedWriteable.NAME,
                   (StreamInput in) ->
                       new TestNamedWriteable(in) {
                         @Override
                         public String getWriteableName() {
                           return "intentionally-broken";
                         }
                       })));
   TestNamedWriteable namedWriteableIn =
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
   out.writeNamedWriteable(namedWriteableIn);
   byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in =
       new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry);
   assertEquals(in.available(), bytes.length);
   AssertionError e =
       expectThrows(AssertionError.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
   assertThat(
       e.getMessage(),
       endsWith(
           " claims to have a different name [intentionally-broken] than it was read from [test-named-writeable]."));
 }
 private <T extends Throwable> T serialize(T exception) throws IOException {
   ElasticsearchAssertions.assertVersionSerializable(
       VersionUtils.randomVersion(random()), exception);
   BytesStreamOutput out = new BytesStreamOutput();
   out.writeThrowable(exception);
   StreamInput in = StreamInput.wrap(out.bytes());
   return in.readThrowable();
 }
 public void testNamedWriteableNotSupportedWithoutWrapping() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   TestNamedWriteable testNamedWriteable = new TestNamedWriteable("test1", "test2");
   out.writeNamedWriteable(testNamedWriteable);
   StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
   try {
     in.readNamedWriteable(BaseNamedWriteable.class);
     fail("Expected UnsupportedOperationException");
   } catch (UnsupportedOperationException e) {
     assertThat(e.getMessage(), is("can't read named writeable from StreamInput"));
   }
 }
Esempio n. 6
0
  protected void messageReceived(
      byte[] data,
      String action,
      LocalTransport sourceTransport,
      Version version,
      @Nullable final Long sendRequestId) {
    Transports.assertTransportThread();
    try {
      transportServiceAdapter.received(data.length);
      StreamInput stream = StreamInput.wrap(data);
      stream.setVersion(version);

      long requestId = stream.readLong();
      byte status = stream.readByte();
      boolean isRequest = TransportStatus.isRequest(status);
      if (isRequest) {
        ThreadContext threadContext = threadPool.getThreadContext();
        threadContext.readHeaders(stream);
        handleRequest(stream, requestId, data.length, sourceTransport, version);
      } else {
        final TransportResponseHandler handler =
            transportServiceAdapter.onResponseReceived(requestId);
        // ignore if its null, the adapter logs it
        if (handler != null) {
          if (TransportStatus.isError(status)) {
            handleResponseError(stream, handler);
          } else {
            handleResponse(stream, sourceTransport, handler);
          }
        }
      }
    } catch (Throwable e) {
      if (sendRequestId != null) {
        TransportResponseHandler handler =
            sourceTransport.transportServiceAdapter.onResponseReceived(sendRequestId);
        if (handler != null) {
          RemoteTransportException error =
              new RemoteTransportException(nodeName(), localAddress, action, e);
          sourceTransport
              .workers()
              .execute(
                  () -> {
                    ThreadContext threadContext = sourceTransport.threadPool.getThreadContext();
                    try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
                      sourceTransport.handleException(handler, error);
                    }
                  });
        }
      } else {
        logger.warn("Failed to receive message for action [{}]", e, action);
      }
    }
  }
 public void testOptionalWriteableReaderReturnsNull() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   out.writeOptionalWriteable(
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10)));
   StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
   IOException e =
       expectThrows(
           IOException.class, () -> in.readOptionalWriteable((StreamInput ignored) -> null));
   assertThat(
       e.getMessage(),
       endsWith(
           "] returned null which is not allowed and probably means it screwed up the stream."));
 }
 public void testNamedWriteable() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
   namedWriteableRegistry.register(
       BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new);
   TestNamedWriteable namedWriteableIn =
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
   out.writeNamedWriteable(namedWriteableIn);
   byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in =
       new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry);
   assertEquals(in.available(), bytes.length);
   BaseNamedWriteable namedWriteableOut = in.readNamedWriteable(BaseNamedWriteable.class);
   assertEquals(namedWriteableOut, namedWriteableIn);
   assertEquals(in.available(), 0);
 }
  @Test
  public void testSerialization() throws Exception {
    int iterations = randomIntBetween(5, 20);
    for (int i = 0; i < iterations; i++) {

      IndicesOptions indicesOptions =
          IndicesOptions.fromOptions(
              randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
      ClusterStateRequest clusterStateRequest =
          new ClusterStateRequest()
              .routingTable(randomBoolean())
              .metaData(randomBoolean())
              .nodes(randomBoolean())
              .blocks(randomBoolean())
              .indices("testindex", "testindex2")
              .indicesOptions(indicesOptions);

      Version testVersion =
          VersionUtils.randomVersionBetween(
              random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
      BytesStreamOutput output = new BytesStreamOutput();
      output.setVersion(testVersion);
      clusterStateRequest.writeTo(output);

      StreamInput streamInput = StreamInput.wrap(output.bytes());
      streamInput.setVersion(testVersion);
      ClusterStateRequest deserializedCSRequest = new ClusterStateRequest();
      deserializedCSRequest.readFrom(streamInput);

      assertThat(deserializedCSRequest.routingTable(), equalTo(clusterStateRequest.routingTable()));
      assertThat(deserializedCSRequest.metaData(), equalTo(clusterStateRequest.metaData()));
      assertThat(deserializedCSRequest.nodes(), equalTo(clusterStateRequest.nodes()));
      assertThat(deserializedCSRequest.blocks(), equalTo(clusterStateRequest.blocks()));
      assertThat(deserializedCSRequest.indices(), equalTo(clusterStateRequest.indices()));

      if (testVersion.onOrAfter(Version.V_1_5_0)) {
        assertOptionsMatch(
            deserializedCSRequest.indicesOptions(), clusterStateRequest.indicesOptions());
      } else {
        // versions before V_1_5_0 use IndicesOptions.lenientExpandOpen()
        assertOptionsMatch(
            deserializedCSRequest.indicesOptions(), IndicesOptions.lenientExpandOpen());
      }
    }
  }
 public void testNamedWriteableReaderReturnsNull() throws IOException {
   BytesStreamOutput out = new BytesStreamOutput();
   NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
   namedWriteableRegistry.register(
       BaseNamedWriteable.class, TestNamedWriteable.NAME, (StreamInput in) -> null);
   TestNamedWriteable namedWriteableIn =
       new TestNamedWriteable(
           randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
   out.writeNamedWriteable(namedWriteableIn);
   byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in =
       new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry);
   assertEquals(in.available(), bytes.length);
   IOException e =
       expectThrows(IOException.class, () -> in.readNamedWriteable(BaseNamedWriteable.class));
   assertThat(
       e.getMessage(),
       endsWith(
           "] returned null which is not allowed and probably means it screwed up the stream."));
 }
  public void testWriteMapOfLists() throws IOException {
    final int size = randomIntBetween(0, 5);
    final Map<String, List<String>> expected = new HashMap<>(size);

    for (int i = 0; i < size; ++i) {
      int listSize = randomIntBetween(0, 5);
      List<String> list = new ArrayList<>(listSize);

      for (int j = 0; j < listSize; ++j) {
        list.add(randomAsciiOfLength(5));
      }

      expected.put(randomAsciiOfLength(2), list);
    }

    final BytesStreamOutput out = new BytesStreamOutput();
    out.writeMapOfLists(expected);

    final StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));

    final Map<String, List<String>> loaded = in.readMapOfLists();

    assertThat(loaded.size(), equalTo(expected.size()));

    for (Map.Entry<String, List<String>> entry : expected.entrySet()) {
      assertThat(loaded.containsKey(entry.getKey()), equalTo(true));

      List<String> loadedList = loaded.get(entry.getKey());

      assertThat(loadedList, hasSize(entry.getValue().size()));

      for (int i = 0; i < loadedList.size(); ++i) {
        assertEquals(entry.getValue().get(i), loadedList.get(i));
      }
    }

    assertEquals(0, in.available());

    in.close();
    out.close();
  }
 public void testSimpleStreams() throws Exception {
   assumeTrue("requires a 64-bit JRE ... ?!", Constants.JRE_IS_64BIT);
   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.writeOptionalLong(11234234L);
   out.writeFloat(1.1f);
   out.writeDouble(2.2);
   int[] intArray = {1, 2, 3};
   out.writeGenericValue(intArray);
   int[] vIntArray = {4, 5, 6};
   out.writeVIntArray(vIntArray);
   long[] longArray = {1, 2, 3};
   out.writeGenericValue(longArray);
   long[] vLongArray = {4, 5, 6};
   out.writeVLongArray(vLongArray);
   float[] floatArray = {1.1f, 2.2f, 3.3f};
   out.writeGenericValue(floatArray);
   double[] doubleArray = {1.1, 2.2, 3.3};
   out.writeGenericValue(doubleArray);
   out.writeString("hello");
   out.writeString("goodbye");
   out.writeGenericValue(BytesRefs.toBytesRef("bytesref"));
   out.writeStringArray(new String[] {"a", "b", "cat"});
   out.writeBytesReference(new BytesArray("test"));
   out.writeOptionalBytesReference(new BytesArray("test"));
   out.writeOptionalDouble(null);
   out.writeOptionalDouble(1.2);
   out.writeTimeZone(DateTimeZone.forID("CET"));
   out.writeOptionalTimeZone(DateTimeZone.getDefault());
   out.writeOptionalTimeZone(null);
   final byte[] bytes = BytesReference.toBytes(out.bytes());
   StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
   assertEquals(in.available(), bytes.length);
   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(-3L));
   assertThat(in.readVLong(), equalTo(4L));
   assertThat(in.readOptionalLong(), equalTo(11234234L));
   assertThat((double) in.readFloat(), closeTo(1.1, 0.0001));
   assertThat(in.readDouble(), closeTo(2.2, 0.0001));
   assertThat(in.readGenericValue(), equalTo((Object) intArray));
   assertThat(in.readVIntArray(), equalTo(vIntArray));
   assertThat(in.readGenericValue(), equalTo((Object) longArray));
   assertThat(in.readVLongArray(), equalTo(vLongArray));
   assertThat(in.readGenericValue(), equalTo((Object) floatArray));
   assertThat(in.readGenericValue(), equalTo((Object) doubleArray));
   assertThat(in.readString(), equalTo("hello"));
   assertThat(in.readString(), equalTo("goodbye"));
   assertThat(in.readGenericValue(), equalTo((Object) BytesRefs.toBytesRef("bytesref")));
   assertThat(in.readStringArray(), equalTo(new String[] {"a", "b", "cat"}));
   assertThat(in.readBytesReference(), equalTo(new BytesArray("test")));
   assertThat(in.readOptionalBytesReference(), equalTo(new BytesArray("test")));
   assertNull(in.readOptionalDouble());
   assertThat(in.readOptionalDouble(), closeTo(1.2, 0.0001));
   assertEquals(DateTimeZone.forID("CET"), in.readTimeZone());
   assertEquals(DateTimeZone.getDefault(), in.readOptionalTimeZone());
   assertNull(in.readOptionalTimeZone());
   assertEquals(0, in.available());
   in.close();
   out.close();
 }
  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(random()));
    multiGetShardRequest.writeTo(out);

    StreamInput in = StreamInput.wrap(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()));
    assertThat(
        multiGetShardRequest2.ignoreErrorsOnGeneratedFields(),
        equalTo(multiGetShardRequest.ignoreErrorsOnGeneratedFields()));
    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);
      assertThat(item2.index(), equalTo(item.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()));
    }
    assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices()));
    assertThat(
        multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions()));
  }
Esempio n. 14
0
    /** executes the actual test */
    public void execute() throws IOException {
      logger.debug("Keys in 'before' map: {}", keys);
      logger.debug("Keys to remove: {}", keysToRemove);
      logger.debug("Keys to override: {}", keysToOverride);
      logger.debug("Keys to add: {}", keysToAdd);

      logger.debug("--> creating 'before' map");
      Map<Integer, V> before = new HashMap<>();
      for (Integer key : keys) {
        before.put(key, createValue(key, true));
      }
      T beforeMap = createMap(before);

      logger.debug("--> creating 'after' map");
      Map<Integer, V> after = new HashMap<>();
      after.putAll(before);
      for (Integer key : keysToRemove) {
        after.remove(key);
      }
      for (Integer key : keysToOverride) {
        after.put(key, createValue(key, false));
      }
      for (Integer key : keysToAdd) {
        after.put(key, createValue(key, false));
      }
      T afterMap = createMap(unmodifiableMap(after));

      MapDiff<Integer, V, T> diffMap = diff(beforeMap, afterMap);

      // check properties of diffMap
      assertThat(new HashSet(diffMap.getDeletes()), equalTo(keysToRemove));
      if (diffableValues()) {
        assertThat(diffMap.getDiffs().keySet(), equalTo(keysToOverride));
        for (Integer key : keysToOverride) {
          assertThat(
              diffMap.getDiffs().get(key).apply(get(beforeMap, key)), equalTo(get(afterMap, key)));
        }
        assertThat(diffMap.getUpserts().keySet(), equalTo(keysToAdd));
        for (Integer key : keysToAdd) {
          assertThat(diffMap.getUpserts().get(key), equalTo(get(afterMap, key)));
        }
      } else {
        assertThat(diffMap.getDiffs(), equalTo(emptyMap()));
        Set<Integer> keysToAddAndOverride = Sets.union(keysToAdd, keysToOverride);
        assertThat(diffMap.getUpserts().keySet(), equalTo(keysToAddAndOverride));
        for (Integer key : keysToAddAndOverride) {
          assertThat(diffMap.getUpserts().get(key), equalTo(get(afterMap, key)));
        }
      }

      if (randomBoolean()) {
        logger.debug("--> serializing diff");
        BytesStreamOutput out = new BytesStreamOutput();
        diffMap.writeTo(out);
        StreamInput in = StreamInput.wrap(out.bytes());
        logger.debug("--> reading diff back");
        diffMap = readDiff(in);
      }
      T appliedDiffMap = diffMap.apply(beforeMap);

      // check properties of appliedDiffMap
      assertThat(
          size(appliedDiffMap), equalTo(keys.size() - keysToRemove.size() + keysToAdd.size()));
      for (Integer key : keysToRemove) {
        assertThat(get(appliedDiffMap, key), nullValue());
      }
      for (Integer key : keysUnchanged) {
        assertThat(get(appliedDiffMap, key), equalTo(get(beforeMap, key)));
      }
      for (Integer key : keysToOverride) {
        assertThat(get(appliedDiffMap, key), not(equalTo(get(beforeMap, key))));
        assertThat(get(appliedDiffMap, key), equalTo(get(afterMap, key)));
      }
      for (Integer key : keysToAdd) {
        assertThat(get(appliedDiffMap, key), equalTo(get(afterMap, key)));
      }
    }