Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: storage gtest #184

Draft
wants to merge 9 commits into
base: unstable
Choose a base branch
from
Draft

fix: storage gtest #184

wants to merge 9 commits into from

Conversation

lqxhub
Copy link
Collaborator

@lqxhub lqxhub commented Jan 22, 2025

修复 storage GTest fail

Summary by CodeRabbit

  • Chores

    • Cleaned up unnecessary include statements in multiple test files.
    • Updated CMakeLists.txt to improve test discovery for Google Test cases.
  • Tests

    • Streamlined test dependencies by removing unused header files.
    • Maintained existing test case functionality across the storage test suite.
    • Added new methods and destructors to enhance functionality and logging in specific test cases.
    • Adjusted formatting for consistency in function signatures and comments across various test files.

Copy link

coderabbitai bot commented Jan 22, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request focuses on cleaning up and streamlining the test files in the storage module. The changes primarily involve removing unnecessary include directives, such as <iostream>, from various test files. Additionally, the CMakeLists.txt for storage tests was updated to enable automatic Google Test case discovery. These modifications aim to reduce redundant dependencies and improve test management without altering the core functionality of the existing test cases.

Changes

File Change Summary
src/storage/tests/CMakeLists.txt Added GTEST_DISCOVER_TESTS(${TEST_NAME}) to enable automatic test case discovery
src/storage/tests/*_test.cc Removed unnecessary #include <iostream> from multiple test files
src/storage/tests/flush_oldest_cf_test.cc Removed redundant includes, added fmt/core.h
src/storage/tests/hashes_filter_test.cc Added RocksDB status header, removed iostream
src/storage/tests/log_index_test.cc Removed rocksdb/listener.h and commented storage/storage.h
src/storage/tests/kv_format_test.cc Removed src/debug.h include
src/storage/tests/options_test.cc Added default destructor and TearDownTestSuite method
src/storage/tests/sets_test.cc Added default destructor, updated method signatures for consistency
src/storage/tests/strings_test.cc Added default destructor, SetUpTestSuite, and TearDownTestSuite methods
src/storage/tests/zsets_test.cc Removed unnecessary includes; no other changes
src/storage/tests/hashes_test.cc Added default destructor, SetUpTestSuite, and TearDownTestSuite methods
src/storage/tests/keys_test.cc Added default destructor, SetUpTestSuite, and TearDownTestSuite methods
src/storage/tests/lists_test.cc Adjusted formatting of function signatures; no logic changes
src/storage/tests/log_index_collector_test.cc Added Next() method in NumberCreator, added destructor to TimerGuard
src/storage/tests/lru_cache_test.cc Adjusted formatting of main function signature

Possibly related PRs

Suggested reviewers

  • AlexStocks
  • marsevilspirit

Poem

🐰 Hop, hop, through the code we go,
Cleaning includes with a swift blow,
Tests shine bright, dependencies light,
No more clutter, everything's just right!
A rabbit's cleanup, neat and clean! 🧹


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ddf8de6 and 9d3ce13.

📒 Files selected for processing (12)
  • src/storage/tests/CMakeLists.txt (1 hunks)
  • src/storage/tests/custom_comparator_test.cc (0 hunks)
  • src/storage/tests/flush_oldest_cf_test.cc (0 hunks)
  • src/storage/tests/hashes_filter_test.cc (1 hunks)
  • src/storage/tests/kv_format_test.cc (0 hunks)
  • src/storage/tests/lists_filter_test.cc (0 hunks)
  • src/storage/tests/log_index_test.cc (0 hunks)
  • src/storage/tests/options_test.cc (0 hunks)
  • src/storage/tests/sets_test.cc (0 hunks)
  • src/storage/tests/strings_filter_test.cc (0 hunks)
  • src/storage/tests/strings_test.cc (0 hunks)
  • src/storage/tests/zsets_test.cc (0 hunks)
💤 Files with no reviewable changes (10)
  • src/storage/tests/kv_format_test.cc
  • src/storage/tests/strings_test.cc
  • src/storage/tests/log_index_test.cc
  • src/storage/tests/sets_test.cc
  • src/storage/tests/custom_comparator_test.cc
  • src/storage/tests/lists_filter_test.cc
  • src/storage/tests/options_test.cc
  • src/storage/tests/strings_filter_test.cc
  • src/storage/tests/flush_oldest_cf_test.cc
  • src/storage/tests/zsets_test.cc
✅ Files skipped from review due to trivial changes (1)
  • src/storage/tests/hashes_filter_test.cc
🔇 Additional comments (1)
src/storage/tests/CMakeLists.txt (1)

39-39: LGTM! Good improvement to test discovery.

Adding GTEST_DISCOVER_TESTS is a valuable enhancement that enables automatic discovery of individual test cases at configure time. This improves test organization and allows for more granular test execution and reporting.

@@ -36,4 +36,5 @@ FOREACH (TEST_SOURCE ${TEST_SOURCES})
PRIVATE ${zstd_LIBRARIES}
PRIVATE binlog_pb
)
GTEST_DISCOVER_TESTS(${TEST_NAME})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Add missing CMake minimum version requirement

The file uses GTEST_DISCOVER_TESTS which requires CMake 3.10 or newer, but no minimum CMake version is specified. For consistency with other test directories (which use 3.18 and 3.25), add cmake_minimum_required(VERSION 3.18) at the beginning of the file.

🔗 Analysis chain

Verify CMake version compatibility.

The GTEST_DISCOVER_TESTS command requires CMake 3.10 or newer. Let's verify the minimum CMake version requirement is properly set.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if minimum CMake version is specified and compatible
rg -A 1 "cmake_minimum_required" 

Length of output: 229


Script:

#!/bin/bash
# Check root and parent CMakeLists.txt files
echo "=== Root CMakeLists.txt ==="
cat CMakeLists.txt 2>/dev/null | grep -A 1 "cmake_minimum" || echo "No root CMakeLists.txt found"

echo -e "\n=== src/storage/CMakeLists.txt ==="
cat src/storage/CMakeLists.txt 2>/dev/null | grep -A 1 "cmake_minimum" || echo "No src/storage/CMakeLists.txt found"

echo -e "\n=== src/storage/tests/CMakeLists.txt ==="
cat src/storage/tests/CMakeLists.txt 2>/dev/null | grep -A 1 "cmake_minimum" || echo "No src/storage/tests/CMakeLists.txt found"

Length of output: 658


Script:

#!/bin/bash
echo "=== Checking file existence ==="
ls -l src/storage/tests/CMakeLists.txt 2>/dev/null || echo "File does not exist"

echo -e "\n=== File content if exists ==="
cat src/storage/tests/CMakeLists.txt 2>/dev/null || echo "Cannot read file content"

Length of output: 1746

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
src/storage/tests/keys_test.cc (1)

Line range hint 5204-5214: Cleanup commented-out logging configuration.

The main function contains commented-out logging configuration code that should be either removed or enabled.

Either remove or enable the logging configuration:

- //   FLAGS_log_dir = "./log";
- //   FLAGS_minloglevel = 0;
- //   FLAGS_max_log_size = 1800;
- //   FLAGS_logbufsecs = 0;
- //   ::google::InitGoogleLogging("keys_test");
🧹 Nitpick comments (8)
src/storage/tests/log_index_collector_test.cc (1)

Line range hint 137-143: Consider making member variables const and addressing thread safety.

Two suggestions for improvement:

  1. Member variables name_ and start_ could be marked as const since they're only set in the constructor.
  2. Be aware that fmt::println is not guaranteed to be thread-safe. Consider using a thread-safe logging mechanism for production code.
struct TimerGuard {
  TimerGuard(std::string_view name = "Test") : name_(name), start_(std::chrono::system_clock::now()) {}

  ~TimerGuard() {
    auto end = std::chrono::system_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_);
    fmt::println("{} cost {}ms", name_, duration.count());
  }

-  std::string_view name_;
-  std::chrono::time_point<std::chrono::system_clock> start_;
+  const std::string_view name_;
+  const std::chrono::time_point<std::chrono::system_clock> start_;
};
src/storage/tests/hashes_test.cc (3)

58-59: Consider consolidating duplicate field_value_match functions.

The two field_value_match functions share similar logic. Consider consolidating them into a single function with an optional database parameter to reduce code duplication.

-static bool field_value_match(storage::Storage *const db, const Slice &key,
-                              const std::vector<FieldValue> &expect_field_value) {
-  std::vector<FieldValue> field_value_out;
-  Status s = db->HGetall(key, &field_value_out);
-  if (!s.ok() && !s.IsNotFound()) {
-    return false;
-  }
-  if (field_value_out.size() != expect_field_value.size()) {
-    return false;
-  }
-  if (s.IsNotFound() && expect_field_value.empty()) {
-    return true;
-  }
-  for (const auto &field_value : expect_field_value) {
-    if (find(field_value_out.begin(), field_value_out.end(), field_value) == field_value_out.end()) {
-      return false;
-    }
-  }
-  return true;
-}
-
-static bool field_value_match(const std::vector<FieldValue> &field_value_out,
-                              const std::vector<FieldValue> &expect_field_value) {
+static bool field_value_match(const std::vector<FieldValue> &expect_field_value,
+                            const std::vector<FieldValue> &field_value_out,
+                            storage::Storage *const db = nullptr,
+                            const Slice &key = Slice()) {
+  std::vector<FieldValue> actual_values;
+  if (db != nullptr) {
+    Status s = db->HGetall(key, &actual_values);
+    if (!s.ok() && !s.IsNotFound()) {
+      return false;
+    }
+    if (s.IsNotFound() && expect_field_value.empty()) {
+      return true;
+    }
+    field_value_out = actual_values;
+  }
   if (field_value_out.size() != expect_field_value.size()) {
     return false;
   }
   for (const auto &field_value : expect_field_value) {
     if (find(field_value_out.begin(), field_value_out.end(), field_value) == field_value_out.end()) {
       return false;
     }
   }
   return true;
 }

Also applies to: 79-80


1004-1005: Document the reason for NOLINT.

The NOLINT comment should include a reason why the lint check is being suppressed. This helps future maintainers understand the rationale.

-TEST_F(HashesTest, HScanTest) {
-  // NOLINT
+TEST_F(HashesTest, HScanTest) {  // NOLINT(readability/function_size) - Complex test requiring multiple scenarios

Line range hint 2445-2454: Remove or document commented logging setup code.

The commented logging setup code should either be removed if it's no longer needed or documented explaining why it's kept but commented out.

-  //   FLAGS_log_dir = "./log";
-  //   FLAGS_minloglevel = 0;
-  //   FLAGS_max_log_size = 1800;
-  //   FLAGS_logbufsecs = 0;
-  //   ::google::InitGoogleLogging("hashes_test");
+  // TODO: Logging setup temporarily disabled until we migrate to a new logging system
src/storage/tests/keys_test.cc (4)

33-33: Add documentation for the destructor.

The destructor is marked as default but lacks documentation explaining why it's needed and what cleanup it performs.

Add a comment explaining the purpose of the destructor:

+ // Virtual destructor to allow proper cleanup of derived test classes
~KeysTest() override = default;

49-50: Document empty test suite lifecycle methods.

The empty SetUpTestSuite and TearDownTestSuite methods lack documentation explaining their purpose.

Add comments explaining why these methods are empty:

+ // No global test suite setup needed
static void SetUpTestSuite() {}

+ // No global test suite cleanup needed 
static void TearDownTestSuite() {}

99-100: Improve test execution order comment.

The comment about test execution order is important but could be more descriptive.

Enhance the comment to better explain the test dependencies:

- // Note: This test needs to execute at first because all of the data is
- // predetermined.
+ // IMPORTANT: This test must execute first as it relies on a specific
+ // predetermined dataset. Later tests may modify this data.

115-115: Consider using const references in range-based for loops.

The range-based for loops could be optimized by using const references to avoid unnecessary copying.

Update the loops to use const references:

- for (const auto &kv : kvs) {
+ for (const auto& kv : kvs) {

This change should be applied to all similar loops in the file.

Also applies to: 295-295, 490-490, 682-682, 875-875, 1088-1088, 1267-1267, 1478-1478, 1689-1689, 1900-1900

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9d3ce13 and 7c2687d.

📒 Files selected for processing (17)
  • src/storage/tests/custom_comparator_test.cc (1 hunks)
  • src/storage/tests/flush_oldest_cf_test.cc (8 hunks)
  • src/storage/tests/hashes_filter_test.cc (9 hunks)
  • src/storage/tests/hashes_test.cc (7 hunks)
  • src/storage/tests/keys_test.cc (21 hunks)
  • src/storage/tests/kv_format_test.cc (2 hunks)
  • src/storage/tests/lists_filter_test.cc (4 hunks)
  • src/storage/tests/lists_test.cc (21 hunks)
  • src/storage/tests/lock_mgr_test.cc (1 hunks)
  • src/storage/tests/log_index_collector_test.cc (3 hunks)
  • src/storage/tests/log_index_test.cc (8 hunks)
  • src/storage/tests/lru_cache_test.cc (1 hunks)
  • src/storage/tests/options_test.cc (2 hunks)
  • src/storage/tests/sets_test.cc (21 hunks)
  • src/storage/tests/strings_filter_test.cc (1 hunks)
  • src/storage/tests/strings_test.cc (5 hunks)
  • src/storage/tests/zsets_test.cc (27 hunks)
✅ Files skipped from review due to trivial changes (3)
  • src/storage/tests/lock_mgr_test.cc
  • src/storage/tests/lru_cache_test.cc
  • src/storage/tests/lists_test.cc
🚧 Files skipped from review as they are similar to previous changes (11)
  • src/storage/tests/hashes_filter_test.cc
  • src/storage/tests/kv_format_test.cc
  • src/storage/tests/log_index_test.cc
  • src/storage/tests/flush_oldest_cf_test.cc
  • src/storage/tests/strings_filter_test.cc
  • src/storage/tests/options_test.cc
  • src/storage/tests/strings_test.cc
  • src/storage/tests/custom_comparator_test.cc
  • src/storage/tests/zsets_test.cc
  • src/storage/tests/lists_filter_test.cc
  • src/storage/tests/sets_test.cc
🔇 Additional comments (4)
src/storage/tests/log_index_collector_test.cc (2)

25-27: Well-implemented thread-safe sequence generator!

The Next() method is correctly implemented using atomic fetch-and-add operation, ensuring thread safety while maintaining efficiency.


35-36: Test case formatting improves readability.

Moving the NOLINT comments to separate lines enhances code readability while maintaining the same functionality.

Also applies to: 80-81

src/storage/tests/hashes_test.cc (2)

32-32: LGTM! Proper destructor declaration.

The destructor is correctly marked as override and defaulted, following best practices for virtual destructors.


49-49: Consider implementing test suite setup/teardown methods.

The static SetUpTestSuite and TearDownTestSuite methods are empty. Consider adding suite-wide setup/teardown logic if needed, or document why they are intentionally empty.

Let's check if other test files have implemented these methods:

Comment on lines +5073 to 5075
for (const auto &ts : type_status) {
WARN("type: {} status: {}", storage::DataTypeStrings[static_cast<int>(ts.first)], ts.second.ToString());
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove debug logging statements.

The warning log statements appear to be debug code that was accidentally committed.

Remove the debug logging:

- WARN("ret: {}", ret);
- for (const auto &ts : type_status) {
-   WARN("type: {} status: {}", storage::DataTypeStrings[static_cast<int>(ts.first)], ts.second.ToString());
- }

Committable suggestion skipped: line range outside the PR's diff.

@lqxhub lqxhub marked this pull request as draft January 23, 2025 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant