8170976: [TESTBUG] LogTestFixture does not restore previous logging state
authormlarsson
Mon, 26 Feb 2018 16:33:48 +0100
changeset 49171 6ecca4a9c75f
parent 49170 49e095e658c7
child 49172 f047fae0169c
8170976: [TESTBUG] LogTestFixture does not restore previous logging state Reviewed-by: rehn, lfoltan, hseigel
src/hotspot/share/logging/logConfiguration.hpp
test/hotspot/gtest/gc/g1/test_g1HeapVerifier.cpp
test/hotspot/gtest/logging/logTestFixture.cpp
test/hotspot/gtest/logging/logTestFixture.hpp
--- a/src/hotspot/share/logging/logConfiguration.hpp	Mon Feb 26 18:04:43 2018 +0100
+++ b/src/hotspot/share/logging/logConfiguration.hpp	Mon Feb 26 16:33:48 2018 +0100
@@ -38,6 +38,7 @@
 // are iterated over and updated accordingly.
 class LogConfiguration : public AllStatic {
  friend class VMError;
+ friend class LogTestFixture;
  public:
   // Function for listeners
   typedef void (*UpdateListenerFunction)(void);
--- a/test/hotspot/gtest/gc/g1/test_g1HeapVerifier.cpp	Mon Feb 26 18:04:43 2018 +0100
+++ b/test/hotspot/gtest/gc/g1/test_g1HeapVerifier.cpp	Mon Feb 26 16:33:48 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,9 +25,13 @@
 #include "precompiled.hpp"
 #include "gc/g1/g1HeapVerifier.hpp"
 #include "logging/logConfiguration.hpp"
+#include "logging/logTestFixture.hpp"
 #include "unittest.hpp"
 
-TEST(G1HeapVerifier, parse) {
+class G1HeapVerifierTest : public LogTestFixture {
+};
+
+TEST_F(G1HeapVerifierTest, parse) {
   G1HeapVerifier verifier(NULL);
 
   LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc, verify));
--- a/test/hotspot/gtest/logging/logTestFixture.cpp	Mon Feb 26 18:04:43 2018 +0100
+++ b/test/hotspot/gtest/logging/logTestFixture.cpp	Mon Feb 26 16:33:48 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
 #include "logTestFixture.hpp"
 #include "logTestUtils.inline.hpp"
 #include "logging/logConfiguration.hpp"
+#include "logging/logOutput.hpp"
 #include "memory/resourceArea.hpp"
 #include "unittest.hpp"
 #include "utilities/ostream.hpp"
@@ -38,10 +39,12 @@
                          ::testing::UnitTest::GetInstance()->current_test_info()->name());
   EXPECT_GT(ret, 0) << "_filename buffer issue";
   TestLogFileName = _filename;
+
+  snapshot_config();
 }
 
 LogTestFixture::~LogTestFixture() {
-  restore_default_log_config();
+  restore_config();
   delete_file(TestLogFileName);
 }
 
@@ -61,7 +64,42 @@
   return success;
 }
 
-void LogTestFixture::restore_default_log_config() {
+void LogTestFixture::snapshot_config() {
+  _n_snapshots = LogConfiguration::_n_outputs;
+  _configuration_snapshot = NEW_C_HEAP_ARRAY(char*, _n_snapshots, mtLogging);
+  for (size_t i = 0; i < _n_snapshots; i++) {
+    ResourceMark rm;
+    stringStream ss;
+    LogConfiguration::_outputs[i]->describe(&ss);
+    _configuration_snapshot[i] = os::strdup_check_oom(ss.as_string(), mtLogging);
+  }
+}
+
+void LogTestFixture::restore_config() {
   LogConfiguration::disable_logging();
-  set_log_config("stdout", "all=warning");
+  for (size_t i = 0; i < _n_snapshots; i++) {
+    // Restore the config based on the saved output description string.
+    // The string has the following format: '<name> <selection> <decorators>[ <options>]'
+    // Extract the different parameters by replacing the spaces with NULLs.
+    char* str = _configuration_snapshot[i];
+
+    char* name = str;
+    str = strchr(str, ' ');
+    *str++ = '\0';
+
+    char* selection = str;
+    str = strchr(str, ' ');
+    *str++ = '\0';
+
+    char* decorators = str;
+
+    char* options = NULL;
+    str = strchr(str, ' ');
+    if (str != NULL) {
+      *str++ = '\0';
+      options = str;
+    }
+
+    set_log_config(name, selection, decorators, options != NULL ? options : "");
+  }
 }
--- a/test/hotspot/gtest/logging/logTestFixture.hpp	Mon Feb 26 18:04:43 2018 +0100
+++ b/test/hotspot/gtest/logging/logTestFixture.hpp	Mon Feb 26 16:33:48 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,8 @@
 class LogTestFixture : public testing::Test {
  private:
   char _filename[2 * K];
+  size_t _n_snapshots;
+  char** _configuration_snapshot;
 
  protected:
   const char* TestLogFileName;
@@ -45,6 +47,7 @@
                              const char* options = "",
                              bool allow_failure = false);
 
-  static void restore_default_log_config();
+  void snapshot_config();
+  void restore_config();
 };