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