1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.monitoring;
21
22 import static org.junit.Assert.*;
23
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26
27 import org.junit.Test;
28
29
30
31
32
33
34 public class TestMemoryBoundedLogMessageBuffer {
35
36 private static final long TEN_KB = 10 * 1024;
37 private static final String JP_TEXT = "こんにちは";
38
39 @Test
40 public void testBuffer() {
41 MemoryBoundedLogMessageBuffer buf =
42 new MemoryBoundedLogMessageBuffer(TEN_KB);
43
44 for (int i = 0; i < 1000; i++) {
45 buf.add("hello " + i);
46 }
47 assertTrue("Usage too big: " + buf.estimateHeapUsage(),
48 buf.estimateHeapUsage() < TEN_KB);
49 assertTrue("Too many retained: " + buf.getMessages().size(),
50 buf.getMessages().size() < 100);
51 StringWriter sw = new StringWriter();
52 buf.dumpTo(new PrintWriter(sw));
53 String dump = sw.toString();
54 assertFalse("The early log messages should be evicted",
55 dump.contains("hello 1\n"));
56 assertTrue("The late log messages should be retained",
57 dump.contains("hello 999\n"));
58 }
59
60 @Test
61 public void testNonAsciiEncoding() {
62 MemoryBoundedLogMessageBuffer buf =
63 new MemoryBoundedLogMessageBuffer(TEN_KB);
64
65 buf.add(JP_TEXT);
66 StringWriter sw = new StringWriter();
67 buf.dumpTo(new PrintWriter(sw));
68 String dump = sw.toString();
69 assertTrue(dump.contains(JP_TEXT));
70 }
71 }