View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.thrift;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.concurrent.LinkedBlockingQueue;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.testclassification.SmallTests;
34  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
35  import org.apache.hadoop.hbase.thrift.CallQueue.Call;
36  import org.junit.experimental.categories.Category;
37  import org.junit.runner.RunWith;
38  import org.junit.runners.Parameterized;
39  import org.junit.runners.Parameterized.Parameters;
40  import org.junit.Test;
41  
42  /**
43   * Unit testing for CallQueue, a part of the
44   * org.apache.hadoop.hbase.thrift package.
45   */
46  @Category(SmallTests.class)
47  @RunWith(Parameterized.class)
48  public class TestCallQueue {
49  
50    private static final Log LOG = LogFactory.getLog(TestCallQueue.class);
51    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52  
53    private static final MetricsAssertHelper metricsHelper =
54        CompatibilitySingletonFactory.getInstance(MetricsAssertHelper.class);
55  
56    private int elementsAdded;
57    private int elementsRemoved;
58  
59    @Parameters
60    public static Collection<Object[]> getParameters() {
61      Collection<Object[]> parameters = new ArrayList<Object[]>();
62      for (int elementsAdded : new int[] {100, 200, 300}) {
63        for (int elementsRemoved : new int[] {0, 20, 100}) {
64          parameters.add(new Object[]{new Integer(elementsAdded),
65                                      new Integer(elementsRemoved)});
66        }
67      }
68      return parameters;
69    }
70  
71    public TestCallQueue(int elementsAdded, int elementsRemoved) {
72      this.elementsAdded = elementsAdded;
73      this.elementsRemoved = elementsRemoved;
74      LOG.debug("elementsAdded:" + elementsAdded +
75                " elementsRemoved:" + elementsRemoved);
76  
77    }
78  
79    @Test(timeout = 60000)
80    public void testPutTake() throws Exception {
81      ThriftMetrics metrics = createMetrics();
82      CallQueue callQueue = new CallQueue(
83          new LinkedBlockingQueue<Call>(), metrics);
84      for (int i = 0; i < elementsAdded; ++i) {
85        callQueue.put(createDummyRunnable());
86      }
87      for (int i = 0; i < elementsRemoved; ++i) {
88        callQueue.take();
89      }
90      verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved);
91    }
92  
93    @Test(timeout = 60000)
94    public void testOfferPoll() throws Exception {
95      ThriftMetrics metrics = createMetrics();
96      CallQueue callQueue = new CallQueue(
97          new LinkedBlockingQueue<Call>(), metrics);
98      for (int i = 0; i < elementsAdded; ++i) {
99        callQueue.offer(createDummyRunnable());
100     }
101     for (int i = 0; i < elementsRemoved; ++i) {
102       callQueue.poll();
103     }
104     verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved);
105   }
106 
107   private static ThriftMetrics createMetrics() throws Exception {
108     Configuration conf = UTIL.getConfiguration();
109     ThriftMetrics m = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.ONE);
110     m.getSource().init();
111     return m;
112   }
113 
114 
115   private static void verifyMetrics(ThriftMetrics metrics, String name, int expectValue)
116       throws Exception { 
117       metricsHelper.assertCounter(name, expectValue, metrics.getSource());
118   }
119 
120   private static Runnable createDummyRunnable() {
121     return new Runnable() {
122       @Override
123       public void run() {
124       }
125     };
126   }
127 
128 }
129