@@ -0,0 +1,3348 @@
+/*%0A * Licensed to Crate.IO GmbH (%22Crate%22) under one or more contributor%0A * license agreements.  See the NOTICE file distributed with this work for%0A * additional information regarding copyright ownership.  Crate licenses%0A * this file to you under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.  You may%0A * obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS, WITHOUT%0A * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the%0A * License for the specific language governing permissions and limitations%0A * under the License.%0A *%0A * However, if you have executed another commercial license agreement%0A * with Crate these terms will supersede the license and you may use the%0A * software solely pursuant to the terms of the relevant commercial agreement.%0A */%0A%0Apackage io.crate.integrationtests;%0A%0Aimport io.crate.action.sql.SQLAction;%0Aimport io.crate.action.sql.SQLRequest;%0Aimport io.crate.action.sql.SQLResponse;%0Aimport org.elasticsearch.action.ActionFuture;%0Aimport org.elasticsearch.common.settings.ImmutableSettings;%0Aimport org.elasticsearch.common.settings.Settings;%0Aimport org.elasticsearch.test.ElasticsearchIntegrationTest;%0Aimport org.hamcrest.Matchers;%0Aimport org.junit.Test;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.TimeUnit;%0A%0A@ElasticsearchIntegrationTest.ClusterScope(maxNumDataNodes = 2)%0Apublic class ThreadPoolsExhaustedIntegrationTest extends SQLTransportIntegrationTest %7B%0A%0A    @Override%0A    protected Settings nodeSettings(int nodeOrdinal) %7B%0A        return ImmutableSettings.settingsBuilder()%0A                .put(super.nodeSettings(nodeOrdinal))%0A                .put(%22threadpool.search.size%22, 2)%0A                .put(%22threadpool.search.queue_size%22, 2)%0A                .build();%0A    %7D%0A%0A    @Test%0A    public void testRegularSelectWithFewAvailableThreadsShouldNeverGetStuck() throws Exception %7B%0A        execute(%22create table t (x int) with (number_of_replicas = 0)%22);%0A        ensureYellow();%0A        bulkInsert(10);%0A%0A        List%3CActionFuture%3CSQLResponse%3E%3E futures = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C 1000; i++) %7B%0A            ActionFuture%3CSQLResponse%3E future = client().execute(%0A                    SQLAction.INSTANCE, new SQLRequest(%22select * from t limit ?%22, new Object%5B%5D %7B 10 %7D));%0A            futures.add(future);%0A        %7D%0A%0A        for (ActionFuture%3CSQLResponse%3E future : futures) %7B%0A            try %7B%0A                future.get(500, TimeUnit.MILLISECONDS);%0A            %7D catch (Exception e) %7B%0A                assertThat(e.getMessage(), Matchers.containsString(%22rejected execution%22));%0A            %7D%0A        %7D%0A%0A        /**%0A         * if there is a rejection exception somewhere the query execution is aborted *before*%0A         * all contexts are created. We'll leave it up to the context reaper to remove dangling contexts%0A         */%0A        runJobContextReapers();%0A    %7D%0A%0A    private void bulkInsert(int docCount) %7B%0A        Object%5B%5D%5B%5D bulkArgs = new Object%5BdocCount%5D%5B1%5D;%0A        for (int i = 0; i %3C docCount; i++) %7B%0A            bulkArgs%5Bi%5D%5B0%5D = i;%0A        %7D%0A        execute(%22insert into t (x) values (?)%22, bulkArgs);%0A    %7D%0A%7D%0A

add an integration test with few available threads