<commit_before>/*
 * Licensed 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 com.addthis.hydra.query;

import java.util.concurrent.atomic.AtomicInteger;

import com.addthis.basis.test.SlowTest;

import com.addthis.hydra.data.query.QueryEngine;
import com.addthis.hydra.data.query.QueryEngineDirectory;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(SlowTest.class)
public class TestQueryEngineCache {

    @Test
    public void test() throws Exception {
        QETestCache cache = new QETestCache();
        for (int i = 0; i < 5; i++) {
            (new DummySearcher(cache)).start();
        }
        for (int i = 0; i < 100; i++) {
//			System.out.println(cache.status());
            Assert.assertTrue("Engine cache grew too large at :" + cache.oe.get(), cache.oe.get() <= 30);
            Thread.sleep(250);
        }
    }

    class DummySearcher extends Thread {

        QETestCache cache;

        public DummySearcher(QETestCache cache) {
            this.cache = cache;
            setDaemon(true);
        }

        public void run() {
            while (true) {
                try {
                    //get the engine we need
                    QueryEngine qe = cache.getAndLease("" + (int) (Math.random() * 30));
                    //search
                    Thread.sleep(500 + (int) (Math.random() * 500));
                    //release engine
                    qe.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class QETestCache extends QueryEngineCache {

        static final int engineCacheSize = 25;
        static final int refreshInterval = 1;
        static final int failInterval = 120;
        public AtomicInteger oe = new AtomicInteger(0);

        public QETestCache() {
            super(engineCacheSize, refreshInterval, failInterval);
        }

        public String status() {
            return "status: newOpenedEngines=" + newEnginesOpened.count() + " enginesRefreshed=" + enginesRefreshed.count() + " openEngines=" + oe;
        }

        @Override
        protected QueryEngine createEngine(String dir) throws Exception {<commit_msg>change name of this inner test class to not mess up navigation<commit_after>/*
 * Licensed 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 com.addthis.hydra.query;

import java.util.concurrent.atomic.AtomicInteger;

import com.addthis.basis.test.SlowTest;

import com.addthis.hydra.data.query.QueryEngine;
import com.addthis.hydra.data.query.QueryEngineDirectory;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(SlowTest.class)
public class TestQueryEngineCache {

    @Test
    public void test() throws Exception {
        QueryEngineTestCache cache = new QueryEngineTestCache();
        for (int i = 0; i < 5; i++) {
            (new DummySearcher(cache)).start();
        }
        for (int i = 0; i < 100; i++) {
//			System.out.println(cache.status());
            Assert.assertTrue("Engine cache grew too large at :" + cache.oe.get(), cache.oe.get() <= 30);
            Thread.sleep(250);
        }
    }

    class DummySearcher extends Thread {

        QueryEngineTestCache cache;

        public DummySearcher(QueryEngineTestCache cache) {
            this.cache = cache;
            setDaemon(true);
        }

        public void run() {
            while (true) {
                try {
                    //get the engine we need
                    QueryEngine qe = cache.getAndLease("" + (int) (Math.random() * 30));
                    //search
                    Thread.sleep(500 + (int) (Math.random() * 500));
                    //release engine
                    qe.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class QueryEngineTestCache extends QueryEngineCache {

        static final int engineCacheSize = 25;
        static final int refreshInterval = 1;
        static final int failInterval = 120;
        public AtomicInteger oe = new AtomicInteger(0);

        public QueryEngineTestCache() {
            super(engineCacheSize, refreshInterval, failInterval);
        }

        public String status() {
            return "status: newOpenedEngines=" + newEnginesOpened.count() + " enginesRefreshed=" + enginesRefreshed.count() + " openEngines=" + oe;
        }

        @Override
        protected QueryEngine createEngine(String dir) throws Exception {
{'fraction_non_alphanumeric': 0.09776984834968779, 'fraction_numerical': 0.010704727921498661, 'mean_word_length': 2.2144495412844036, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535453', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1461, 'n_words': 459}