Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add delay to gtest Test Case

I'm using google test framework for testing hardware Ethernet switch. Some operations (e.x. enabling RSTP) take time to proceed. So I need to implement some sort of a Sleep() function inside the test case:

TEST_F(RSTP, enableRSTP) {
    snmp.set(OID, Integer32(3));
    // after changing value switch is unavailable
    // so I need to wait before request
    auto result = snmp.get(OID);
    auto res = std::get<Integer32>(result);
    ASSERT_EQ(res, Integer32(3));
}

How do I accomplish this?

like image 798
Alexandr Avatar asked Mar 07 '23 22:03

Alexandr


1 Answers

As mentioned in one of the comments, you could just use (c++14):

#include <chrono>
#include <thread>
TEST_F(RSTP, enableRSTP) {
  ...
  using namespace std::chrono_literals;
  std::this_thread::sleep_for(2s);
  ...
}

... or for c++11, replace 2s with:

std::chrono::seconds(2)

If you don't use >= c++11, then this becomes an OS specific question (not standard c++)

like image 96
Werner Erasmus Avatar answered Mar 19 '23 07:03

Werner Erasmus