/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase.monitoring;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
 * Test case for the MemoryBoundedLogMessageBuffer utility. Ensures that it uses no more memory than
 * it's supposed to, and that it properly deals with multibyte encodings.
 */
@Tag(MiscTests.TAG)
@Tag(SmallTests.TAG)
public class TestMemoryBoundedLogMessageBuffer {

  private static final long TEN_KB = 10 * 1024;
  private static final String JP_TEXT = "こんにちは";

  @Test
  public void testBuffer() {
    MemoryBoundedLogMessageBuffer buf = new MemoryBoundedLogMessageBuffer(TEN_KB);

    for (int i = 0; i < 1000; i++) {
      buf.add("hello " + i);
    }
    assertTrue(buf.estimateHeapUsage() < TEN_KB, "Usage too big: " + buf.estimateHeapUsage());
    assertTrue(buf.getMessages().size() < 100, "Too many retained: " + buf.getMessages().size());
    StringWriter sw = new StringWriter();
    buf.dumpTo(new PrintWriter(sw));
    String dump = sw.toString();
    String eol = System.getProperty("line.separator");
    assertFalse(dump.contains("hello 1" + eol), "The early log messages should be evicted");
    assertTrue(dump.contains("hello 999" + eol), "The late log messages should be retained");
  }

  @Test
  public void testNonAsciiEncoding() {
    MemoryBoundedLogMessageBuffer buf = new MemoryBoundedLogMessageBuffer(TEN_KB);

    buf.add(JP_TEXT);
    StringWriter sw = new StringWriter();
    buf.dumpTo(new PrintWriter(sw));
    String dump = sw.toString();
    assertTrue(dump.contains(JP_TEXT));
  }

}
