From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/log/test.cc | 381 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 381 insertions(+) create mode 100644 src/log/test.cc (limited to 'src/log/test.cc') diff --git a/src/log/test.cc b/src/log/test.cc new file mode 100644 index 00000000..52fbb284 --- /dev/null +++ b/src/log/test.cc @@ -0,0 +1,381 @@ +#include + +#include "log/Log.h" +#include "common/Clock.h" +#include "include/coredumpctl.h" +#include "SubsystemMap.h" + +#include "global/global_init.h" +#include "common/ceph_argparse.h" +#include "global/global_context.h" +#include "common/dout.h" + +using namespace ceph::logging; + +TEST(Log, Simple) +{ + SubsystemMap subs; + subs.set_log_level(0, 10); + subs.set_gather_level(0, 10); + + subs.set_log_level(1, 20); + subs.set_gather_level(1, 1); + + subs.set_log_level(2, 20); + subs.set_gather_level(2, 2); + + subs.set_log_level(3, 10); + subs.set_gather_level(3, 3); + + Log log(&subs); + log.start(); + + log.set_log_file("foo"); + log.reopen_log_file(); + + log.set_stderr_level(5, -1); + + + for (int i=0; i<100; i++) { + int sys = i % 4; + int l = 5 + (i%4); + if (subs.should_gather(sys, l)) { + MutableEntry e(l, sys); + log.submit_entry(std::move(e)); + } + } + + log.flush(); + + log.dump_recent(); + + log.stop(); +} + +TEST(Log, ReuseBad) +{ + SubsystemMap subs; + subs.set_log_level(1, 1); + subs.set_gather_level(1, 1); + Log log(&subs); + log.start(); + log.set_log_file("foo"); + log.reopen_log_file(); + + const int l = 0; + { + MutableEntry e(l, 1); + auto& out = e.get_ostream(); + out << (std::streambuf*)nullptr; + EXPECT_TRUE(out.bad()); // writing nullptr to a stream sets its badbit + log.submit_entry(std::move(e)); + } + { + MutableEntry e(l, 1); + auto& out = e.get_ostream(); + EXPECT_FALSE(out.bad()); // should not see failures from previous log entry + out << "hello world"; + log.submit_entry(std::move(e)); + } + + log.flush(); + log.stop(); +} + +int many = 10000; + +TEST(Log, ManyNoGather) +{ + SubsystemMap subs; + subs.set_log_level(1, 1); + subs.set_gather_level(1, 1); + Log log(&subs); + log.start(); + log.set_log_file("big"); + log.reopen_log_file(); + for (int i=0; i struct do_log +{ + void log(CephContext* cct); +}; + +template struct do_log<12, x> +{ + void log(CephContext* cct); +}; + +template void do_log::log(CephContext* cct) +{ + ldout(cct, 20) << "Log depth=" << depth << " x=" << x << dendl; + if (rand() % 2) { + do_log log; + log.log(cct); + } else { + do_log log; + log.log(cct); + } +} + +std::string recursion(CephContext* cct) +{ + ldout(cct, 20) << "Preparing recursion string" << dendl; + return "here-recursion"; +} + +template void do_log<12, x>::log(CephContext* cct) +{ + if ((rand() % 16) == 0) { + ldout(cct, 20) << "End " << recursion(cct) << "x=" << x << dendl; + } else { + ldout(cct, 20) << "End x=" << x << dendl; + } +} + +TEST(Log, Speed_gather) +{ + do_log<0,0> start; + g_ceph_context->_conf->subsys.set_gather_level(ceph_subsys_context, 30); + g_ceph_context->_conf->subsys.set_log_level(ceph_subsys_context, 0); + for (int i=0; i<100000;i++) { + ldout(g_ceph_context, 20) << "Iteration " << i << dendl; + start.log(g_ceph_context); + } +} + +TEST(Log, Speed_nogather) +{ + do_log<0,0> start; + g_ceph_context->_conf->subsys.set_gather_level(ceph_subsys_context, 0); + g_ceph_context->_conf->subsys.set_log_level(ceph_subsys_context, 0); + for (int i=0; i<100000;i++) { + ldout(g_ceph_context, 20) << "Iteration " << i << dendl; + start.log(g_ceph_context); + } +} + +TEST(Log, GarbleRecovery) +{ + static const char* test_file="log_for_moment"; + + Log* saved = g_ceph_context->_log; + Log log(&g_ceph_context->_conf->subsys); + log.start(); + unlink(test_file); + log.set_log_file(test_file); + log.reopen_log_file(); + g_ceph_context->_log = &log; + + std::string long_message(1000,'c'); + ldout(g_ceph_context, 0) << long_message << dendl; + ldout(g_ceph_context, 0) << "Prologue" << (std::streambuf*)nullptr << long_message << dendl; + ldout(g_ceph_context, 0) << "Epitaph" << long_message << dendl; + + g_ceph_context->_log = saved; + log.flush(); + log.stop(); + struct stat file_status; + ASSERT_EQ(lstat(test_file, &file_status), 0); + ASSERT_GT(file_status.st_size, 2000); +} + +int main(int argc, char **argv) +{ + vector args; + argv_to_vec(argc, (const char **)argv, args); + + auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, + CODE_ENVIRONMENT_UTILITY, + CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); + common_init_finish(g_ceph_context); + + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} -- cgit v1.2.3