< prev index next >

test/native/logging/test_logFileOutput.cpp

Print this page
rev 11942 : 8165600: Convert internal logging tests to GTest

@@ -20,10 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  *
  */
 #include "precompiled.hpp"
+#include "logTestUtils.inline.hpp"
 #include "logging/logFileOutput.hpp"
 #include "memory/resourceArea.hpp"
 #include "runtime/os.hpp"
 #include "unittest.hpp"
 #include "utilities/globalDefinitions.hpp"

@@ -99,5 +100,84 @@
   ResourceMark rm;
   stringStream ss;
   LogFileOutput fo(name);
   EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";
 }
+
+TEST(LogFileOutput, startup_rotation) {
+  const size_t rotations = 5;
+  const char* filename = "start-rotate-test";
+  char* rotated_file[rotations];
+
+  ResourceMark rm;
+  for (size_t i = 0; i < rotations; i++) {
+    size_t len = strlen(filename) + 3;
+    rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
+    int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
+    ASSERT_NE(-1, ret);
+    delete_file(rotated_file[i]);
+  }
+
+  delete_file(filename);
+  init_log_file(filename);
+  ASSERT_TRUE(file_exists(filename))
+    << "configured logging to file '" << filename << "' but file was not found";
+
+  // Initialize the same file a bunch more times to trigger rotations
+  for (size_t i = 0; i < rotations; i++) {
+    init_log_file(filename);
+    EXPECT_TRUE(file_exists(rotated_file[i]));
+  }
+
+  // Remove a file and expect its slot to be re-used
+  delete_file(rotated_file[1]);
+  init_log_file(filename);
+  EXPECT_TRUE(file_exists(rotated_file[1]));
+
+  // Clean up after test
+  delete_file(filename);
+  for (size_t i = 0; i < rotations; i++) {
+    delete_file(rotated_file[i]);
+  }
+}
+
+TEST(LogFileOutput, startup_truncation) {
+  const char* filename = "start-truncate-test";
+  const char* archived_filename = "start-truncate-test.0";
+
+  delete_file(filename);
+  delete_file(archived_filename);
+
+  // Use the same log file twice and expect it to be overwritten/truncated
+  init_log_file(filename, "filecount=0");
+  ASSERT_TRUE(file_exists(filename))
+    << "configured logging to file '" << filename << "' but file was not found";
+
+  init_log_file(filename, "filecount=0");
+  ASSERT_TRUE(file_exists(filename))
+    << "configured logging to file '" << filename << "' but file was not found";
+  EXPECT_FALSE(file_exists(archived_filename))
+    << "existing log file was not properly truncated when filecount was 0";
+
+  // Verify that the file was really truncated and not just appended
+  EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL));
+  const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL };
+  EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated))
+    << "log file " << filename << " appended rather than truncated";
+
+  delete_file(filename);
+  delete_file(archived_filename);
+}
+
+TEST(LogFileOutput, invalid_file) {
+  ResourceMark rm;
+  stringStream ss;
+
+  // Attempt to log to a directory (existing log not a regular file)
+  create_directory("tmplogdir");
+  LogFileOutput bad_file("file=tmplogdir");
+  EXPECT_FALSE(bad_file.initialize("", &ss))
+    << "file was initialized when there was an existing directory with the same name";
+  EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file"))
+    << "missing expected error message, received msg: %s" << ss.as_string();
+  remove("tmplogdir");
+}
< prev index next >