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.HBaseTestingUtil;
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 org.apache.hadoop.hbase.thrift package.
041 */
042@Category({ ClientTests.class, SmallTests.class })
043@RunWith(Parameterized.class)
044public class TestCallQueue {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestCallQueue.class);
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestCallQueue.class);
051  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
052
053  private static final MetricsAssertHelper metricsHelper =
054    CompatibilitySingletonFactory.getInstance(MetricsAssertHelper.class);
055
056  private int elementsAdded;
057  private int elementsRemoved;
058
059  @Parameters
060  public static Collection<Object[]> getParameters() {
061    Collection<Object[]> parameters = new ArrayList<>();
062    for (int elementsAdded : new int[] { 100, 200, 300 }) {
063      for (int elementsRemoved : new int[] { 0, 20, 100 }) {
064        parameters.add(new Object[] { elementsAdded, elementsRemoved });
065      }
066    }
067    return parameters;
068  }
069
070  public TestCallQueue(int elementsAdded, int elementsRemoved) {
071    this.elementsAdded = elementsAdded;
072    this.elementsRemoved = elementsRemoved;
073    LOG.debug("elementsAdded:" + elementsAdded + " elementsRemoved:" + elementsRemoved);
074
075  }
076
077  @Test
078  public void testPutTake() throws Exception {
079    ThriftMetrics metrics = createMetrics();
080    CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics);
081    for (int i = 0; i < elementsAdded; ++i) {
082      callQueue.put(createDummyRunnable());
083    }
084    for (int i = 0; i < elementsRemoved; ++i) {
085      callQueue.take();
086    }
087    verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved);
088  }
089
090  @Test
091  public void testOfferPoll() throws Exception {
092    ThriftMetrics metrics = createMetrics();
093    CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics);
094    for (int i = 0; i < elementsAdded; ++i) {
095      callQueue.offer(createDummyRunnable());
096    }
097    for (int i = 0; i < elementsRemoved; ++i) {
098      callQueue.poll();
099    }
100    verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved);
101  }
102
103  private static ThriftMetrics createMetrics() throws Exception {
104    Configuration conf = UTIL.getConfiguration();
105    ThriftMetrics m = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.ONE);
106    m.getSource().init();
107    return m;
108  }
109
110  private static void verifyMetrics(ThriftMetrics metrics, String name, int expectValue)
111    throws Exception {
112    metricsHelper.assertCounter(name, expectValue, metrics.getSource());
113  }
114
115  private static Runnable createDummyRunnable() {
116    return new Runnable() {
117      @Override
118      public void run() {
119      }
120    };
121  }
122
123}