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.monitoring; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022 023import java.io.PrintWriter; 024import java.io.StringWriter; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032/** 033 * Test case for the MemoryBoundedLogMessageBuffer utility. Ensures that it uses no more memory than 034 * it's supposed to, and that it properly deals with multibyte encodings. 035 */ 036@Category({ MiscTests.class, SmallTests.class }) 037public class TestMemoryBoundedLogMessageBuffer { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestMemoryBoundedLogMessageBuffer.class); 042 043 private static final long TEN_KB = 10 * 1024; 044 private static final String JP_TEXT = "こんにちは"; 045 046 @Test 047 public void testBuffer() { 048 MemoryBoundedLogMessageBuffer buf = new MemoryBoundedLogMessageBuffer(TEN_KB); 049 050 for (int i = 0; i < 1000; i++) { 051 buf.add("hello " + i); 052 } 053 assertTrue("Usage too big: " + buf.estimateHeapUsage(), buf.estimateHeapUsage() < TEN_KB); 054 assertTrue("Too many retained: " + buf.getMessages().size(), buf.getMessages().size() < 100); 055 StringWriter sw = new StringWriter(); 056 buf.dumpTo(new PrintWriter(sw)); 057 String dump = sw.toString(); 058 String eol = System.getProperty("line.separator"); 059 assertFalse("The early log messages should be evicted", dump.contains("hello 1" + eol)); 060 assertTrue("The late log messages should be retained", dump.contains("hello 999" + eol)); 061 } 062 063 @Test 064 public void testNonAsciiEncoding() { 065 MemoryBoundedLogMessageBuffer buf = new MemoryBoundedLogMessageBuffer(TEN_KB); 066 067 buf.add(JP_TEXT); 068 StringWriter sw = new StringWriter(); 069 buf.dumpTo(new PrintWriter(sw)); 070 String dump = sw.toString(); 071 assertTrue(dump.contains(JP_TEXT)); 072 } 073 074}