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