2019-11-28 18:08:04 +08:00
|
|
|
|
#include "tkc/utils.h"
|
|
|
|
|
#include "base/idle.h"
|
2018-04-19 14:24:41 +08:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
static string s_log;
|
|
|
|
|
|
|
|
|
|
static ret_t on_idle(const idle_info_t* idle) {
|
|
|
|
|
s_log += "o:";
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 11:51:14 +08:00
|
|
|
|
static ret_t on_idle_repeat(const idle_info_t* idle) {
|
|
|
|
|
s_log += "r:";
|
|
|
|
|
return RET_REPEAT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ret_t on_idle_remove(const idle_info_t* idle) {
|
|
|
|
|
uint32_t id = (char*)(idle->ctx) - (char*)NULL;
|
|
|
|
|
|
|
|
|
|
idle_remove(id);
|
|
|
|
|
s_log += "remove:";
|
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:24:41 +08:00
|
|
|
|
TEST(Idle, basic) {
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
uint32_t nr = 10;
|
|
|
|
|
|
2018-06-29 15:46:43 +08:00
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
|
2018-04-19 14:24:41 +08:00
|
|
|
|
for (i = 0; i < nr; i++) {
|
2019-11-28 18:08:04 +08:00
|
|
|
|
ASSERT_EQ(idle_add(on_idle, tk_pointer_from_int(i)) > 0, true);
|
|
|
|
|
ASSERT_EQ(idle_exist(on_idle, tk_pointer_from_int(i)) > 0, true);
|
2018-04-19 14:24:41 +08:00
|
|
|
|
ASSERT_EQ(idle_count(), i + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 11:51:14 +08:00
|
|
|
|
s_log = "";
|
2018-04-19 14:24:41 +08:00
|
|
|
|
ASSERT_EQ(idle_dispatch(), RET_OK);
|
|
|
|
|
ASSERT_EQ(idle_count(), 0);
|
|
|
|
|
ASSERT_EQ(s_log, "o:o:o:o:o:o:o:o:o:o:");
|
2018-04-20 21:49:47 +08:00
|
|
|
|
|
2018-04-19 14:24:41 +08:00
|
|
|
|
for (i = 0; i < nr; i++) {
|
2019-11-28 18:08:04 +08:00
|
|
|
|
uint32_t id = idle_add(on_idle, tk_pointer_from_int(i));
|
|
|
|
|
ASSERT_EQ(idle_exist(on_idle, tk_pointer_from_int(i)) > 0, true);
|
2018-04-19 14:24:41 +08:00
|
|
|
|
ASSERT_EQ(id > 0, true);
|
|
|
|
|
ASSERT_EQ(idle_remove(id), RET_OK);
|
|
|
|
|
ASSERT_EQ(idle_count(), 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-04 11:51:14 +08:00
|
|
|
|
|
|
|
|
|
TEST(Idle, repeat) {
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
uint32_t nr = 10;
|
|
|
|
|
|
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
|
ASSERT_EQ(idle_add(on_idle_repeat, NULL) > 0, true);
|
|
|
|
|
ASSERT_EQ(idle_count(), i + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_log = "";
|
|
|
|
|
ASSERT_EQ(idle_dispatch(), RET_OK);
|
|
|
|
|
ASSERT_EQ(idle_count(), nr);
|
|
|
|
|
ASSERT_EQ(s_log, "r:r:r:r:r:r:r:r:r:r:");
|
|
|
|
|
|
|
|
|
|
s_log = "";
|
|
|
|
|
ASSERT_EQ(idle_dispatch(), RET_OK);
|
|
|
|
|
ASSERT_EQ(idle_count(), nr);
|
|
|
|
|
ASSERT_EQ(s_log, "r:r:r:r:r:r:r:r:r:r:");
|
|
|
|
|
|
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Idle, idle_remove) {
|
|
|
|
|
uint32_t id = 0;
|
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
|
|
|
|
|
id = idle_add(on_idle_repeat, NULL);
|
|
|
|
|
idle_add(on_idle_remove, ((char*)NULL) + id);
|
|
|
|
|
ASSERT_EQ(idle_count(), 2);
|
|
|
|
|
|
|
|
|
|
s_log = "";
|
|
|
|
|
ASSERT_EQ(idle_dispatch(), RET_OK);
|
|
|
|
|
ASSERT_EQ(idle_count(), 0);
|
|
|
|
|
ASSERT_EQ(s_log, "r:remove:");
|
|
|
|
|
|
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
}
|
2021-09-02 09:23:10 +08:00
|
|
|
|
|
|
|
|
|
TEST(Idle, id) {
|
|
|
|
|
uint32_t id1 = 0;
|
|
|
|
|
uint32_t id2 = 0;
|
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
|
|
|
|
|
id1 = idle_add(on_idle_repeat, NULL);
|
|
|
|
|
idle_manager()->next_idle_id = id1;
|
|
|
|
|
id2 = idle_add(on_idle_repeat, NULL);
|
|
|
|
|
ASSERT_NE(id1, id2);
|
|
|
|
|
|
|
|
|
|
idle_manager_remove_all(idle_manager());
|
|
|
|
|
}
|