Skip to content

Commit

Permalink
fix ParseReplicaDirNameTest and fix IWYU
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan authored and 王聃 committed Sep 23, 2024
1 parent 4ee3030 commit a6e5fa5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/replica/replica_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2094,15 +2094,15 @@ replica *replica_stub::new_replica(gpid gpid,
/* static */ bool
replica_stub::parse_replica_dir_name(const std::string &dir_name, gpid &pid, std::string &app_type)
{
std::vector<int32_t> ids(2, 0);
std::vector<uint32_t> ids(2, 0);
size_t begin = 0;
for (auto &id : ids) {
size_t end = dir_name.find('.', begin);
if (end == std::string::npos) {
return false;
}

if (!buf2int32(std::string_view(dir_name.data() + begin, end - begin), id)) {
if (!buf2uint32(std::string_view(dir_name.data() + begin, end - begin), id)) {
return false;
}

Expand All @@ -2113,8 +2113,8 @@ replica_stub::parse_replica_dir_name(const std::string &dir_name, gpid &pid, std
return false;
}

pid.set_app_id(ids[0]);
pid.set_partition_index(ids[1]);
pid.set_app_id(static_cast<int32_t>(ids[0]));
pid.set_partition_index(static_cast<int32_t>(ids[1]));

// TODO(wangdan): the 3rd parameter `count` does not support default argument for CentOS 7
// (gcc 7.3.1). After CentOS 7 is deprecated, consider dropping std::string::npos.
Expand Down
19 changes: 19 additions & 0 deletions src/replica/test/replica_dir_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>
#include <vector>

#include "common/gpid.h"
#include "gtest/gtest.h"
#include "replica/replica_stub.h"

Expand Down Expand Up @@ -97,8 +98,26 @@ class ParseReplicaDirNameTest : public testing::TestWithParam<parse_replica_dir_
TEST_P(ParseReplicaDirNameTest, ParseReplicaDirName) { test_parse_replica_dir_name(); }

const std::vector<parse_replica_dir_name_case> parse_replica_dir_name_tests{
// Empty dir name.
{"", false, {}, ""},
// Single-digit IDs.
{"1.2.pegasus", true, {1, 2}, "pegasus"},
// Multi-digit IDs.
{"1234.56789.pegasus", true, {1234, 56789}, "pegasus"},
// Custom app type other than "pegasus".
{"1.2.another", true, {1, 2}, "another"},
// Custom app type with dot.
{"1.2.another.pegasus", true, {1, 2}, "another.pegasus"},
// Custom app type with other specific symbol.
{"1.2.another_pegasus", true, {1, 2}, "another_pegasus"},
// Missing one ID.
{"1.pegasus", false, {}, ""},
// Missing both IDs.
{"pegasus", false, {}, ""},
// ID with letter.
{"1.2a.pegasus", false, {}, ""},
// ID with minus.
{"1.-2.pegasus", false, {}, ""},
};

INSTANTIATE_TEST_SUITE_P(ReplicaDirTest,
Expand Down

0 comments on commit a6e5fa5

Please sign in to comment.