APACHE-KYLIN-2902: add unit test Signed-off-by: lidongsjtu <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/1fbd51bf Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/1fbd51bf Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/1fbd51bf Branch: refs/heads/master Commit: 1fbd51bf1f1038ad5c5151868e1cb259335dc396 Parents: f6b2d70 Author: Zhong <[email protected]> Authored: Sun Dec 3 19:21:38 2017 +0800 Committer: lidongsjtu <[email protected]> Committed: Wed Dec 20 23:20:11 2017 +0800 ---------------------------------------------------------------------- .../apache/kylin/rest/util/RequestUtilTest.java | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/1fbd51bf/server-base/src/test/java/org/apache/kylin/rest/util/RequestUtilTest.java ---------------------------------------------------------------------- diff --git a/server-base/src/test/java/org/apache/kylin/rest/util/RequestUtilTest.java b/server-base/src/test/java/org/apache/kylin/rest/util/RequestUtilTest.java new file mode 100644 index 0000000..5445a86 --- /dev/null +++ b/server-base/src/test/java/org/apache/kylin/rest/util/RequestUtilTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 org.apache.kylin.rest.util; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Test; + +public class RequestUtilTest { + + @Test + public void testOpenAndCloseQueryRequest() { + int nThread = 5; + + final Integer maxConcurrentQuery = 2; + final String project = "test"; + + final AtomicInteger nQueryFailed = new AtomicInteger(0); + + Thread[] threads = new Thread[nThread]; + final CountDownLatch lock = new CountDownLatch(nThread); + for (int i = 0; i < nThread; i++) { + final int j = i; + threads[j] = new Thread(new Runnable() { + @Override + public void run() { + try { + boolean ifOpen = RequestUtil.openQueryRequest(project, maxConcurrentQuery); + lock.countDown(); + if (ifOpen) { + lock.await(); + RequestUtil.closeQueryRequest(project); + } else { + nQueryFailed.incrementAndGet(); + } + } catch (InterruptedException e) { + } + } + }); + threads[j].start(); + } + for (int i = 0; i < nThread; i++) { + try { + threads[i].join(); + } catch (InterruptedException e) { + } + } + Assert.assertEquals(new Integer(0), RequestUtil.getCurrentRunningQuery(project)); + Assert.assertEquals(nThread - maxConcurrentQuery, nQueryFailed.get()); + } +}
