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. 034 * Ensures that it uses no more memory than it's supposed to, 035 * and that it properly deals with multibyte encodings. 036 */ 037@Category({MiscTests.class, SmallTests.class}) 038public class TestMemoryBoundedLogMessageBuffer { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestMemoryBoundedLogMessageBuffer.class); 043 044 private static final long TEN_KB = 10 * 1024; 045 private static final String JP_TEXT = "こんにちは"; 046 047 @Test 048 public void testBuffer() { 049 MemoryBoundedLogMessageBuffer buf = 050 new MemoryBoundedLogMessageBuffer(TEN_KB); 051 052 for (int i = 0; i < 1000; i++) { 053 buf.add("hello " + i); 054 } 055 assertTrue("Usage too big: " + buf.estimateHeapUsage(), 056 buf.estimateHeapUsage() < TEN_KB); 057 assertTrue("Too many retained: " + buf.getMessages().size(), 058 buf.getMessages().size() < 100); 059 StringWriter sw = new StringWriter(); 060 buf.dumpTo(new PrintWriter(sw)); 061 String dump = sw.toString(); 062 String eol = System.getProperty("line.separator"); 063 assertFalse("The early log messages should be evicted", 064 dump.contains("hello 1" + eol)); 065 assertTrue("The late log messages should be retained", 066 dump.contains("hello 999" + eol)); 067 } 068 069 @Test 070 public void testNonAsciiEncoding() { 071 MemoryBoundedLogMessageBuffer buf = 072 new MemoryBoundedLogMessageBuffer(TEN_KB); 073 074 buf.add(JP_TEXT); 075 StringWriter sw = new StringWriter(); 076 buf.dumpTo(new PrintWriter(sw)); 077 String dump = sw.toString(); 078 assertTrue(dump.contains(JP_TEXT)); 079 } 080 081} 082