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.List; 022import java.util.concurrent.LinkedBlockingQueue; 023import java.util.stream.Stream; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.CompatibilitySingletonFactory; 026import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.test.MetricsAssertHelper; 029import org.apache.hadoop.hbase.testclassification.ClientTests; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.junit.jupiter.api.Tag; 032import org.junit.jupiter.api.TestTemplate; 033import org.junit.jupiter.params.provider.Arguments; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037/** 038 * Unit testing for CallQueue, a part of the org.apache.hadoop.hbase.thrift package. 039 */ 040@Tag(ClientTests.TAG) 041@Tag(SmallTests.TAG) 042@HBaseParameterizedTestTemplate 043public class TestCallQueue { 044 045 private static final Logger LOG = LoggerFactory.getLogger(TestCallQueue.class); 046 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 047 048 private static final MetricsAssertHelper metricsHelper = 049 CompatibilitySingletonFactory.getInstance(MetricsAssertHelper.class); 050 051 private int elementsAdded; 052 private int elementsRemoved; 053 054 public static Stream<Arguments> parameters() { 055 List<Arguments> params = new ArrayList<>(); 056 for (int elementsAdded : new int[] { 100, 200, 300 }) { 057 for (int elementsRemoved : new int[] { 0, 20, 100 }) { 058 params.add(Arguments.of(elementsAdded, elementsRemoved)); 059 } 060 } 061 return params.stream(); 062 } 063 064 public TestCallQueue(int elementsAdded, int elementsRemoved) { 065 this.elementsAdded = elementsAdded; 066 this.elementsRemoved = elementsRemoved; 067 LOG.debug("elementsAdded:" + elementsAdded + " elementsRemoved:" + elementsRemoved); 068 069 } 070 071 @TestTemplate 072 public void testPutTake() throws Exception { 073 ThriftMetrics metrics = createMetrics(); 074 CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics); 075 for (int i = 0; i < elementsAdded; ++i) { 076 callQueue.put(createDummyRunnable()); 077 } 078 for (int i = 0; i < elementsRemoved; ++i) { 079 callQueue.take(); 080 } 081 verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved); 082 } 083 084 @TestTemplate 085 public void testOfferPoll() throws Exception { 086 ThriftMetrics metrics = createMetrics(); 087 CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics); 088 for (int i = 0; i < elementsAdded; ++i) { 089 callQueue.offer(createDummyRunnable()); 090 } 091 for (int i = 0; i < elementsRemoved; ++i) { 092 callQueue.poll(); 093 } 094 verifyMetrics(metrics, "timeInQueue_num_ops", elementsRemoved); 095 } 096 097 private static ThriftMetrics createMetrics() throws Exception { 098 Configuration conf = UTIL.getConfiguration(); 099 ThriftMetrics m = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.ONE); 100 m.getSource().init(); 101 return m; 102 } 103 104 private static void verifyMetrics(ThriftMetrics metrics, String name, int expectValue) 105 throws Exception { 106 metricsHelper.assertCounter(name, expectValue, metrics.getSource()); 107 } 108 109 private static Runnable createDummyRunnable() { 110 return new Runnable() { 111 @Override 112 public void run() { 113 } 114 }; 115 } 116}