加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0063-etmem-add-code-of-testcase.patch 26.55 KB
一键复制 编辑 原始数据 按行查看 历史
刘波 提交于 2022-08-01 10:57 . etmem: sync source repo submission
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
From 95029b0ae49082d660330042c6c2becc9133124d Mon Sep 17 00:00:00 2001
From: liubo <liubo254@huawei.com>
Date: Thu, 3 Mar 2022 10:25:08 +0800
Subject: [PATCH 13/33] etmem: add code of testcase
add code of testcase for etmem scan function
add code of testcase for etmem threadpool function
add code of testcase for etmem threadtimer function
Signed-off-by: liubo <liubo254@huawei.com>
---
.../etmem_scan_ops_export_llt_test/CMakeLists.txt | 26 ++
.../etmem_scan_ops_export_llt.c | 279 +++++++++++++++++++++
.../etmem_threadpool_ops_llt_test/CMakeLists.txt | 26 ++
.../etmem_threadpool_ops_llt.c | 260 +++++++++++++++++++
etmem/test/etmem_timer_ops_llt_test/CMakeLists.txt | 26 ++
.../etmem_timer_ops_llt_test/etmem_timer_ops_llt.c | 183 ++++++++++++++
6 files changed, 800 insertions(+)
create mode 100644 etmem/test/etmem_scan_ops_export_llt_test/CMakeLists.txt
create mode 100644 etmem/test/etmem_scan_ops_export_llt_test/etmem_scan_ops_export_llt.c
create mode 100644 etmem/test/etmem_threadpool_ops_llt_test/CMakeLists.txt
create mode 100644 etmem/test/etmem_threadpool_ops_llt_test/etmem_threadpool_ops_llt.c
create mode 100644 etmem/test/etmem_timer_ops_llt_test/CMakeLists.txt
create mode 100644 etmem/test/etmem_timer_ops_llt_test/etmem_timer_ops_llt.c
diff --git a/etmem/test/etmem_scan_ops_export_llt_test/CMakeLists.txt b/etmem/test/etmem_scan_ops_export_llt_test/CMakeLists.txt
new file mode 100644
index 0000000..be68154
--- /dev/null
+++ b/etmem/test/etmem_scan_ops_export_llt_test/CMakeLists.txt
@@ -0,0 +1,26 @@
+# /******************************************************************************
+# * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
+# * etmem is licensed under the Mulan PSL v2.
+# * You can use this software according to the terms and conditions of the Mulan PSL v2.
+# * You may obtain a copy of Mulan PSL v2 at:
+# * http://license.coscl.org.cn/MulanPSL2
+# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# * PURPOSE.
+# * See the Mulan PSL v2 for more details.
+# * Author: shikemeng
+# * Create: 2021-11-30
+# * Description: CMakefileList for etmem_scan_ops_llt_test
+# ******************************************************************************/
+
+project(etmem)
+
+INCLUDE_DIRECTORIES(../../inc/etmem_inc)
+INCLUDE_DIRECTORIES(../../inc/etmemd_inc)
+INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
+
+SET(EXE etmem_scan_ops_export_llt)
+
+add_executable(${EXE} etmem_scan_ops_export_llt.c)
+
+target_link_libraries(${EXE} cunit ${BUILD_DIR}/lib/libetmemd.so pthread dl rt libboundscheck numa ${GLIB2_LIBRARIES})
diff --git a/etmem/test/etmem_scan_ops_export_llt_test/etmem_scan_ops_export_llt.c b/etmem/test/etmem_scan_ops_export_llt_test/etmem_scan_ops_export_llt.c
new file mode 100644
index 0000000..e8e6098
--- /dev/null
+++ b/etmem/test/etmem_scan_ops_export_llt_test/etmem_scan_ops_export_llt.c
@@ -0,0 +1,279 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
+ * etmem is licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: yangkunlin
+ * Create: 2021-11-30
+ * Description: test for the export scan library
+ ******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <CUnit/Basic.h>
+#include <CUnit/Automated.h>
+#include <CUnit/Console.h>
+
+#include "etmemd_scan_export.h"
+
+/* normal init and exit */
+static void test_etmem_exp_scan_001(void)
+{
+ /* for test of exit without init*/
+ etmemd_scan_exit();
+
+ CU_ASSERT_EQUAL(etmemd_scan_init(), 0);
+ etmemd_scan_exit();
+}
+
+static void test_etmem_exp_scan_002(void)
+{
+ /* for test of exit without init*/
+ etmemd_scan_exit();
+
+ CU_ASSERT_EQUAL(etmemd_scan_init(), 0);
+
+ /* init again before exit */
+ CU_ASSERT_NOT_EQUAL(etmemd_scan_init(), 0);
+ etmemd_scan_exit();
+}
+
+static void check_vmas(struct vmas *vmas)
+{
+ int i;
+ struct vma *curr_vma = NULL;
+
+ CU_ASSERT_NOT_EQUAL(vmas->vma_cnt, 0);
+
+ curr_vma = vmas->vma_list;
+ for (i = 0; i < vmas->vma_cnt; i++) {
+ CU_ASSERT_PTR_NOT_NULL(curr_vma);
+ curr_vma = curr_vma->next;
+ }
+}
+
+/* test invalid get_vmas */
+static void test_etmem_exp_scan_004(void)
+{
+ const char *pid = "1";
+ char *vmflags_array[10] = {"rd"};
+ int vmflag_num = 1;
+ int is_anon_only = false;
+ struct vmas *vmas = NULL;
+
+ CU_ASSERT_EQUAL(etmemd_scan_init(), 0);
+
+ /* non-exist pid */
+ vmas = etmemd_get_vmas("0", vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NULL(vmas);
+
+ /* pid is NULL */
+ vmas = etmemd_get_vmas(NULL, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NULL(vmas);
+
+ /* pid contains invalid characters */
+ vmas = etmemd_get_vmas("1-", vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NULL(vmas);
+
+ /* vmflags contains space */
+ vmflags_array[0] = "r ";
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NULL(vmas);
+
+ /* vmflags length is not 2 */
+ vmflags_array[0] = "rd ";
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NULL(vmas);
+
+ /* vmflags is NULL */
+ vmflags_array[0] = NULL;
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NULL(vmas);
+
+ /* test free is NULL */
+ vmas = NULL;
+ etmemd_free_vmas(vmas);
+
+ etmemd_scan_exit();
+}
+
+
+/* test valid get_vmas */
+static void test_etmem_exp_scan_003(void)
+{
+ const char *pid = "1";
+ char *vmflags_array[10] = {"rd"};
+ int vmflag_num = 1;
+ int is_anon_only = false;
+ struct vmas *vmas = NULL;
+ struct vma *curr_vma = NULL;
+ int i;
+
+ /* get vmas without init */
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NOT_NULL(vmas);
+ check_vmas(vmas);
+ etmemd_free_vmas(vmas);
+
+ /* get vmas with init */
+ CU_ASSERT_EQUAL(etmemd_scan_init(), 0);
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NOT_NULL(vmas);
+ check_vmas(vmas);
+ etmemd_free_vmas(vmas);
+ etmemd_scan_exit();
+}
+
+/* test invalid get_page_refs */
+static void test_etmem_exp_scan_006(void)
+{
+ const char *pid = "1";
+ char *vmflags_array[10] = {"rd"};
+ int vmflag_num = 1;
+ int is_anon_only = false;
+ struct vmas *vmas = NULL;
+ struct page_refs *page_refs = NULL;
+ int flags = SCAN_AS_HUGE | SCAN_IGN_HOST;
+
+ CU_ASSERT_EQUAL(etmemd_scan_init(), 0);
+
+ /* free null pointer */
+ etmemd_free_page_refs(page_refs);
+
+ /* vmas is NULL */
+ CU_ASSERT_EQUAL(etmemd_get_page_refs(vmas, pid, &page_refs, flags), -1);
+
+ /* vmas address range invalid*/
+ vmas = (struct vmas *)calloc(1, sizeof(struct vmas));
+ CU_ASSERT_PTR_NOT_NULL(vmas);
+ vmas->vma_cnt = 1;
+
+ struct vma *vma = (struct vma *)calloc(1, sizeof(struct vma));
+ CU_ASSERT_PTR_NOT_NULL(vma);
+ vma->start = 0x0ff;
+ vma->end = 0x000;
+ vma->next = NULL;
+ vmas->vma_list = vma;
+
+ CU_ASSERT_EQUAL(etmemd_get_page_refs(vmas, pid, &page_refs, flags), -1);
+ etmemd_free_vmas(vmas);
+
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NOT_NULL(vmas);
+ check_vmas(vmas);
+
+ /* pid not exist */
+ CU_ASSERT_EQUAL(etmemd_get_page_refs(vmas, "0", &page_refs, flags), -1);
+ CU_ASSERT_PTR_NULL(page_refs);
+
+ /* pid is NULL */
+ CU_ASSERT_EQUAL(etmemd_get_page_refs(vmas, NULL, &page_refs, flags), -1);
+ CU_ASSERT_PTR_NULL(page_refs);
+
+ /* pid contains invalid chars */
+ CU_ASSERT_EQUAL(etmemd_get_page_refs(vmas, "--", &page_refs, flags), -1);
+ CU_ASSERT_PTR_NULL(page_refs);
+
+ etmemd_free_page_refs(page_refs);
+ etmemd_free_vmas(vmas);
+ etmemd_scan_exit();
+}
+
+/* test valid get_page_refs */
+static void test_etmem_exp_scan_005(void)
+{
+ CU_ASSERT_EQUAL(etmemd_scan_init(), 0);
+
+ const char *pid = "1";
+ char *vmflags_array[10] = {"rd"};
+ int vmflag_num = 1;
+ int is_anon_only = false;
+ struct vmas *vmas = NULL;
+ struct page_refs *page_refs = NULL;
+ int flags = SCAN_AS_HUGE | SCAN_IGN_HOST;
+
+ vmas = etmemd_get_vmas(pid, vmflags_array, vmflag_num, is_anon_only);
+ CU_ASSERT_PTR_NOT_NULL(vmas);
+ check_vmas(vmas);
+
+ CU_ASSERT_EQUAL(etmemd_get_page_refs(vmas, pid, &page_refs, flags), 0);
+ CU_ASSERT_PTR_NOT_NULL(page_refs);
+
+ etmemd_scan_exit();
+
+ /* get_page_refs after exit */
+ CU_ASSERT_NOT_EQUAL(etmemd_get_page_refs(vmas, pid, &page_refs, flags), 0);
+
+ etmemd_free_page_refs(page_refs);
+ etmemd_free_vmas(vmas);
+}
+
+typedef enum {
+ CUNIT_SCREEN = 0,
+ CUNIT_XMLFILE,
+ CUNIT_CONSOLE
+} cu_run_mode;
+
+int main(int argc, const char **argv)
+{
+ CU_pSuite suite;
+ CU_pTest pTest;
+ unsigned int num_failures;
+ cu_run_mode cunit_mode = CUNIT_SCREEN;
+ int error_num;
+
+ if (argc > 1) {
+ cunit_mode = atoi(argv[1]);
+ }
+
+ if (CU_initialize_registry() != CUE_SUCCESS) {
+ return CU_get_error();
+ }
+
+ suite = CU_add_suite("etmem_scan_ops_exp", NULL, NULL);
+ if (suite == NULL) {
+ goto ERROR;
+ }
+
+ if (CU_ADD_TEST(suite, test_etmem_exp_scan_001) == NULL ||
+ CU_ADD_TEST(suite, test_etmem_exp_scan_002) == NULL ||
+ CU_ADD_TEST(suite, test_etmem_exp_scan_003) == NULL ||
+ CU_ADD_TEST(suite, test_etmem_exp_scan_004) == NULL ||
+ CU_ADD_TEST(suite, test_etmem_exp_scan_005) == NULL ||
+ CU_ADD_TEST(suite, test_etmem_exp_scan_006) == NULL) {
+ goto ERROR;
+ }
+
+ switch (cunit_mode) {
+ case CUNIT_SCREEN:
+ CU_basic_set_mode(CU_BRM_VERBOSE);
+ CU_basic_run_tests();
+ break;
+ case CUNIT_XMLFILE:
+ CU_set_output_filename("etmemd_scan.c");
+ CU_automated_run_tests();
+ break;
+ case CUNIT_CONSOLE:
+ CU_console_run_tests();
+ break;
+ default:
+ printf("not support cunit mode, only support: "
+ "0 for CUNIT_SCREEN, 1 for CUNIT_XMLFILE, 2 for CUNIT_CONSOLE\n");
+ goto ERROR;
+ }
+
+ num_failures = CU_get_number_of_failures();
+ CU_cleanup_registry();
+ return num_failures;
+
+ERROR:
+ error_num = CU_get_error();
+ CU_cleanup_registry();
+ return -error_num;
+}
diff --git a/etmem/test/etmem_threadpool_ops_llt_test/CMakeLists.txt b/etmem/test/etmem_threadpool_ops_llt_test/CMakeLists.txt
new file mode 100644
index 0000000..cb084cf
--- /dev/null
+++ b/etmem/test/etmem_threadpool_ops_llt_test/CMakeLists.txt
@@ -0,0 +1,26 @@
+# /******************************************************************************
+# * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
+# * etmem is licensed under the Mulan PSL v2.
+# * You can use this software according to the terms and conditions of the Mulan PSL v2.
+# * You may obtain a copy of Mulan PSL v2 at:
+# * http://license.coscl.org.cn/MulanPSL2
+# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# * PURPOSE.
+# * See the Mulan PSL v2 for more details.
+# * Author: liubo
+# * Create: 2021-11-30
+# * Description: CMakefileList for etmem_threadpool_ops_llt_test
+# ******************************************************************************/
+
+project(etmem)
+
+INCLUDE_DIRECTORIES(../../inc/etmem_inc)
+INCLUDE_DIRECTORIES(../../inc/etmemd_inc)
+INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
+
+SET(EXE etmem_threadpool_ops_llt)
+
+add_executable(${EXE} etmem_threadpool_ops_llt.c)
+
+target_link_libraries(${EXE} cunit ${BUILD_DIR}/lib/libetmemd.so pthread dl rt libboundscheck numa ${GLIB2_LIBRARIES})
diff --git a/etmem/test/etmem_threadpool_ops_llt_test/etmem_threadpool_ops_llt.c b/etmem/test/etmem_threadpool_ops_llt_test/etmem_threadpool_ops_llt.c
new file mode 100644
index 0000000..9917954
--- /dev/null
+++ b/etmem/test/etmem_threadpool_ops_llt_test/etmem_threadpool_ops_llt.c
@@ -0,0 +1,260 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
+ * etmem is licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: liubo
+ * Create: 2020-08-13
+ * Description: test for etmem threadpool operations
+ ******************************************************************************/
+#include <sys/file.h>
+#include <sys/sysinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <limits.h>
+#include <glib.h>
+#include <pthread.h>
+
+#include <CUnit/Basic.h>
+#include <CUnit/Automated.h>
+#include <CUnit/Console.h>
+
+#include "etmemd_threadpool.h"
+#include "etmemd_pool_adapter.h"
+#include "etmemd_project.h"
+#include "etmemd_file.h"
+#include "etmemd_common.h"
+#include "etmemd_engine.h"
+#include "etmemd_slide.h"
+#include "etmemd_cslide.h"
+#include "securec.h"
+
+#define ADD_WORKER_NUM 100
+typedef void *(*add_worker_exector)(void *);
+
+static struct task_executor g_test_exec;
+static int g_task_exe_time = 0;
+static struct engine_ops g_test_eng_ops = {
+ .alloc_pid_params = NULL,
+ .free_pid_params = NULL,
+};
+
+static void get_threadpool_create_num(int num, int exp)
+{
+ thread_pool *pool = NULL;
+
+ pool = threadpool_create(num);
+ CU_ASSERT_PTR_NOT_NULL(pool);
+
+ CU_ASSERT_EQUAL(pool->max_thread_cap, exp);
+ threadpool_stop_and_destroy(&pool);
+}
+
+static void test_threadpool_create(void)
+{
+ int core = get_nprocs();
+
+ get_threadpool_create_num(1, 1);
+ get_threadpool_create_num(2, 2);
+ get_threadpool_create_num(2 * core, 2 * core);
+ get_threadpool_create_num(2 * core + 1, 2 * core + 1);
+
+ threadpool_notify(NULL);
+ threadpool_reset_status(NULL);
+}
+
+static void test_threadpool_delete(void)
+{
+ thread_pool *pool = NULL;
+ threadpool_stop_and_destroy(&pool);
+
+ pool = threadpool_create(1);
+ CU_ASSERT_PTR_NOT_NULL(pool);
+
+ threadpool_stop_and_destroy(&pool);
+ CU_ASSERT_PTR_NULL(pool);
+}
+
+static void *add_worker_fun(void *str)
+{
+ char *temp_str = str;
+ printf("str: %s \n", temp_str);
+ return NULL;
+}
+
+static unsigned int get_workerlist_num(const thread_pool *pool)
+{
+ unsigned int num = 0;
+ thread_worker *worker = pool->worker_list;
+
+ while (worker) {
+ num++;
+ worker = worker->next_node;
+ }
+
+ return num;
+}
+
+static void test_thpool_addwk_single(void)
+{
+ char *args = "for add worker test.\n";
+ thread_pool *pool = NULL;
+ add_worker_exector exector = add_worker_fun;
+
+ pool = threadpool_create(1);
+ CU_ASSERT_PTR_NOT_NULL(pool);
+
+ CU_ASSERT_EQUAL(threadpool_add_worker(pool, NULL, NULL), -1);
+ CU_ASSERT_EQUAL(threadpool_add_worker(pool, exector, NULL), -1);
+ CU_ASSERT_EQUAL(threadpool_add_worker(pool, NULL, args), -1);
+ CU_ASSERT_EQUAL(threadpool_add_worker(pool, exector, args), 0);
+
+ CU_ASSERT_PTR_NOT_NULL(pool->worker_list);
+ CU_ASSERT_EQUAL(get_workerlist_num(pool), 1);
+
+ threadpool_stop_and_destroy(&pool);
+ CU_ASSERT_PTR_NULL(pool);
+}
+
+static void test_thpool_addwk_mul(void)
+{
+ char *args = "for add worker test.\n";
+ thread_pool *pool = NULL;
+ int add_num;
+ add_worker_exector exector = add_worker_fun;
+
+ pool = threadpool_create(1);
+ CU_ASSERT_PTR_NOT_NULL(pool);
+
+ for (add_num = 0; add_num < ADD_WORKER_NUM; add_num++) {
+ CU_ASSERT_EQUAL(threadpool_add_worker(pool, exector, args), 0);
+ }
+
+ CU_ASSERT_PTR_NOT_NULL(pool->worker_list);
+ CU_ASSERT_EQUAL(__atomic_load_n(&pool->scheduing_size, __ATOMIC_SEQ_CST), ADD_WORKER_NUM);
+
+ threadpool_stop_and_destroy(&pool);
+ CU_ASSERT_PTR_NULL(pool);
+}
+
+static void init_thpool_objs(struct project *proj, struct engine *eng, struct task *tk)
+{
+ proj->interval = 1;
+ proj->start = true;
+ proj->name = "test_project";
+ eng->proj = proj;
+ eng->ops = &g_test_eng_ops;
+ tk->eng = eng;
+ tk->type = "pid";
+ tk->value = "1";
+ tk->max_threads = 10;
+}
+
+static void test_thpool_start_error(void)
+{
+ struct task tk;
+ struct engine eng;
+ struct project proj;
+
+ init_thpool_objs(&proj, &eng, &tk);
+
+ tk.max_threads = 0;
+ g_test_exec.tk = &tk;
+
+ CU_ASSERT_EQUAL(start_threadpool_work(&g_test_exec), -1);
+}
+
+static void *task_executor(void *arg)
+{
+ g_task_exe_time++;
+ return NULL;
+}
+
+static void test_thpool_start_stop(void)
+{
+ struct task tk = {0};
+ struct engine eng = {0};
+ struct project proj = {0};
+
+ init_thpool_objs(&proj, &eng, &tk);
+
+ g_test_exec.tk = &tk;
+ g_test_exec.func = task_executor;
+
+ CU_ASSERT_EQUAL(start_threadpool_work(&g_test_exec), 0);
+ /* wait threadpool to work */
+ sleep(2);
+ stop_and_delete_threadpool_work(&tk);
+}
+
+typedef enum {
+ CUNIT_SCREEN = 0,
+ CUNIT_XMLFILE,
+ CUNIT_CONSOLE
+} cu_run_mode;
+
+int main(int argc, const char **argv)
+{
+ CU_pSuite suite;
+ CU_pTest pTest;
+ unsigned int num_failures;
+ cu_run_mode cunit_mode = CUNIT_SCREEN;
+ int error_num;
+
+ if (argc > 1) {
+ cunit_mode = atoi(argv[1]);
+ }
+
+ if (CU_initialize_registry() != CUE_SUCCESS) {
+ return CU_get_error();
+ }
+
+ suite = CU_add_suite("etmem_threadpool_ops", NULL, NULL);
+ if (suite == NULL) {
+ goto ERROR;
+ }
+
+ if (CU_ADD_TEST(suite, test_threadpool_create) == NULL ||
+ CU_ADD_TEST(suite, test_threadpool_delete) == NULL ||
+ CU_ADD_TEST(suite, test_thpool_addwk_single) == NULL ||
+ CU_ADD_TEST(suite, test_thpool_addwk_mul) == NULL ||
+ CU_ADD_TEST(suite, test_thpool_start_stop) == NULL ||
+ CU_ADD_TEST(suite, test_thpool_start_error) == NULL) {
+ goto ERROR;
+ }
+
+ switch (cunit_mode) {
+ case CUNIT_SCREEN:
+ CU_basic_set_mode(CU_BRM_VERBOSE);
+ CU_basic_run_tests();
+ break;
+ case CUNIT_XMLFILE:
+ CU_set_output_filename("etmemd_threadpool.c");
+ CU_automated_run_tests();
+ break;
+ case CUNIT_CONSOLE:
+ CU_console_run_tests();
+ break;
+ default:
+ printf("not support cunit mode, only support: "
+ "0 for CUNIT_SCREEN, 1 for CUNIT_XMLFILE, 2 for CUNIT_CONSOLE\n");
+ goto ERROR;
+ }
+
+ num_failures = CU_get_number_of_failures();
+ CU_cleanup_registry();
+ return num_failures;
+
+ERROR:
+ error_num = CU_get_error();
+ CU_cleanup_registry();
+ return -error_num;
+}
diff --git a/etmem/test/etmem_timer_ops_llt_test/CMakeLists.txt b/etmem/test/etmem_timer_ops_llt_test/CMakeLists.txt
new file mode 100644
index 0000000..76aa48e
--- /dev/null
+++ b/etmem/test/etmem_timer_ops_llt_test/CMakeLists.txt
@@ -0,0 +1,26 @@
+# /******************************************************************************
+# * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
+# * etmem is licensed under the Mulan PSL v2.
+# * You can use this software according to the terms and conditions of the Mulan PSL v2.
+# * You may obtain a copy of Mulan PSL v2 at:
+# * http://license.coscl.org.cn/MulanPSL2
+# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# * PURPOSE.
+# * See the Mulan PSL v2 for more details.
+# * Author: liubo
+# * Create: 2021-11-30
+# * Description: CMakefileList for etmem_timer_ops_llt_test
+# ******************************************************************************/
+
+project(etmem)
+
+INCLUDE_DIRECTORIES(../../inc/etmem_inc)
+INCLUDE_DIRECTORIES(../../inc/etmemd_inc)
+INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
+
+SET(EXE etmem_timer_ops_llt)
+
+add_executable(${EXE} etmem_timer_ops_llt.c)
+
+target_link_libraries(${EXE} cunit ${BUILD_DIR}/lib/libetmemd.so pthread dl rt libboundscheck numa ${GLIB2_LIBRARIES})
diff --git a/etmem/test/etmem_timer_ops_llt_test/etmem_timer_ops_llt.c b/etmem/test/etmem_timer_ops_llt_test/etmem_timer_ops_llt.c
new file mode 100644
index 0000000..c97cdf9
--- /dev/null
+++ b/etmem/test/etmem_timer_ops_llt_test/etmem_timer_ops_llt.c
@@ -0,0 +1,183 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
+ * etmem is licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: liubo
+ * Create: 2020-08-13
+ * Description: test for etmem timer operations
+ ******************************************************************************/
+#include <sys/file.h>
+#include <sys/sysinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include <CUnit/Basic.h>
+#include <CUnit/Automated.h>
+#include <CUnit/Console.h>
+
+#include "etmemd_threadtimer.h"
+
+static int g_timer_exec_time = 0;
+
+typedef void *(*timer_exector)(void *);
+
+static void get_timer_expired_time(int time, int exp)
+{
+ timer_thread *timer = NULL;
+
+ timer = thread_timer_create(time);
+ CU_ASSERT_PTR_NOT_NULL(timer);
+
+ CU_ASSERT_EQUAL(timer->expired_time, exp);
+ thread_timer_destroy(&timer);
+}
+
+static void test_timer_create_delete(void)
+{
+ timer_thread *timer = NULL;
+ thread_timer_destroy(&timer);
+
+ get_timer_expired_time(1, 1);
+ get_timer_expired_time(2, 2);
+ get_timer_expired_time(50, 50);
+ get_timer_expired_time(1199, 1199);
+ get_timer_expired_time(1201, 1201);
+}
+
+static void *threadtimer_exector(void *str)
+{
+ char *temp_str = str;
+ printf("threadtimer_exector: %s\n", temp_str);
+ g_timer_exec_time++;
+ return NULL;
+}
+
+static void test_timer_start_error(void)
+{
+ char *timer_args = "for timer start test.\n";
+ timer_exector exector = threadtimer_exector;
+ timer_thread *timer = NULL;
+
+ timer = thread_timer_create(60);
+ CU_ASSERT_PTR_NOT_NULL(timer);
+
+ CU_ASSERT_EQUAL(thread_timer_start(NULL, NULL, NULL), -1);
+ CU_ASSERT_EQUAL(thread_timer_start(timer, NULL, NULL), -1);
+ CU_ASSERT_EQUAL(thread_timer_start(timer, exector, NULL), -1);
+ CU_ASSERT_EQUAL(thread_timer_start(timer, NULL, timer_args), -1);
+ CU_ASSERT_EQUAL(thread_timer_start(timer, exector, timer_args), 0);
+ CU_ASSERT_FALSE(timer->down);
+ CU_ASSERT_EQUAL(thread_timer_start(timer, exector, timer_args), 0);
+
+ thread_timer_destroy(&timer);
+ thread_timer_stop(timer);
+}
+
+static void test_timer_start_ok(void)
+{
+ char *timer_args = "for timer start test.\n";
+ timer_exector exector = threadtimer_exector;
+ timer_thread *timer = NULL;
+
+ timer = thread_timer_create(1);
+ CU_ASSERT_PTR_NOT_NULL(timer);
+
+ CU_ASSERT_EQUAL(thread_timer_start(timer, exector, timer_args), 0);
+ CU_ASSERT_FALSE(timer->down);
+
+ sleep(2);
+ CU_ASSERT_NOT_EQUAL(g_timer_exec_time, 0);
+
+ thread_timer_stop(timer);
+ thread_timer_destroy(&timer);
+}
+
+static void test_timer_stop(void)
+{
+ char *timer_args = "for timer start test.\n";
+ timer_exector exector = threadtimer_exector;
+ timer_thread *timer = NULL;
+
+ thread_timer_stop(timer);
+
+ timer = thread_timer_create(60);
+ CU_ASSERT_PTR_NOT_NULL(timer);
+
+ CU_ASSERT_EQUAL(thread_timer_start(timer, exector, timer_args), 0);
+ CU_ASSERT_FALSE(timer->down);
+
+ thread_timer_stop(timer);
+ CU_ASSERT_TRUE(timer->down);
+ thread_timer_destroy(&timer);
+}
+
+typedef enum {
+ CUNIT_SCREEN = 0,
+ CUNIT_XMLFILE,
+ CUNIT_CONSOLE
+} cu_run_mode;
+
+int main(int argc, const char **argv)
+{
+ CU_pSuite suite;
+ CU_pTest pTest;
+ unsigned int num_failures;
+ cu_run_mode cunit_mode = CUNIT_SCREEN;
+ int error_num;
+
+ if (argc > 1) {
+ cunit_mode = atoi(argv[1]);
+ }
+
+ if (CU_initialize_registry() != CUE_SUCCESS) {
+ return CU_get_error();
+ }
+
+ suite = CU_add_suite("etmem_timer_ops", NULL, NULL);
+ if (suite == NULL) {
+ goto ERROR;
+ }
+
+ if (CU_ADD_TEST(suite, test_timer_create_delete) == NULL ||
+ CU_ADD_TEST(suite, test_timer_start_error) == NULL ||
+ CU_ADD_TEST(suite, test_timer_start_ok) == NULL ||
+ CU_ADD_TEST(suite, test_timer_stop) == NULL) {
+ goto ERROR;
+ }
+
+ switch (cunit_mode) {
+ case CUNIT_SCREEN:
+ CU_basic_set_mode(CU_BRM_VERBOSE);
+ CU_basic_run_tests();
+ break;
+ case CUNIT_XMLFILE:
+ CU_set_output_filename("etmemd_threadtimer.c");
+ CU_automated_run_tests();
+ break;
+ case CUNIT_CONSOLE:
+ CU_console_run_tests();
+ break;
+ default:
+ printf("not support cunit mode, only support: "
+ "0 for CUNIT_SCREEN, 1 for CUNIT_XMLFILE, 2 for CUNIT_CONSOLE\n");
+ goto ERROR;
+ }
+
+ num_failures = CU_get_number_of_failures();
+ CU_cleanup_registry();
+ return num_failures;
+
+ERROR:
+ error_num = CU_get_error();
+ CU_cleanup_registry();
+ return -error_num;
+}
--
1.8.3.1
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化