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.apache.hadoop.hbase.MediumTests;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31
32
33
34
35
36 @Category(MediumTests.class)
37 public class TestMemoryBoundedLogMessageBuffer {
38
39 private static final long TEN_KB = 10 * 1024;
40 private static final String JP_TEXT = "こんにちは";
41
42 @Test
43 public void testBuffer() {
44 MemoryBoundedLogMessageBuffer buf =
45 new MemoryBoundedLogMessageBuffer(TEN_KB);
46
47 for (int i = 0; i < 1000; i++) {
48 buf.add("hello " + i);
49 }
50 assertTrue("Usage too big: " + buf.estimateHeapUsage(),
51 buf.estimateHeapUsage() < TEN_KB);
52 assertTrue("Too many retained: " + buf.getMessages().size(),
53 buf.getMessages().size() < 100);
54 StringWriter sw = new StringWriter();
55 buf.dumpTo(new PrintWriter(sw));
56 String dump = sw.toString();
57 assertFalse("The early log messages should be evicted",
58 dump.contains("hello 1\n"));
59 assertTrue("The late log messages should be retained",
60 dump.contains("hello 999\n"));
61 }
62
63 @Test
64 public void testNonAsciiEncoding() {
65 MemoryBoundedLogMessageBuffer buf =
66 new MemoryBoundedLogMessageBuffer(TEN_KB);
67
68 buf.add(JP_TEXT);
69 StringWriter sw = new StringWriter();
70 buf.dumpTo(new PrintWriter(sw));
71 String dump = sw.toString();
72 assertTrue(dump.contains(JP_TEXT));
73 }
74
75 @org.junit.Rule
76 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
77 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
78 }
79