From 07f2d8b4cc361444a715cc498ce18ecac7f20204 Mon Sep 17 00:00:00 2001 From: fenghaiyue Date: Wed, 21 Sep 2022 16:50:32 +0800 Subject: [PATCH] system cpu probe: add unit test --- test/test_probes/CMakeLists.txt | 1 + test/test_probes/main.c | 2 +- test/test_probes/test_probes.c | 50 ++++++++++++++++++++++++++++++++- test/test_probes/test_probes.h | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/test/test_probes/CMakeLists.txt b/test/test_probes/CMakeLists.txt index 9ec98088..e4ba433d 100644 --- a/test/test_probes/CMakeLists.txt +++ b/test/test_probes/CMakeLists.txt @@ -43,6 +43,7 @@ SET(SOURCES main.c test_probes.c ${PROBE_DIR}/probe.c ${PROBE_DIR}/extend_probe.c ${IMDB_DIR}/imdb.c + ${IMDB_DIR}/metrics.c ${WEBSERVER_DIR}/web_server.c ${COMMON_DIR}/util.c diff --git a/test/test_probes/main.c b/test/test_probes/main.c index 749bcff8..8937bf3f 100644 --- a/test/test_probes/main.c +++ b/test/test_probes/main.c @@ -35,7 +35,7 @@ int main(int argc, char *argv[]) return CU_get_error(); } - CU_ADD_TEST(suite, test_probe_meta_coinstance); + CU_ADD_TEST(suite, testProbesMain); CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); diff --git a/test/test_probes/test_probes.c b/test/test_probes/test_probes.c index 31682af9..2c98a4c3 100644 --- a/test/test_probes/test_probes.c +++ b/test/test_probes/test_probes.c @@ -15,10 +15,58 @@ #include #include #include +#include "probe.h" +#include "../../probes/system_infos.probe/system_cpu.h" -void test_probe_meta_coinstance(void) +static void TestSystemCpuInit(void) { uint32_t ret = 0; + ret = system_cpu_init(); CU_ASSERT(ret == 0); + system_cpu_destroy(); +} + +static void TestSystemCpuProbe(void) +{ + uint32_t ret = 0; + uint32_t *elemP = NULL; + struct probe_params params = {.period = DEFAULT_PERIOD}; + ret = system_cpu_init(); + CU_ASSERT(ret == 0); + + ret = system_cpu_probe(¶ms); + CU_ASSERT(ret == 0); + + // nprobe_fprintf的g_probe是null,需要初始化 + // 按照代码逻辑,第二次上报指标信息,需要检测两次 + g_probe = ProbeCreate(); + CU_ASSERT(g_probe != NULL); + + if (g_probe != NULL) { + CU_ASSERT(g_probe->fifo != NULL); + (void)snprintf(g_probe->name, MAX_PROBE_NAME_LEN - 1, "test_cpu_probe"); + + ret = system_cpu_probe(¶ms); + CU_ASSERT(ret == 0); + + ret = FifoGet(g_probe->fifo, (void **) &elemP); + CU_ASSERT(ret == 0); + CU_ASSERT(g_probe->fifo->out == 1); + + ProbeDestroy(g_probe); + } + system_cpu_destroy(); +} + +static void TestSystemCpu(void) +{ + TestSystemCpuInit(); + TestSystemCpuProbe(); +} + + +void testProbesMain(void) +{ + TestSystemCpu(); } diff --git a/test/test_probes/test_probes.h b/test/test_probes/test_probes.h index 68e0f8fc..fac694ad 100644 --- a/test/test_probes/test_probes.h +++ b/test/test_probes/test_probes.h @@ -15,7 +15,7 @@ #ifndef __TEST_PROBES_H__ #define __TEST_PROBES_H__ -void test_probe_meta_coinstance(void); +void testProbesMain(void); #endif -- Gitee