test: for tsim

This commit is contained in:
Shengliang Guan 2024-12-12 19:37:34 +08:00
parent 414b1c3061
commit b5f2d4d382
9 changed files with 248 additions and 67 deletions

View File

@ -35,7 +35,7 @@ static void *funcPtr1(void *param) {
return NULL; return NULL;
} }
TEST(osThreadTests, invalidParameter) { TEST(osThreadTests, para1) {
TdThread tid1 = {0}; TdThread tid1 = {0};
TdThread tid2 = {0}; TdThread tid2 = {0};
int32_t reti = 0; int32_t reti = 0;
@ -192,3 +192,62 @@ TEST(osThreadTests, invalidParameter) {
reti = taosThreadCondAttrDestroy(&condattr); reti = taosThreadCondAttrDestroy(&condattr);
EXPECT_EQ(reti, 0); EXPECT_EQ(reti, 0);
} }
TEST(osThreadTests, option) {
int32_t reti = 0;
TdThreadSpinlock lock = {0};
reti = taosThreadSpinInit(&lock, -1);
EXPECT_NE(reti, 0);
reti = taosThreadSpinLock(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinTrylock(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinUnlock(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinDestroy(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinInit(NULL, 0);
EXPECT_NE(reti, 0);
reti = taosThreadSpinLock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadSpinTrylock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadSpinUnlock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadSpinDestroy(NULL);
EXPECT_NE(reti, 0);
}
TEST(osThreadTests, splilock) {
int32_t reti = 0;
TdThreadSpinlock lock = {0};
reti = taosThreadSpinInit(&lock, -1);
EXPECT_NE(reti, 0);
reti = taosThreadSpinLock(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinTrylock(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinUnlock(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinDestroy(&lock);
EXPECT_NE(reti, 0);
reti = taosThreadSpinInit(NULL, 0);
EXPECT_NE(reti, 0);
reti = taosThreadSpinLock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadSpinTrylock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadSpinUnlock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadSpinDestroy(NULL);
EXPECT_NE(reti, 0);
}
TEST(osThreadTests, others) {
taosThreadTestCancel();
taosThreadClear(NULL);
}

View File

@ -1,14 +1,29 @@
aux_source_directory(src TSIM_SRC) LIST(APPEND TSIM_SRC src/simEntry.c)
add_executable(tsim ${TSIM_SRC}) LIST(APPEND TSIM_SRC src/simExec.c)
target_link_libraries( LIST(APPEND TSIM_SRC src/simParse.c)
tsim LIST(APPEND TSIM_SRC src/simSystem.c)
ADD_LIBRARY(tsim_static STATIC ${TSIM_SRC})
TARGET_INCLUDE_DIRECTORIES(
tsim_static
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/inc"
)
TARGET_LINK_LIBRARIES(
tsim_static
PUBLIC ${TAOS_LIB} PUBLIC ${TAOS_LIB}
PUBLIC util PUBLIC util
PUBLIC common PUBLIC common
PUBLIC os PUBLIC os
PUBLIC cjson PUBLIC cjson
) )
target_include_directories(
LIST(APPEND TSIM_EXE_SRC src/simMain.c)
ADD_EXECUTABLE(tsim ${TSIM_EXE_SRC})
TARGET_LINK_LIBRARIES(
tsim tsim
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" PUBLIC tsim_static
) )
IF(${BUILD_TEST})
ADD_SUBDIRECTORY(test)
ENDIF(${BUILD_TEST})

View File

@ -16,6 +16,10 @@
#ifndef _TD_SIM_INT_H_ #ifndef _TD_SIM_INT_H_
#define _TD_SIM_INT_H_ #define _TD_SIM_INT_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "os.h" #include "os.h"
#include "cJSON.h" #include "cJSON.h"
@ -161,8 +165,8 @@ typedef struct _script_t {
int32_t type; int32_t type;
bool killed; bool killed;
void *taos; void *taos;
char rows[12]; // number of rows data retrieved char rows[12]; // number of rows data retrieved
char cols[12]; // number of columns data retrieved char cols[12]; // number of columns data retrieved
char data[MAX_QUERY_ROW_NUM][MAX_QUERY_COL_NUM][MAX_QUERY_VALUE_LEN]; // query results char data[MAX_QUERY_ROW_NUM][MAX_QUERY_COL_NUM][MAX_QUERY_VALUE_LEN]; // query results
char system_exit_code[12]; char system_exit_code[12];
char system_ret_content[MAX_SYSTEM_RESULT_LEN]; char system_ret_content[MAX_SYSTEM_RESULT_LEN];
@ -192,7 +196,7 @@ SScript *simParseScript(char *fileName);
SScript *simProcessCallOver(SScript *script); SScript *simProcessCallOver(SScript *script);
void *simExecuteScript(void *script); void *simExecuteScript(void *script);
void simInitsimCmdList(); void simInitsimCmdList();
bool simSystemInit(); void simSystemInit();
void simSystemCleanUp(); void simSystemCleanUp();
char *simGetVariable(SScript *script, char *varName, int32_t varLen); char *simGetVariable(SScript *script, char *varName, int32_t varLen);
bool simExecuteExpCmd(SScript *script, char *option); bool simExecuteExpCmd(SScript *script, char *option);
@ -214,4 +218,11 @@ bool simExecuteLineInsertErrorCmd(SScript *script, char *option);
bool simExecuteSetBIModeCmd(SScript *script, char *option); bool simExecuteSetBIModeCmd(SScript *script, char *option);
void simVisuallizeOption(SScript *script, char *src, char *dst); void simVisuallizeOption(SScript *script, char *src, char *dst);
int32_t simEntry(int32_t argc, char **argv);
void simHandleSignal(int32_t signo, void *sigInfo, void *context);
#ifdef __cplusplus
}
#endif
#endif /*_TD_SIM_INT_H_*/ #endif /*_TD_SIM_INT_H_*/

64
utils/tsim/src/simEntry.c Normal file
View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _DEFAULT_SOURCE
#include "simInt.h"
bool simExecSuccess = false;
bool abortExecution = false;
bool useValgrind = false;
void simHandleSignal(int32_t signo, void *sigInfo, void *context) {
simSystemCleanUp();
abortExecution = true;
}
int32_t simEntry(int32_t argc, char **argv) {
char scriptFile[MAX_FILE_NAME_LEN] = "sim_main_test.sim";
for (int32_t i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-c") == 0 && i < argc - 1) {
tstrncpy(configDir, argv[++i], 128);
} else if (strcmp(argv[i], "-f") == 0 && i < argc - 1) {
tstrncpy(scriptFile, argv[++i], MAX_FILE_NAME_LEN);
} else if (strcmp(argv[i], "-v") == 0) {
useValgrind = true;
} else {
printf("usage: %s [options] \n", argv[0]);
printf(" [-c config]: config directory, default is: %s\n", configDir);
printf(" [-f script]: script filename\n");
return 0;
}
}
simInfo("simulator is running ...");
simSystemInit();
taosSetSignal(SIGINT, simHandleSignal);
SScript *script = simParseScript(scriptFile);
if (script == NULL) {
simError("parse script file:%s failed", scriptFile);
return -1;
}
simScriptList[++simScriptPos] = script;
simExecuteScript(script);
int32_t ret = simExecSuccess ? 0 : -1;
simInfo("execute result %d", ret);
return ret;
}

View File

@ -382,8 +382,8 @@ bool simExecuteRunBackCmd(SScript *script, char *option) {
return true; return true;
} }
void simReplaceDirSep(char *buf) {
#ifdef WINDOWS #ifdef WINDOWS
void simReplaceDirSep(char *buf) {
int i = 0; int i = 0;
while (buf[i] != '\0') { while (buf[i] != '\0') {
if (buf[i] == '/') { if (buf[i] == '/') {
@ -391,8 +391,8 @@ void simReplaceDirSep(char *buf) {
} }
i++; i++;
} }
#endif
} }
#endif
bool simReplaceStr(char *buf, char *src, char *dst) { bool simReplaceStr(char *buf, char *src, char *dst) {
bool replaced = false; bool replaced = false;

View File

@ -16,58 +16,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "simInt.h" #include "simInt.h"
bool simExecSuccess = false;
bool abortExecution = false;
bool useValgrind = false;
void simHandleSignal(int32_t signo, void *sigInfo, void *context) {
simSystemCleanUp();
abortExecution = true;
}
int32_t main(int32_t argc, char *argv[]) { int32_t main(int32_t argc, char *argv[]) {
char scriptFile[MAX_FILE_NAME_LEN] = "sim_main_test.sim"; // entry function used for unit testing.
return simEntry(argc, argv);
for (int32_t i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-c") == 0 && i < argc - 1) {
tstrncpy(configDir, argv[++i], 128);
} else if (strcmp(argv[i], "-f") == 0 && i < argc - 1) {
tstrncpy(scriptFile, argv[++i], MAX_FILE_NAME_LEN);
} else if (strcmp(argv[i], "-v") == 0) {
useValgrind = true;
} else {
printf("usage: %s [options] \n", argv[0]);
printf(" [-c config]: config directory, default is: %s\n", configDir);
printf(" [-f script]: script filename\n");
return 0;
}
}
if (!simSystemInit()) {
simError("failed to initialize the system");
simSystemCleanUp();
return -1;
}
simInfo("simulator is running ...");
taosSetSignal(SIGINT, simHandleSignal);
SScript *script = simParseScript(scriptFile);
if (script == NULL) {
simError("parse script file:%s failed", scriptFile);
return -1;
}
if (abortExecution) {
simError("execute abort");
return -1;
}
simScriptList[++simScriptPos] = script;
simExecuteScript(script);
int32_t ret = simExecSuccess ? 0 : -1;
simInfo("execute result %d", ret);
return ret;
} }

View File

@ -35,11 +35,10 @@ int32_t simInitCfg() {
return 0; return 0;
} }
bool simSystemInit() { void simSystemInit() {
simInitCfg(); simInitCfg();
simInitsimCmdList(); simInitsimCmdList();
memset(simScriptList, 0, sizeof(SScript *) * MAX_MAIN_SCRIPT_NUM); memset(simScriptList, 0, sizeof(SScript *) * MAX_MAIN_SCRIPT_NUM);
return true;
} }
void simSystemCleanUp() {} void simSystemCleanUp() {}

View File

@ -0,0 +1,23 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20)
PROJECT(TDengine)
FIND_PATH(HEADER_GTEST_INCLUDE_DIR gtest.h /usr/include/gtest /usr/local/include/gtest)
FIND_LIBRARY(LIB_GTEST_STATIC_DIR libgtest.a /usr/lib/ /usr/local/lib /usr/lib64)
FIND_LIBRARY(LIB_GTEST_SHARED_DIR libgtest.so /usr/lib/ /usr/local/lib /usr/lib64)
IF(HEADER_GTEST_INCLUDE_DIR AND(LIB_GTEST_STATIC_DIR OR LIB_GTEST_SHARED_DIR))
MESSAGE(STATUS "gTest library found, build os test")
INCLUDE_DIRECTORIES(${HEADER_GTEST_INCLUDE_DIR})
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCE_LIST)
ENDIF()
INCLUDE_DIRECTORIES(${TD_SOURCE_DIR}/src/util/inc)
ADD_EXECUTABLE(simTests "simTests.cpp")
TARGET_LINK_LIBRARIES(simTests os util tsim_static gtest_main)
ADD_TEST(
NAME simTests
COMMAND simTests
)

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#pragma GCC diagnostic ignored "-Wpointer-arith"
#include "simInt.h"
void simHandleSignal(int32_t signo, void *sigInfo, void *context);
TEST(simTests, parameters) {
int32_t ret = 0;
int32_t argc = 2;
char *argv[4] = {0};
simSystemCleanUp();
// argv[1] = "-c";
// ret = simEntry(argc, argv);
// EXPECT_EQ(ret, 0);
// argv[1] = "-f";
// ret = simEntry(argc, argv);
// EXPECT_EQ(ret, 0);
// argv[1] = "-v";
// ret = simEntry(argc, argv);
// EXPECT_EQ(ret, 0);
// argv[1] = "-h";
// ret = simEntry(argc, argv);
// EXPECT_EQ(ret, 0);
// simHandleSignal(0, NULL, NULL);
// simDebugFlag = 0;
// argc = 1;
// ret = simEntry(argc, argv);
// EXPECT_EQ(ret, -1);
}