extern_interface/tests/test_event.cpp

72 lines
1.5 KiB
C++

#include <vector>
#include <thread>
#include <unistd.h>
#include "logging/interface.h"
#include "exint/event.h"
#include "exint/detail.hpp"
#include "c_testcase.h"
static std::vector<int> g_vec;
ON_EVENT_EX(test, _test_handler, nullptr) {
int* arg = (int*)args;
g_vec.push_back(*arg);
}
SETUP {
log_set_level(LOG_LEVEL_DEBUG);
g_vec.clear();
return 0;
}
TEARDOWN {
exint_event_thread_stop();
return 0;
}
TEST_CASE(test_unknow_event) {
exint_event("unknow_event", 0, NULL);
END_TEST;
}
TEST_CASE(test_event) {
int i = 1;
exint_event("test", sizeof(int), &i);
assert_eq(g_vec.size(), 1);
assert_eq(g_vec[0], i);
END_TEST;
}
TEST_CASE(test_event_thread) {
exint_event_thread_start(2);
int i = 2;
int ret = 0;
exint_event("test", sizeof(int), &i);
for (int i = 0; i < 10; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (g_vec.size() != 1) {
continue;
}
assert_eq(g_vec[0], 2);
goto end;
}
ret = 1;
end:
exint_event_thread_stop();
return ret;
}
TEST_CASE(test_public)
{
assert_eq(g_vec.size(), 0);
exint_public_event_register("test_public", _test_handler, nullptr);
exint_public_event("test_public", 0, NULL);
assert_eq(g_vec.size(), 1);
assert_eq(g_vec[0], 0);
exint_public_event_unregister("test_public", _test_handler, nullptr);
exint_public_event("test_public", 0, NULL);
assert_eq(g_vec.size(), 1);
END_TEST;
}