Skip to content

Commit b09bdce

Browse files
committed
tests: resolve raft simulation and networking race conditions
1 parent 5f5eb18 commit b09bdce

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

tests/multi_raft_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <gtest/gtest.h>
77

88
#include <chrono>
9+
#include <csignal>
910
#include <thread>
1011
#include <vector>
1112

@@ -92,6 +93,7 @@ TEST(MultiRaftTests, StateMachineIntegration) {
9293
* This ensures high availability by validating consensus emergence.
9394
*/
9495
TEST(MultiRaftTests, LeaderElectionAndFailover) {
96+
signal(SIGPIPE, SIG_IGN);
9597
const int num_nodes = 3;
9698
const int base_port = 9200;
9799

tests/raft_simulation_tests.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <gtest/gtest.h>
77

88
#include <chrono>
9+
#include <cstdio>
910
#include <thread>
1011

1112
#include "common/cluster_manager.hpp"
@@ -18,6 +19,7 @@ using namespace cloudsql::raft;
1819
namespace {
1920

2021
TEST(RaftSimulationTests, FollowerToCandidate) {
22+
static_cast<void>(std::remove("raft_group_1.state"));
2123
config::Config config;
2224
config.mode = config::RunMode::Coordinator;
2325

@@ -34,24 +36,31 @@ TEST(RaftSimulationTests, FollowerToCandidate) {
3436
std::this_thread::sleep_for(std::chrono::milliseconds(500));
3537

3638
// Should have attempted to become candidate/leader
39+
group.stop();
40+
static_cast<void>(std::remove("raft_group_1.state"));
3741
}
3842

3943
TEST(RaftSimulationTests, HeartbeatReset) {
44+
static_cast<void>(std::remove("raft_group_2.state"));
4045
config::Config config;
4146
config.mode = config::RunMode::Coordinator;
4247

4348
cluster::ClusterManager cm(&config);
4449
network::RpcServer rpc(7001);
4550

46-
RaftGroup group(1, "node1", cm, rpc);
51+
RaftGroup group(2, "node2", cm, rpc);
4752
group.start();
4853

4954
// Send periodic heartbeats to prevent election
5055
for (int i = 0; i < 5; ++i) {
51-
std::vector<uint8_t> payload(8, 0); // Term 0
56+
std::vector<uint8_t> payload(8, 0);
57+
// Use a high term to ensure it's accepted
58+
term_t term = 100;
59+
std::memcpy(payload.data(), &term, 8);
60+
5261
network::RpcHeader header;
5362
header.type = network::RpcType::AppendEntries;
54-
header.group_id = 1;
63+
header.group_id = 2;
5564
header.payload_len = 8;
5665

5766
group.handle_append_entries(header, payload, -1);
@@ -60,6 +69,8 @@ TEST(RaftSimulationTests, HeartbeatReset) {
6069
// Should NOT be leader yet because heartbeats reset the timer
6170
EXPECT_FALSE(group.is_leader());
6271
}
72+
group.stop();
73+
static_cast<void>(std::remove("raft_group_2.state"));
6374
}
6475

6576
} // namespace

0 commit comments

Comments
 (0)