content: /*
   * Copyright 2021-2024 Aklivity Inc.
   *
   * Aklivity licenses this file to you under the Apache License,
   * version 2.0 (the "License"); you may not use this file except in compliance
   * with the License. You may obtain a copy of the License at:
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   * License for the specific language governing permissions and limitations
   * under the License.
   */
  package io.aklivity.zilla.runtime.binding.kafka.internal.stream;
  
  import static io.aklivity.zilla.runtime.binding.kafka.internal.KafkaConfiguration.KAFKA_CACHE_SEGMENT_BYTES;
  import static io.aklivity.zilla.runtime.binding.kafka.internal.KafkaConfiguration.KAFKA_CACHE_SEGMENT_INDEX_BYTES;
  import static io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_BUFFER_SLOT_CAPACITY;
  import static io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DRAIN_ON_CLOSE;
  import static java.util.concurrent.TimeUnit.SECONDS;
  import static org.junit.rules.RuleChain.outerRule;
  
  import org.junit.Rule;
  import org.junit.Test;
  import org.junit.rules.DisableOnDebug;
  import org.junit.rules.TestRule;
  import org.junit.rules.Timeout;
  
  import io.aklivity.k3po.runtime.junit.annotation.Specification;
  import io.aklivity.k3po.runtime.junit.rules.K3poRule;
  import io.aklivity.zilla.runtime.engine.test.EngineRule;
  import io.aklivity.zilla.runtime.engine.test.annotation.Configuration;
  
  public class CacheBootstrapIT
  {
      private final K3poRule k3po = new K3poRule()
          .addScriptRoot("net", "io/aklivity/zilla/specs/binding/kafka/streams/application/bootstrap")
          .addScriptRoot("app", "io/aklivity/zilla/specs/binding/kafka/streams/application/merged");
  
      private final TestRule timeout = new DisableOnDebug(new Timeout(10, SECONDS));
  
      private final EngineRule engine = new EngineRule()
          .directory("target/zilla-itests")
          .countersBufferCapacity(16384)
          .configure(ENGINE_BUFFER_SLOT_CAPACITY, 8192)
          .configure(KAFKA_CACHE_SEGMENT_BYTES, 1 * 1024 * 1024)
          .configure(KAFKA_CACHE_SEGMENT_INDEX_BYTES, 256 * 1024)
          .configure(ENGINE_DRAIN_ON_CLOSE, false)
          .configurationRoot("io/aklivity/zilla/specs/binding/kafka/config")
          .external("app1")
          .clean();
  
      @Rule
      public final TestRule chain = outerRule(k3po).around(engine).around(timeout);
  
      @Test
      @Configuration("cache.options.bootstrap.yaml")
      @Specification({
          "${app}/unmerged.fetch.message.values/server"})
      public void shouldReceiveMergedMessageValues() throws Exception
      {
          Thread.sleep(500);
          k3po.start();
          k3po.awaitBarrier("CHANGING_PARTITION_COUNT");
          Thread.sleep(200); // allow A1, B1, A2, B2 to be merged
          k3po.notifyBarrier("CHANGED_PARTITION_COUNT");
          k3po.awaitBarrier("SENT_MESSAGE_A2");
          k3po.awaitBarrier("SENT_MESSAGE_B2");
          k3po.awaitBarrier("SENT_MESSAGE_C2");
          k3po.finish();
      }
  
      @Test
      @Configuration("cache.options.bootstrap.live.yaml")
      @Specification({
          "${app}/unmerged.fetch.message.values.live/server"})
      public void shouldReceiveMergedMessageValuesLive() throws Exception
      {
          Thread.sleep(500);
          k3po.start();
          k3po.awaitBarrier("CHANGING_PARTITION_COUNT");
          Thread.sleep(200); // allow A1, B1, A2, B2 to be merged
          k3po.notifyBarrier("CHANGED_PARTITION_COUNT");
          k3po.awaitBarrier("SENT_MESSAGE_A2");
          k3po.awaitBarrier("SENT_MESSAGE_B2");
          k3po.awaitBarrier("SENT_MESSAGE_C2");
          k3po.finish();
      }
  
      @Test
      @Configuration("cache.yaml")
      @Specification({
          "${net}/group.fetch.message.value/client",
          "${app}/unmerged.group.fetch.message.value/server"})
      public void shouldReceiveGroupMessageValue() throws Exception
      {
          k3po.finish();
      }
  }
  