#include #include #include #include #include #include #include using namespace std; using namespace Di; using std::chrono::steady_clock; TEST(Timer, isElapsed) { Timer t; t.set(chrono::milliseconds(250)); t.start(); // Timer has not expired, so result will be FALSE EXPECT_FALSE(t.isElapsed()); usleep(250000); // Timer has expired, so result will be TRUE EXPECT_TRUE(t.isElapsed()); t.reset(); t.start(); t.stop(); // Timer has been stopped but not expired, so result will be FALSE EXPECT_FALSE(t.isElapsed()); t.start(); usleep(250000); // Timer has expired, so result will be TRUE EXPECT_TRUE(t.isElapsed()); } static atomic_int callback_called(0); void callback(Timer *t, int param) { if (t->isElapsed()) callback_called = param; else callback_called = param; } TEST(Timer, CallbackTest) { Timer t; callback_called = 0; t.setCallback(bind(callback, placeholders::_1, 4)); t.set(chrono::milliseconds(250)); t.start(); usleep(500 * 1000); EXPECT_TRUE(t.isElapsed()); EXPECT_EQ(callback_called, 4); } void callbackReset(Timer *t, atomic_int *param) { callback_called--; *param = 1; if (callback_called > 0) { t->reset(); } } TEST(Timer, CallbackResetTest) { Timer t; atomic_int test(0); callback_called = 4; t.setCallback(bind(callbackReset, placeholders::_1, &test)); t.set(chrono::milliseconds(100)); t.start(); while (test == 0) usleep(10); test = 0; EXPECT_EQ(callback_called, 3); while (test == 0) usleep(10); test = 0; EXPECT_EQ(callback_called, 2); while (test == 0) usleep(10); test = 0; EXPECT_EQ(callback_called, 1); while (test == 0) usleep(10); test = 0; EXPECT_EQ(callback_called, 0); usleep(500 * 1000); EXPECT_EQ(callback_called, 0); } void callbackStopStart(Timer *t, atomic_int *param) { Log::debug("callbackStopStart"); callback_called--; *param = 1; if (callback_called > 0) { t->reset(); } } TEST(Timer, CallbackStopStart) { Timer t; atomic_int test(0); callback_called = 5; t.setCallback(bind(callbackStopStart, placeholders::_1, &test)); t.set(chrono::milliseconds(100)); t.start(); while (test == 0) usleep(10); EXPECT_EQ(callback_called, 4); t.stop(); usleep(200 * 1000); test = 0; t.start(); int i = 0; // We must also quit after 1s. while (test == 0 && i < 1000) { i++; usleep(1000); } EXPECT_EQ(callback_called, 3); } TEST(Timer, CallbackPrematureEndTest) { Timer *t = new Timer(); callback_called = 0; t->setCallback(bind(callback, placeholders::_1, 4)); t->set(chrono::seconds(1)); auto begin = steady_clock::now(); t->start(); usleep(500000); delete t; auto end = steady_clock::now(); // The expected end-time must be in the future EXPECT_TRUE((begin + chrono::seconds(1)) > end); // The callback was not called EXPECT_EQ(callback_called, 0); } TEST(Timer, CallbackPrematureTriggerTest) { Timer *t = new Timer(); callback_called = 0; t->setCallback(bind(callback, placeholders::_1, 4)); t->set(chrono::seconds(1)); auto begin = steady_clock::now(); t->start(); usleep(75 * 1000); t->trigger(); usleep(75 * 1000); delete t; auto end = steady_clock::now(); // The expected end-time must be in the future EXPECT_TRUE((begin + chrono::seconds(1)) > end); // The callback was called EXPECT_EQ(callback_called, 4); }