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 */ 018 019package org.apache.hadoop.hbase.ipc; 020 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertFalse; 023import static org.junit.Assert.assertTrue; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.log4j.Level; 029import org.apache.log4j.Logger; 030import org.junit.BeforeClass; 031import org.junit.ClassRule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034import org.mockito.Mockito; 035 036 037@Category(SmallTests.class) 038public class TestRpcServerTraceLogging { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule 042 .forClass(TestRpcServerTraceLogging.class); 043 044 static Logger rpcServerLog = Logger.getLogger(RpcServer.class); 045 046 static final String TRACE_LOG_MSG = 047 "This is dummy message for testing:: region { type: REGION_NAME value: \"hbase:meta,,1\" }" 048 + " scan { column { family: \"info\" } time_range { from: 0 to: 9223372036854775807 } " 049 + "max_versions: 1 cache_blocks: true max_result_size: 2097152 caching: 2147483647 } " 050 + "number_of_rows: 2147483647 close_scanner: false client_handles_partials: " 051 + "true client_handles_heartbeats: true track_scan_metrics: false"; 052 053 static final int TRACE_LOG_LENGTH = TRACE_LOG_MSG.length(); 054 055 static final RpcServer mockRpcServer = Mockito.mock(RpcServer.class); 056 057 static final Configuration conf = new Configuration(false); 058 059 @BeforeClass 060 public static void setUp() { 061 Mockito.when(mockRpcServer.getConf()).thenReturn(conf); 062 Mockito.when(mockRpcServer.truncateTraceLog(Mockito.any(String.class))).thenCallRealMethod(); 063 } 064 065 @Test 066 public void testLoggingWithTraceOff() { 067 conf.setInt("hbase.ipc.trace.log.max.length", 250); 068 rpcServerLog.setLevel(Level.DEBUG); 069 String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG); 070 071 assertEquals(150 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length()); 072 assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED)); 073 } 074 075 @Test 076 public void testLoggingWithTraceOn() { 077 conf.setInt("hbase.ipc.trace.log.max.length", 250); 078 rpcServerLog.setLevel(Level.TRACE); 079 String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG); 080 081 assertEquals(250 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length()); 082 assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED)); 083 } 084 085 @Test 086 public void testLoggingWithTraceOnLargeMax() { 087 conf.setInt("hbase.ipc.trace.log.max.length", 2000); 088 rpcServerLog.setLevel(Level.TRACE); 089 String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG); 090 091 assertEquals(TRACE_LOG_LENGTH, truncatedString.length()); 092 assertFalse( 093 mockRpcServer.truncateTraceLog(TRACE_LOG_MSG).contains(RpcServer.KEY_WORD_TRUNCATED)); 094 } 095}