001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.thrift;
019
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.concurrent.LinkedBlockingQueue;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.test.MetricsAssertHelper;
028import org.apache.hadoop.hbase.testclassification.ClientTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.junit.runner.RunWith;
034import org.junit.runners.Parameterized;
035import org.junit.runners.Parameterized.Parameters;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * Unit testing for CallQueue, a part of the
041 * org.apache.hadoop.hbase.thrift package.
042 */
043@Category({ClientTests.class, SmallTests.class})
044@RunWith(Parameterized.class)
045public class TestCallQueue {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestCallQueue.class);
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestCallQueue.class);
052  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
053
054  private static final MetricsAssertHelper metricsHelper =
055      CompatibilitySingletonFactory.getInstance(MetricsAssertHelper.class);
056
057  private int elementsAdded;
058  private int elementsRemoved;
059
060  @Parameters
061  public static Collection<Object[]> getParameters() {
062    Collection<Object[]> parameters = new ArrayList<>();
063    for (int elementsAdded : new int[] {100, 200, 300}) {
064      for (int elementsRemoved : new int[] {0, 20, 100}) {
065        parameters.add(new Object[]{ elementsAdded, elementsRemoved });
066      }
067    }
068    return parameters;
069  }
070
071  public TestCallQueue(int elementsAdded, int elementsRemoved) {
072    this.elementsAdded = elementsAdded;
073    this.elementsRemoved = elementsRemoved;
074    LOG.debug("elementsAdded:" + elementsAdded +
075              " elementsRemoved:" + elementsRemoved);
076
077  }
078
079  @Test
080  public void testPutTake() throws Exception {
081    ThriftMetrics metrics = createMetrics();
082    CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics);
083    for (int i = 0; i < elementsAdded; ++i) {
084      callQueue.put(createDummyRunnable());
085    }
086    for (int i = 0; i < elementsRemoved; ++i) {
087      callQueue.take();
088    }
089    verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved);
090  }
091
092  @Test
093  public void testOfferPoll() throws Exception {
094    ThriftMetrics metrics = createMetrics();
095    CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics);
096    for (int i = 0; i < elementsAdded; ++i) {
097      callQueue.offer(createDummyRunnable());
098    }
099    for (int i = 0; i < elementsRemoved; ++i) {
100      callQueue.poll();
101    }
102    verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved);
103  }
104
105  private static ThriftMetrics createMetrics() throws Exception {
106    Configuration conf = UTIL.getConfiguration();
107    ThriftMetrics m = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.ONE);
108    m.getSource().init();
109    return m;
110  }
111
112
113  private static void verifyMetrics(ThriftMetrics metrics, String name, int expectValue)
114      throws Exception {
115    metricsHelper.assertCounter(name, expectValue, metrics.getSource());
116  }
117
118  private static Runnable createDummyRunnable() {
119    return new Runnable() {
120      @Override
121      public void run() {
122      }
123    };
124  }
125}
126