/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.operation.projectors;

import io.crate.testing.BatchSimulatingIterator;
import io.crate.testing.TestingBatchIterators;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.common.unit.TimeValue;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.MatcherAssert.assertThat;

public class BatchIteratorBackpressureExecutorTest {

    private ExecutorService executor;
    private ScheduledExecutorService scheduler;

    @Before
    public void setUp() throws Exception {
        executor = Executors.newFixedThreadPool(4);
        scheduler = Executors.newSingleThreadScheduledExecutor();
    }

    @After
    public void tearDown() throws Exception {
        executor.shutdown();
        scheduler.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
        scheduler.awaitTermination(10, TimeUnit.SECONDS);
    }

    @Test
    public void testPauseOnFirstBatch() throws Exception {
        BatchSimulatingIterator it = new BatchSimulatingIterator(TestingBatchIterators.range(0, 10), 2, 5, executor);
        AtomicInteger numRows = new AtomicInteger(0);
        AtomicInteger numBatches = new AtomicInteger(0);
        AtomicInteger numPauses = new AtomicInteger(0);
        BatchIteratorBackpressureExecutor<Object> executor = new BatchIteratorBackpressureExecutor<>(
            it,
            scheduler,
            r -> numRows.incrementAndGet(),
            () -> CompletableFuture.supplyAsync(() -> {
                numBatches.incrementAndGet();
                int rowsReceived = numRows.getAndSet(0);
                assertThat("rowsReceived must not be larger than the bulkSize", rowsReceived, Matchers.lessThanOrEqualTo(3));
                return null;
            }, this.executor),
            () -> {
                if (numBatches.get() == 0 && numPauses.get() == 0) {
                    numPauses.incrementAndGet();
                    return true;
                }
                return false;
            },
            3,
            BackoffPolicy.exponentialBackoff(TimeValue.timeValueNanos(10), 1000)
        );
        CompletableFuture<Void> result = executor.consumeIteratorAndExecute();

        result.get(10, TimeUnit.SECONDS);

        assertThat(numPauses.get(), Matchers.is(1));
        assertThat(numBatches.get(), Matchers.is(4));
    }
}

@@ -1032,16 +1032,64 @@
 ctors;%0A%0A
+import io.crate.test.integration.CrateUnitTest;%0A
 import i
@@ -1129,16 +1129,16 @@
 erator;%0A
-
 import i
@@ -1657,62 +1657,8 @@
 r;%0A%0A
-import static org.hamcrest.MatcherAssert.assertThat;%0A%0A
 publ
@@ -1704,16 +1704,38 @@
 torTest 
+extends CrateUnitTest 
 %7B%0A%0A    p
@@ -1848,13 +1848,20 @@
 oid 
-setUp
+initExecutor
 () t
@@ -2034,16 +2034,24 @@
 oid 
-tearDown
+shutdownExecutor
 () t
@@ -2810,91 +2810,9 @@
  -%3E 
-CompletableFuture.supplyAsync(() -%3E %7B%0A                numBatches.incrementAndGet();
+%7B
 %0A   
@@ -2987,24 +2987,25 @@
 qualTo(3));%0A
+%0A
             
@@ -3019,25 +3019,88 @@
 urn 
-null;%0A           
+CompletableFuture.supplyAsync(() -%3E %7B numBatches.incrementAndGet(); return null;
  %7D, 
@@ -3113,16 +3113,31 @@
 xecutor)
+;%0A            %7D
 ,%0A      

Fix flaky `testPauseOnFirstBatch`