1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 #include "precompiled.hpp"
  25 #include "logTestFixture.hpp"
  26 #include "logTestUtils.inline.hpp"
  27 #include "logging/asynclog.hpp"
  28 #include "logging/logAsyncFlusher.hpp"
  29 #include "runtime/interfaceSupport.inline.hpp"
  30 #include "runtime/vmOperations.hpp"
  31 #include "runtime/vmThread.hpp"
  32 #include "unittest.hpp"
  33 
  34 
  35 class AsyncLogTest : public LogTestFixture {
  36  protected:
  37   void verify_stream(outputStream* stream);
  38 };
  39 
  40 void AsyncLogTest::verify_stream(outputStream* stream) {
  41 }
  42 
  43 TEST_VM_F(AsyncLogTest, fifo) {
  44   LinkedListFIFO<int> fifo;
  45   LinkedListImpl<int, ResourceObj::C_HEAP, mtLogging> result;
  46 
  47   fifo.push(1);
  48   fifo.pop_all(&result);
  49   EXPECT_EQ(result.size(), (size_t)1);
  50   EXPECT_EQ(*(result.head()->data()), 1);
  51   result.clear();
  52 
  53   fifo.push(2);
  54   fifo.push(1);
  55   fifo.pop_all(&result);
  56   EXPECT_EQ(result.size(), (size_t)2);
  57   EXPECT_EQ(*(result.head()->data()), 2);
  58   EXPECT_EQ(*(result.head()->next()->data()), 1);
  59   result.clear();
  60 
  61   const int N = 1000;
  62   for (int i=0; i<N; ++i) {
  63     fifo.push(i);
  64   }
  65   fifo.pop_all(&result);
  66   EXPECT_EQ(result.size(), (size_t)N);
  67   LinkedListIterator<int> it(result.head());
  68   for (int i=0; i<N; ++i) {
  69     int* e = it.next();
  70     EXPECT_EQ(*e, i);
  71   }
  72 }
  73 
  74 class VM_TestFlusher: public VM_GTestExecuteAtSafepoint {
  75 public:
  76   void doit() {
  77     LogStream ls(AsyncLog(logging)::info());
  78     outputStream* os = &ls;
  79     os->print_cr("LogStreamWithAsyncLogImpl");
  80     os->print_cr("LogStreamWithAsyncLogImpl secondline");
  81 
  82     //multi-lines
  83     os->print("logStream msg1-");
  84     os->print("msg2-");
  85     os->print("msg3\n");
  86     os->print_cr("logStream newline");
  87 
  88     test_asynclog_raw();
  89   }
  90 
  91   void test_asynclog_raw() {
  92     AsyncLog(logging) logger;
  93 #define LOG_LEVEL(level, name) logger.name("1" #level);
  94 LOG_LEVEL_LIST
  95 #undef LOG_LEVEL
  96 
  97     AsyncLogTarget(Trace, logging) t;
  98     AsyncLogTarget(Debug, logging) d;
  99     EXPECT_FALSE(t.is_enabled());
 100     EXPECT_TRUE(d.is_enabled());
 101 
 102     d.print("AsyncLogTarget.print = %d", 1);
 103     asynclog_trace(logging)("log_trace-test");
 104     asynclog_debug(logging)("log_debug-test");
 105   }
 106 };
 107 
 108 TEST_VM_F(AsyncLogTest, asynclog) {
 109   set_log_config(TestLogFileName, "logging=debug");
 110 
 111   VM_TestFlusher op;
 112   ThreadInVMfromNative invm(JavaThread::current());
 113   VMThread::execute(&op);
 114 
 115   LogAsyncFlusher::cleanup();
 116   EXPECT_TRUE(file_contains_substring(TestLogFileName, "LogStreamWithAsyncLogImpl"));
 117   EXPECT_TRUE(file_contains_substring(TestLogFileName, "logStream msg1-msg2-msg3"));
 118   EXPECT_TRUE(file_contains_substring(TestLogFileName, "logStream newline"));
 119 
 120   EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Debug"));
 121   EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Info"));
 122   EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Warning"));
 123   EXPECT_TRUE(file_contains_substring(TestLogFileName, "1Error"));
 124   EXPECT_FALSE(file_contains_substring(TestLogFileName, "1Trace")); // trace message is masked out
 125 
 126   EXPECT_TRUE(file_contains_substring(TestLogFileName, "AsyncLogTarget.print = 1"));
 127   EXPECT_FALSE(file_contains_substring(TestLogFileName, "log_trace-test")); // trace message is masked out
 128   EXPECT_TRUE(file_contains_substring(TestLogFileName, "log_debug-test"));
 129 }