@Test
  public void testIndexBufferSizeUpdateAfterCreationRemoval() throws InterruptedException {

    createNode(ImmutableSettings.EMPTY);

    prepareCreate("test1")
        .setSettings(
            IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
        .get();

    ensureGreen();

    final IndexShard shard1 =
        internalCluster().getInstance(IndicesService.class).indexService("test1").shard(0);

    prepareCreate("test2")
        .setSettings(
            IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
        .get();

    ensureGreen();

    final IndexShard shard2 =
        internalCluster().getInstance(IndicesService.class).indexService("test2").shard(0);
    final long expected1ShardSize =
        internalCluster().getInstance(IndexingMemoryController.class).indexingBufferSize().bytes();
    final long expected2ShardsSize = expected1ShardSize / 2;

    boolean success =
        awaitBusy(
            new Predicate<Object>() {
              @Override
              public boolean apply(Object input) {
                return shard1.engine().config().getIndexingBufferSize().bytes()
                        <= expected2ShardsSize
                    && shard2.engine().config().getIndexingBufferSize().bytes()
                        <= expected2ShardsSize;
              }
            });

    if (!success) {
      fail(
          "failed to update shard indexing buffer size. expected ["
              + expected2ShardsSize
              + "] shard1 ["
              + shard1.engine().config().getIndexingBufferSize().bytes()
              + "] shard2  ["
              + shard2.engine().config().getIndexingBufferSize().bytes()
              + "]");
    }

    client().admin().indices().prepareDelete("test2").get();
    success =
        awaitBusy(
            new Predicate<Object>() {
              @Override
              public boolean apply(Object input) {
                return shard1.engine().config().getIndexingBufferSize().bytes()
                    >= expected1ShardSize;
              }
            });

    if (!success) {
      fail(
          "failed to update shard indexing buffer size after deleting shards. expected ["
              + expected1ShardSize
              + "] got ["
              + shard1.engine().config().getIndexingBufferSize().bytes()
              + "]");
    }
  }
  @Test
  public void testIndexBufferSizeUpdateInactiveShard() throws InterruptedException {

    createNode(
        ImmutableSettings.builder().put("indices.memory.shard_inactive_time", "100ms").build());

    prepareCreate("test1")
        .setSettings(
            IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
        .get();

    ensureGreen();

    final IndexShard shard1 =
        internalCluster().getInstance(IndicesService.class).indexService("test1").shard(0);
    boolean success =
        awaitBusy(
            new Predicate<Object>() {
              @Override
              public boolean apply(Object input) {
                return shard1.engine().config().getIndexingBufferSize().bytes()
                    == EngineConfig.INACTIVE_SHARD_INDEXING_BUFFER.bytes();
              }
            });
    if (!success) {
      fail(
          "failed to update shard indexing buffer size due to inactive state. expected ["
              + EngineConfig.INACTIVE_SHARD_INDEXING_BUFFER
              + "] got ["
              + shard1.engine().config().getIndexingBufferSize().bytes()
              + "]");
    }

    index("test1", "type", "1", "f", 1);

    success =
        awaitBusy(
            new Predicate<Object>() {
              @Override
              public boolean apply(Object input) {
                return shard1.engine().config().getIndexingBufferSize().bytes()
                    > EngineConfig.INACTIVE_SHARD_INDEXING_BUFFER.bytes();
              }
            });
    if (!success) {
      fail(
          "failed to update shard indexing buffer size due to inactive state. expected something larger then ["
              + EngineConfig.INACTIVE_SHARD_INDEXING_BUFFER
              + "] got ["
              + shard1.engine().config().getIndexingBufferSize().bytes()
              + "]");
    }

    flush(); // clean translogs

    success =
        awaitBusy(
            new Predicate<Object>() {
              @Override
              public boolean apply(Object input) {
                return shard1.engine().config().getIndexingBufferSize().bytes()
                    == EngineConfig.INACTIVE_SHARD_INDEXING_BUFFER.bytes();
              }
            });
    if (!success) {
      fail(
          "failed to update shard indexing buffer size due to inactive state. expected ["
              + EngineConfig.INACTIVE_SHARD_INDEXING_BUFFER
              + "] got ["
              + shard1.engine().config().getIndexingBufferSize().bytes()
              + "]");
    }
  }