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.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.SmallTests;
33  import org.apache.hadoop.hbase.thrift.CallQueue.Call;
34  import org.apache.hadoop.hbase.thrift.generated.Hbase;
35  import org.apache.hadoop.metrics.ContextFactory;
36  import org.apache.hadoop.metrics.MetricsContext;
37  import org.apache.hadoop.metrics.MetricsUtil;
38  import org.apache.hadoop.metrics.spi.NoEmitMetricsContext;
39  import org.apache.hadoop.metrics.spi.OutputRecord;
40  import org.junit.experimental.categories.Category;
41  import org.junit.runner.RunWith;
42  import org.junit.runners.Parameterized;
43  import org.junit.runners.Parameterized.Parameters;
44  import org.junit.Test;
45  
46  /**
47   * Unit testing for CallQueue, a part of the
48   * org.apache.hadoop.hbase.thrift package.
49   */
50  @Category(SmallTests.class)
51  @RunWith(Parameterized.class)
52  public class TestCallQueue {
53  
54    public static final Log LOG = LogFactory.getLog(TestCallQueue.class);
55    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
56  
57    private int elementsAdded;
58    private int elementsRemoved;
59  
60    @Parameters
61    public static Collection<Object[]> getParameters() {
62      Collection<Object[]> parameters = new ArrayList<Object[]>();
63      for (int elementsAdded : new int[] {100, 200, 300}) {
64        for (int elementsRemoved : new int[] {0, 20, 100}) {
65          parameters.add(new Object[]{new Integer(elementsAdded),
66                                      new Integer(elementsRemoved)});
67        }
68      }
69      return parameters;
70    }
71  
72    public TestCallQueue(int elementsAdded, int elementsRemoved) {
73      this.elementsAdded = elementsAdded;
74      this.elementsRemoved = elementsRemoved;
75      LOG.debug("elementsAdded:" + elementsAdded +
76                " elementsRemoved:" + elementsRemoved);
77    }
78  
79    @Test(timeout=3000)
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=3000)
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     setupMetricsContext();
109     Configuration conf = UTIL.getConfiguration();
110     return new ThriftMetrics(
111         ThriftServerRunner.DEFAULT_LISTEN_PORT, conf, Hbase.Iface.class);
112   }
113 
114   private static void setupMetricsContext() throws Exception {
115     ContextFactory factory = ContextFactory.getFactory();
116     factory.setAttribute(ThriftMetrics.CONTEXT_NAME + ".class",
117         NoEmitMetricsContext.class.getName());
118     MetricsUtil.getContext(ThriftMetrics.CONTEXT_NAME)
119                .createRecord(ThriftMetrics.CONTEXT_NAME).remove();
120   }
121 
122   private static void verifyMetrics(ThriftMetrics metrics, String name, int expectValue)
123       throws Exception { 
124     MetricsContext context = MetricsUtil.getContext( 
125         ThriftMetrics.CONTEXT_NAME); 
126     metrics.doUpdates(context); 
127     OutputRecord record = context.getAllRecords().get( 
128         ThriftMetrics.CONTEXT_NAME).iterator().next(); 
129     assertEquals(expectValue, record.getMetric(name).intValue()); 
130   }
131 
132   private static Runnable createDummyRunnable() {
133     return new Runnable() {
134       @Override
135       public void run() {
136       }
137     };
138   }
139 
140   @org.junit.Rule
141   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
142     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
143 }
144