Path: blob/master/test/hotspot/gtest/logging/test_logStream.cpp
41145 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "jvm.h"25#include "logTestFixture.hpp"26#include "logTestUtils.inline.hpp"27#include "logging/log.hpp"28#include "logging/logStream.hpp"29#include "unittest.hpp"3031class LogStreamTest : public LogTestFixture {32protected:33void verify_stream(outputStream* stream);34};3536void LogStreamTest::verify_stream(outputStream* stream) {37set_log_config(TestLogFileName, "gc=debug");38stream->print("%d ", 3);39stream->print("workers");40stream->cr();41EXPECT_TRUE(file_contains_substring(TestLogFileName, "3 workers\n"));42}4344TEST_VM_F(LogStreamTest, from_log) {45Log(gc) log;46LogStream stream(log.debug());4748verify_stream(&stream);49}5051TEST_VM_F(LogStreamTest, from_logtarget) {52LogTarget(Debug, gc) log;53LogStream stream(log);5455verify_stream(&stream);56}5758TEST_VM_F(LogStreamTest, handle) {59LogStreamHandle(Debug, gc) stream;6061verify_stream(&stream);62}6364TEST_VM_F(LogStreamTest, no_rm) {65ResourceMark rm;66LogStream ls(Log(gc)::debug());67verify_stream(&ls);68}6970TEST_VM_F(LogStreamTest, TestLineBufferAllocation) {71const int max_line_len = 1024;72char* const test_string = (char*) os::malloc(max_line_len, mtLogging);73memset(test_string, 'A', max_line_len);74Log(gc) log;75set_log_config(TestLogFileName, "gc=debug");76for (int interval = 1; interval < max_line_len; interval++) {77LogStream ls(log.debug());78int written = 0;79while (written < max_line_len) {80const int to_write = MIN2(interval, max_line_len - written);81ls.write(test_string, interval);82written += interval;83const char* const line_buffer = ls._current_line.buffer();84for (int i = 0; i < written; i++) {85ASSERT_TRUE(line_buffer[i] == 'A');86}87ASSERT_TRUE(line_buffer[written] == '\0');88}89}90}9192// Test, in release build, that the internal line buffer of a LogStream93// object caps out at 1M.94TEST_VM_F(LogStreamTest, TestLineBufferAllocationCap) {95LogStream ls(Log(logging)::info());96for (size_t i = 0; i < (1*M + 512); i ++) {97ls.print_raw("A");98}99const char* const line_buffer = ls._current_line.buffer();100ASSERT_TRUE(strlen(line_buffer) == 1*M - 1);101// reset to prevent assert for unflushed content102ls._current_line.reset();103}104105TEST_VM_F(LogStreamTest, autoflush_on_destruction) {106Log(gc) log;107set_log_config(TestLogFileName, "gc=debug");108{109LogStream stream(log.debug());110stream.print("ABCD"); // Unfinished line. I expect not to assert upon leaving the scope.111}112EXPECT_TRUE(file_contains_substring(TestLogFileName, "ABCD\n"));113}114115116117