#include "helper.h"
#include "knet.h"
static int Test_Timer_i = 0;
CASE(Test_Timer_Run_Once) {
struct holder {
static void timer_cb(ktimer_t* t, void*) {
Test_Timer_i++;
EXPECT_TRUE(ktimer_check_dead(t));
}
};
ktimer_loop_t* l = ktimer_loop_create(1000);
for (int i = 0; i < 100; i++) {
ktimer_t* t = ktimer_create(l);
ktimer_start_once(t, &holder::timer_cb, 0, 900);
}
thread_sleep_ms(1000);
ktimer_loop_run_once(l);
EXPECT_TRUE(100 == Test_Timer_i);
ktimer_loop_destroy(l);
}
CASE(Test_Timer_Run) {
Test_Timer_i = 0;
struct holder {
static void timer_cb(ktimer_t* t, void*) {
EXPECT_FALSE(ktimer_check_dead(t));
Test_Timer_i++;
if (Test_Timer_i >= 100) {
ktimer_loop_exit(ktimer_get_loop(t));
}
}
};
ktimer_loop_t* l = ktimer_loop_create(1000);
for (int i = 0; i < 100; i++) {
ktimer_t* t = ktimer_create(l);
ktimer_start(t, &holder::timer_cb, 0, 1000);
}
ktimer_loop_run(l);
EXPECT_TRUE(100 <= Test_Timer_i);
ktimer_loop_destroy(l);
}
CASE(Test_Timer_Start_Times) {
Test_Timer_i = 0;
struct holder {
static void timer_cb(ktimer_t* t, void*) {
Test_Timer_i++;
if (Test_Timer_i >= 200) {
ktimer_loop_exit(ktimer_get_loop(t));
}
}
};
ktimer_loop_t* l = ktimer_loop_create(1000);
for (int i = 0; i < 100; i++) {
ktimer_t* t = ktimer_create(l);
ktimer_start_times(t, &holder::timer_cb, 0, 1000, 2);
}
ktimer_loop_run(l);
EXPECT_TRUE(200 == Test_Timer_i);
ktimer_loop_destroy(l);
}
CASE(Test_Timer_Stop) {
Test_Timer_i = 0;
struct holder {
static void timer_cb(ktimer_t* t, void*) {
EXPECT_TRUE(error_ok == ktimer_stop(t));
Test_Timer_i++;
if (Test_Timer_i >= 100) {
ktimer_loop_exit(ktimer_get_loop(t));
}
}
};
ktimer_loop_t* l = ktimer_loop_create(1000);
for (int i = 0; i < 100; i++) {
ktimer_t* t = ktimer_create(l);
ktimer_start(t, &holder::timer_cb, 0, 1000);
}
ktimer_loop_run(l);
EXPECT_TRUE(100 == Test_Timer_i);
ktimer_loop_destroy(l);
}