/*
 * Licensed to Crate.IO GmbH ("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.integrationtests;

import io.crate.testing.SQLResponse;
import io.crate.testing.SQLTransportExecutor;
import io.crate.testing.UseJdbc;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESIntegTestCase;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@ESIntegTestCase.ClusterScope(maxNumDataNodes = 2)
@UseJdbc
public class ThreadPoolsExhaustedIntegrationTest extends SQLTransportIntegrationTest {

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        return Settings.settingsBuilder()
            .put(super.nodeSettings(nodeOrdinal))
            .put("threadpool.search.size", 2)
            .put("threadpool.search.queue_size", 2)
            .build();
    }

    @Test
    public void testRegularSelectWithFewAvailableThreadsShouldNeverGetStuck() throws Exception {
        execute("create table t (x int) with (number_of_replicas = 0)");
        ensureYellow();
        bulkInsert(10);

        List<ActionFuture<SQLResponse>> futures = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            ActionFuture<SQLResponse> future = sqlExecutor.execute(
                "select * from t limit ?", new Object[]{10});
            futures.add(future);
        }

        for (ActionFuture<SQLResponse> future : futures) {
            try {
                future.get(SQLTransportExecutor.REQUEST_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS);
            } catch (TimeoutException e) {
                fail("query run into a timeout");
            } catch (Exception e) {
                assertThat(e.getMessage(), Matchers.anyOf(
                    Matchers.containsString("rejected execution"),
                    // FIXME: the original cause should bubble, not killed - ignore this for now
                    // the main purpose of this test is to make sure queries don't get stuck
                    Matchers.containsString("Job killed")
                ));
            }
        }
    }

    private void bulkInsert(int docCount) {
        Object[][] bulkArgs = new Object[docCount][1];
        for (int i = 0; i < docCount; i++) {
            bulkArgs[i][0] = i;
        }
        execute("insert into t (x) values (?)", bulkArgs);
    }
}

@@ -2806,346 +2806,43 @@
 ers.
-anyOf(%0A                    Matchers.containsString(%22rejected execution%22),%0A                    // FIXME: the original cause should bubble, not killed - ignore this for now%0A                    // the main purpose of this test is to make sure queries don't get stuck%0A                    Matchers.containsString(%22Job killed%22)%0A                
+containsString(%22rejected execution%22
 ));%0A

Remove kill propagation workaround