diff --git a/src/sonic-eventd/tests/eventd_ut.cpp b/src/sonic-eventd/tests/eventd_ut.cpp index 13f8e9104696..db7543f552b5 100644 --- a/src/sonic-eventd/tests/eventd_ut.cpp +++ b/src/sonic-eventd/tests/eventd_ut.cpp @@ -703,24 +703,15 @@ TEST(eventd, service) printf("Service TEST completed\n"); } - -TEST(eventd, heartbeat) +void +wait_for_heartbeat(stats_collector &stats_instance, int cnt) { - printf("heartbeat TEST started\n"); - - int rc, cnt; - stats_collector stats_instance; - - debug_on(); - - rc = stats_instance.start(); - EXPECT_EQ(rc, 0); - auto st = duration_cast(system_clock::now().time_since_epoch()).count(); - while (stats_instance.heartbeats_cnt() != 0) { + while (stats_instance.heartbeats_cnt() != cnt) { auto en = duration_cast(system_clock::now().time_since_epoch()).count(); if ((en -st) > 3000) { EXPECT_LE((en -st), 3000); + EXPECT_EQ(cnt, stats_instance.heartbeats_cnt()); break; } else { @@ -729,10 +720,27 @@ TEST(eventd, heartbeat) printf("DROP:Waiting for heartbeat. duration:%s ms", ss.str().c_str()); } } +} +TEST(eventd, heartbeat) +{ + printf("heartbeat TEST started\n"); + + int rc, cnt; + stats_collector stats_instance; + + debug_on(); + + rc = stats_instance.start(); + EXPECT_EQ(rc, 0); + + /* Wait for any non-zero heartbeat */ + wait_for_heartbeat(stats_instance, 0); + + /* Pause heartbeat */ stats_instance.heartbeat_ctrl(true); - /* Sleep to ensure the other thread noticed the update. */ + /* Sleep to ensure the other thread noticed the pause request. */ this_thread::sleep_for(chrono::milliseconds(200)); /* Get current count */ @@ -746,20 +754,9 @@ TEST(eventd, heartbeat) /* Turn on heartbeat stats_instance.heartbeat_ctrl(); - st = duration_cast(system_clock::now().time_since_epoch()).count(); - /* Wait for a heartbeat */ - while (stats_instance.heartbeats_cnt() == cnt) { - auto en = duration_cast(system_clock::now().time_since_epoch()).count(); - if ((en -st) > 3000) { - EXPECT_LE((en -st), 3000); - break; - } - else { - stringstream ss; - ss << (en -st); - printf("DROP:Waiting for NO heartbeat for 3secs. duration:%s ms", ss.str().c_str()); - } - } + /* Wait for heartbeat count to change from last count */ + wait_for_heartbeat(stats_instance, cnt); + stats_instance.stop(); printf("heartbeat TEST completed\n"); } diff --git a/src/sonic-eventd/tests/main.cpp b/src/sonic-eventd/tests/main.cpp index 86a08c335b8f..0095f819d869 100644 --- a/src/sonic-eventd/tests/main.cpp +++ b/src/sonic-eventd/tests/main.cpp @@ -13,6 +13,8 @@ string global_existing_file = "./tests/redis_multi_db_ut_config/database_global. #define TEST_NAMESPACE "asic0" #define INVALID_NAMESPACE "invalid" +SwsscommonEnvironment* const env = NULL; + class SwsscommonEnvironment : public ::testing::Environment { public: // Override this to define how to set up the environment. @@ -86,7 +88,7 @@ int main(int argc, char* argv[]) testing::InitGoogleTest(&argc, argv); // Registers a global test environment, and verifies that the // registration function returns its argument. - SwsscommonEnvironment* const env = new SwsscommonEnvironment; + env = new SwsscommonEnvironment; testing::AddGlobalTestEnvironment(env); return RUN_ALL_TESTS(); }