Skip to content

Commit 7456de8

Browse files
authored
fix: fix cpp ut (#4035)
1 parent fb9ec49 commit 7456de8

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

codecov.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ codecov:
22
notify:
33
wait_for_ci: yes
44
require_ci_to_pass: no
5+
6+
coverage:
7+
status:
8+
project:
9+
default:
10+
informational: true
11+
patch:
12+
default:
13+
informational: true

src/replica/snapshot_replica_test.cc

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ TEST_P(SnapshotReplicaTest, AddReplicate) {
102102
}
103103

104104
TEST_P(SnapshotReplicaTest, LeaderAndFollower) {
105+
FLAGS_endpoint = "127.0.0.1:18529";
106+
std::string leader_point = "127.0.0.1:18529";
105107
::openmldb::tablet::TabletImpl* tablet = new ::openmldb::tablet::TabletImpl();
106108
tablet->Init("");
107109
brpc::Server server;
@@ -110,7 +112,6 @@ TEST_P(SnapshotReplicaTest, LeaderAndFollower) {
110112
exit(1);
111113
}
112114
brpc::ServerOptions options;
113-
std::string leader_point = "127.0.0.1:18529";
114115
if (server.Start(leader_point.c_str(), &options) != 0) {
115116
PDLOG(WARNING, "fail to start server %s", leader_point.c_str());
116117
exit(1);
@@ -140,10 +141,7 @@ TEST_P(SnapshotReplicaTest, LeaderAndFollower) {
140141
}
141142

142143
sleep(3);
143-
144-
::openmldb::test::TempPath temp_path;
145-
FLAGS_db_root_path = temp_path.GetTempPath();
146-
FLAGS_hdd_root_path = temp_path.GetTempPath();
144+
::openmldb::test::ResetRandomDiskFlags();
147145
FLAGS_endpoint = "127.0.0.1:18530";
148146
::openmldb::tablet::TabletImpl* tablet1 = new ::openmldb::tablet::TabletImpl();
149147
tablet1->Init("");
@@ -213,6 +211,8 @@ TEST_P(SnapshotReplicaTest, LeaderAndFollower) {
213211

214212
TEST_P(SnapshotReplicaTest, SendSnapshot) {
215213
FLAGS_make_snapshot_threshold_offset = 0;
214+
FLAGS_endpoint = "127.0.0.1:18529";
215+
std::string leader_point = "127.0.0.1:18529";
216216
::openmldb::tablet::TabletImpl* tablet = new ::openmldb::tablet::TabletImpl();
217217
MockClosure closure;
218218
tablet->Init("");
@@ -222,7 +222,6 @@ TEST_P(SnapshotReplicaTest, SendSnapshot) {
222222
exit(1);
223223
}
224224
brpc::ServerOptions options;
225-
std::string leader_point = "127.0.0.1:18529";
226225
if (server.Start(leader_point.c_str(), &options) != 0) {
227226
PDLOG(WARNING, "fail to start server %s", leader_point.c_str());
228227
exit(1);
@@ -257,9 +256,7 @@ TEST_P(SnapshotReplicaTest, SendSnapshot) {
257256
tablet->MakeSnapshot(NULL, &grq, &grp, &closure);
258257

259258
sleep(3);
260-
::openmldb::test::TempPath temp_path;
261-
FLAGS_db_root_path = temp_path.GetTempPath();
262-
FLAGS_hdd_root_path = temp_path.GetTempPath();
259+
::openmldb::test::ResetRandomDiskFlags();
263260
FLAGS_endpoint = "127.0.0.1:18530";
264261
::openmldb::tablet::TabletImpl* tablet1 = new ::openmldb::tablet::TabletImpl();
265262
tablet1->Init("");
@@ -319,6 +316,8 @@ TEST_P(SnapshotReplicaTest, SendSnapshot) {
319316
tablet->DropTable(NULL, &dr, &drs, &closure);
320317
tablet1->DropTable(NULL, &dr, &drs, &closure);
321318
sleep(2);
319+
server.Stop(10);
320+
server1.Stop(10);
322321
}
323322

324323
TEST_P(SnapshotReplicaTest, IncompleteSnapshot) {
@@ -540,7 +539,10 @@ TEST_P(SnapshotReplicaTest, IncompleteSnapshot) {
540539
}
541540

542541
TEST_P(SnapshotReplicaTest, LeaderAndFollowerTS) {
542+
::openmldb::test::ResetRandomDiskFlags();
543543
auto storage_mode = GetParam();
544+
FLAGS_endpoint = "127.0.0.1:18529";
545+
std::string leader_point = "127.0.0.1:18529";
544546
::openmldb::tablet::TabletImpl* tablet = new ::openmldb::tablet::TabletImpl();
545547
tablet->Init("");
546548
brpc::Server server;
@@ -549,7 +551,6 @@ TEST_P(SnapshotReplicaTest, LeaderAndFollowerTS) {
549551
exit(1);
550552
}
551553
brpc::ServerOptions options;
552-
std::string leader_point = "127.0.0.1:18529";
553554
if (server.Start(leader_point.c_str(), &options) != 0) {
554555
PDLOG(WARNING, "fail to start server %s", leader_point.c_str());
555556
exit(1);
@@ -586,9 +587,8 @@ TEST_P(SnapshotReplicaTest, LeaderAndFollowerTS) {
586587
ASSERT_TRUE(client.Put(tid, pid, cur_time, value, dimensions).OK());
587588

588589
sleep(3);
589-
::openmldb::test::TempPath temp_path;
590-
FLAGS_db_root_path = temp_path.GetTempPath();
591-
FLAGS_hdd_root_path = temp_path.GetTempPath();
590+
591+
::openmldb::test::ResetRandomDiskFlags();
592592
FLAGS_endpoint = "127.0.0.1:18530";
593593
::openmldb::tablet::TabletImpl* tablet1 = new ::openmldb::tablet::TabletImpl();
594594
tablet1->Init("");
@@ -628,7 +628,10 @@ TEST_P(SnapshotReplicaTest, LeaderAndFollowerTS) {
628628
::openmldb::api::DropTableResponse drs;
629629
tablet->DropTable(NULL, &dr, &drs, &closure);
630630
tablet1->DropTable(NULL, &dr, &drs, &closure);
631+
631632
sleep(1);
633+
server.Stop(10);
634+
server1.Stop(10);
632635
}
633636

634637
INSTANTIATE_TEST_SUITE_P(DBSDK, SnapshotReplicaTest,

src/sdk/mini_cluster.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class MiniCluster {
208208
private:
209209
bool StartTablet(brpc::Server* tb_server) {
210210
std::string tb_endpoint = "127.0.0.1:" + GenRand();
211+
FLAGS_endpoint = tb_endpoint;
211212
tb_endpoints_.push_back(tb_endpoint);
212213
::openmldb::tablet::TabletImpl* tablet = new ::openmldb::tablet::TabletImpl();
213214
bool ok = tablet->Init(zk_cluster_, zk_path_, tb_endpoint, "");
@@ -368,12 +369,13 @@ class StandaloneEnv {
368369

369370
bool StartTablet(brpc::Server* tb_server) {
370371
std::string tb_endpoint = "127.0.0.1:" + std::to_string(GenRand());
372+
FLAGS_endpoint = tb_endpoint;
371373
tb_endpoint_ = tb_endpoint;
372374
::openmldb::tablet::TabletImpl* tablet = new ::openmldb::tablet::TabletImpl();
373375
bool ok = tablet->Init("", "", tb_endpoint, "");
374376
if (!ok) {
375377
return false;
376-
}
378+
}
377379

378380
auto ts_authenticator = new openmldb::authn::BRPCAuthenticator(
379381
[tablet](const std::string& host, const std::string& username, const std::string& password) {

src/statistics/query_response_time/deployment_metric_collector.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ absl::Status DeploymentMetricCollector::DeleteDeploy(const std::string& db, cons
3838

3939
void DeploymentMetricCollector::Reset() {
4040
absl::WriterMutexLock lock(&mutex_);
41+
md_recorder_.reset();
4142
md_recorder_ = make_shared(prefix_);
4243
}
4344
} // namespace openmldb::statistics

src/test/util.cc

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,30 @@ namespace test {
4747

4848
using ::openmldb::codec::SchemaCodec;
4949

50+
std::string GenRand() {
51+
return std::to_string(rand() % 10000000 + 1); // NOLINT
52+
}
53+
54+
void SetRootPath(const std::filesystem::path& prefix) {
55+
std::filesystem::path tmp_path = prefix / GenRand();
56+
LOG(INFO) << "setting root path for test in " << tmp_path;
57+
FLAGS_db_root_path = tmp_path / "db_root";
58+
FLAGS_ssd_root_path = tmp_path / "ssd_root";
59+
FLAGS_hdd_root_path = tmp_path / "hdd_root";
60+
FLAGS_recycle_bin_root_path = tmp_path / "recycle_root";
61+
FLAGS_recycle_bin_hdd_root_path = tmp_path / "recycle_hdd_root";
62+
FLAGS_recycle_bin_ssd_root_path = tmp_path / "recycle_ssd_root";
63+
}
64+
5065
struct DiskFlagsRAII {
5166
// no copy
5267
DiskFlagsRAII(const DiskFlagsRAII&) = delete;
5368
DiskFlagsRAII(DiskFlagsRAII&&) = default;
5469
DiskFlagsRAII& operator=(const DiskFlagsRAII&) = delete;
5570
DiskFlagsRAII& operator=(DiskFlagsRAII&&) = default;
5671

57-
explicit DiskFlagsRAII(const std::filesystem::path& p) : prefix(p) {
58-
LOG(INFO) << "setting temp path for test in " << prefix;
59-
FLAGS_db_root_path = prefix / "db_root";
60-
FLAGS_ssd_root_path = prefix / "ssd_root";
61-
FLAGS_hdd_root_path = prefix / "hdd_root";
62-
FLAGS_recycle_bin_root_path = prefix / "recycle_root";
63-
FLAGS_recycle_bin_hdd_root_path = prefix / "recycle_hdd_root";
64-
FLAGS_recycle_bin_ssd_root_path = prefix / "recycle_ssd_root";
72+
explicit DiskFlagsRAII(const std::filesystem::path& p) : prefix(p){
73+
SetRootPath(prefix);
6574
}
6675

6776
~DiskFlagsRAII() {
@@ -73,10 +82,6 @@ struct DiskFlagsRAII {
7382
};
7483
static const DiskFlagsRAII* flags_raii_ptr = nullptr;
7584

76-
std::string GenRand() {
77-
return std::to_string(rand() % 10000000 + 1); // NOLINT
78-
}
79-
8085
void AddDefaultSchema(uint64_t abs_ttl, uint64_t lat_ttl, ::openmldb::type::TTLType ttl_type,
8186
::openmldb::nameserver::TableInfo* table_meta) {
8287
auto column_desc = table_meta->add_column_desc();
@@ -367,13 +372,14 @@ static DiskFlagsRAII GenerateRandomDiskFlags(absl::string_view tag) {
367372
static bool InitRandomDiskFlagsImpl(absl::string_view tag) {
368373
static DiskFlagsRAII flags_raii = GenerateRandomDiskFlags(tag);
369374
auto sig_handler = [](int sig) {
375+
LOG(INFO) << "Quit " << sig;
370376
flags_raii.~DiskFlagsRAII();
371377
_exit(1);
372378
};
373379
signal(SIGINT, sig_handler);
374380
signal(SIGKILL, sig_handler);
375381
signal(SIGTERM, sig_handler);
376-
signal(SIGABRT, sig_handler);
382+
// signal(SIGABRT, sig_handler);
377383

378384
flags_raii_ptr = &flags_raii;
379385
return true;
@@ -386,5 +392,9 @@ bool InitRandomDiskFlags(std::string_view tag) {
386392
static bool flags_raii_flag = InitRandomDiskFlagsImpl(tag);
387393
return flags_raii_flag;
388394
}
395+
396+
void ResetRandomDiskFlags() {
397+
SetRootPath(flags_raii_ptr->prefix);
398+
}
389399
} // namespace test
390400
} // namespace openmldb

src/test/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ api::TableMeta CreateTableMeta(const std::string& name, uint32_t tid, uint32_t p
113113
// Since glfags are global, the function does take effect only once
114114
[[maybe_unused]]
115115
bool InitRandomDiskFlags(std::string_view tag = "test");
116+
[[maybe_unused]]
117+
void ResetRandomDiskFlags();
116118
} // namespace test
117119
} // namespace openmldb
118120
#endif // SRC_TEST_UTIL_H_

0 commit comments

Comments
 (0)