/Users/deen/code/yugabyte-db/src/yb/rocksutil/yb_rocksdb_logger-test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <string> |
15 | | |
16 | | #include <glog/logging.h> |
17 | | |
18 | | #include "yb/rocksutil/yb_rocksdb_logger.h" |
19 | | |
20 | | #include "yb/util/random.h" |
21 | | #include "yb/util/random_util.h" |
22 | | #include "yb/util/string_trim.h" |
23 | | #include "yb/util/test_util.h" |
24 | | #include "yb/util/tostring.h" |
25 | | |
26 | | using yb::YBRocksDBLogger; |
27 | | using rocksdb::InfoLogLevel; |
28 | | |
29 | | namespace yb { |
30 | | |
31 | | namespace { |
32 | | static constexpr char kPrefix[] = "prefix: "; |
33 | | } // namespace |
34 | | |
35 | | class YBRocksDBLoggerTest: public ::testing::Test, public google::base::Logger { |
36 | | protected: |
37 | 0 | YBRocksDBLoggerTest() : rocksdb_logger_(kPrefix) { |
38 | 0 | } |
39 | | |
40 | 0 | void SetUp() override { |
41 | 0 | google::InitGoogleLogging(""); |
42 | 0 | old_logger_ = google::base::GetLogger(google::GLOG_WARNING); |
43 | 0 | google::base::SetLogger(google::GLOG_WARNING, this); |
44 | 0 | } |
45 | | |
46 | 0 | void TearDown() override { |
47 | 0 | google::base::SetLogger(google::GLOG_WARNING, old_logger_); |
48 | 0 | } |
49 | | |
50 | | public: |
51 | 0 | void Write(bool force_flush, time_t timestamp, const char *message, int message_len) override { |
52 | 0 | log_.append(message, message_len); |
53 | 0 | } |
54 | | |
55 | 0 | void Flush() override { |
56 | 0 | } |
57 | | |
58 | 0 | google::uint32 LogSize() override { |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | | YBRocksDBLogger rocksdb_logger_; |
63 | | std::string log_; |
64 | | |
65 | | private: |
66 | | google::base::Logger *old_logger_; |
67 | | }; |
68 | | |
69 | 0 | TEST_F(YBRocksDBLoggerTest, LogvWithContextSmall) { |
70 | 0 | Random r(SeedRandom()); |
71 | |
|
72 | 0 | static constexpr size_t reserve_for_prefix = 100; |
73 | 0 | static constexpr size_t lengths_to_test[] = {512, 1024, 32768, 65536, 131072}; |
74 | |
|
75 | 0 | for (const size_t length : lengths_to_test) { |
76 | 0 | const std::string text = RandomHumanReadableString(length, &r); |
77 | 0 | RWARN(&rocksdb_logger_, "%s %d", text.c_str(), length); |
78 | |
|
79 | 0 | const std::string trimmed_log = util::RightTrimStr(log_); |
80 | 0 | const size_t prefix_pos = trimmed_log.find(kPrefix); |
81 | 0 | ASSERT_NE(prefix_pos, std::string::npos); |
82 | 0 | const std::string log_after_prefix = trimmed_log.substr(prefix_pos + sizeof(kPrefix) - 1); |
83 | 0 | const std::string formatted_text = text + " " + ToString(length); |
84 | 0 | if (length > google::LogMessage::kMaxLogMessageLen - reserve_for_prefix) { |
85 | | // Log part after prefix should be a substring of what we've logged. |
86 | 0 | ASSERT_STR_CONTAINS(formatted_text, log_after_prefix); |
87 | 0 | } else { |
88 | 0 | ASSERT_EQ(formatted_text, log_after_prefix); |
89 | 0 | } |
90 | |
|
91 | 0 | log_.clear(); |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | } // namespace yb |