Skip to content

Commit

Permalink
ut: support wait leader in timeout (#10)
Browse files Browse the repository at this point in the history
support `wait_leader` in `timeout`(default 100 seconds) to avoid some
failed unit test waiting for leader to be elected forever.

details: #10
  • Loading branch information
ehds authored Apr 28, 2024
1 parent 9a20605 commit f2cda9e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <gflags/gflags.h>

#include "butil/time.h"
#include "braft/enum.pb.h"
#include "braft/errno.pb.h"
#include "braft/node.h"
Expand Down Expand Up @@ -384,16 +385,21 @@ class Cluster {
}
return NULL;
}

void wait_leader() {
while (true) {

// return true if there is a leader, false when reach timeout.
void wait_leader(int64_t timeout_ms = 100 * 1000 /*100 seconds*/) {
int64_t deadline = butil::timespec_to_microseconds(
butil::milliseconds_from_now(timeout_ms));

while (butil::gettimeofday_ms() < deadline) {
braft::Node* node = leader();
if (node) {
return;
} else {
usleep(100 * 1000);
}
}
ASSERT_TRUE(false); // wait time out.
}

void check_node_status() {
Expand Down

0 comments on commit f2cda9e

Please sign in to comment.