Merge pull request #29924 from taosdata/fix/internal

fix: add some unitest for os moudle
This commit is contained in:
Simon Guan 2025-02-25 19:00:12 +08:00 committed by GitHub
commit 18350b918f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 977 additions and 521 deletions

View File

@ -47,8 +47,10 @@ int32_t taosGetProcMemory(int64_t *usedKB);
int32_t taosGetSysMemory(int64_t *usedKB); int32_t taosGetSysMemory(int64_t *usedKB);
int32_t taosGetSysAvailMemory(int64_t *availSize); int32_t taosGetSysAvailMemory(int64_t *availSize);
int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize);
int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes);
int32_t taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes); int32_t taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes);
void taosSetDefaultProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes); void taosSetDefaultProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes);
int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes);
int32_t taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes); int32_t taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes);
void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes); void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes);

View File

@ -87,7 +87,7 @@ void taosRandStr(char* str, int32_t size) {
void taosRandStr2(char* str, int32_t size) { void taosRandStr2(char* str, int32_t size) {
const char* set = "abcdefghijklmnopqrstuvwxyz0123456789@"; const char* set = "abcdefghijklmnopqrstuvwxyz0123456789@";
int32_t len = strlen(set); int32_t len = 37;
for (int32_t i = 0; i < size; ++i) { for (int32_t i = 0; i < size; ++i) {
str[i] = set[taosRand() % len]; str[i] = set[taosRand() % len];

View File

@ -114,6 +114,7 @@ int32_t taosDflSignal(int32_t signum) {
return 0; return 0;
} }
#if 0
int32_t taosKillChildOnParentStopped() { int32_t taosKillChildOnParentStopped() {
#ifndef _TD_DARWIN_64 #ifndef _TD_DARWIN_64
int32_t code = prctl(PR_SET_PDEATHSIG, SIGKILL); int32_t code = prctl(PR_SET_PDEATHSIG, SIGKILL);
@ -126,5 +127,6 @@ int32_t taosKillChildOnParentStopped() {
#endif #endif
return 0; return 0;
} }
#endif
#endif #endif

View File

@ -294,6 +294,7 @@ int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) {
//#endif //#endif
} }
#if 0
int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) { int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) {
if (target_ucs4 == NULL || source_ucs4 == NULL || len_ucs4 <= 0) { if (target_ucs4 == NULL || source_ucs4 == NULL || len_ucs4 <= 0) {
return TSDB_CODE_INVALID_PARA; return TSDB_CODE_INVALID_PARA;
@ -307,11 +308,12 @@ int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4)
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
#endif
iconv_t taosAcquireConv(int32_t *idx, ConvType type, void* charsetCxt) { iconv_t taosAcquireConv(int32_t *idx, ConvType type, void* charsetCxt) {
if(idx == NULL) { if(idx == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return (iconv_t)-1; return (iconv_t)NULL;
} }
if (charsetCxt == NULL){ if (charsetCxt == NULL){
charsetCxt = tsCharsetCxt; charsetCxt = tsCharsetCxt;

View File

@ -147,7 +147,6 @@ static void taosGetProcIOnfos() {
#endif #endif
static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
OS_PARAM_CHECK(cpuInfo);
int32_t code = 0; int32_t code = 0;
#ifdef WINDOWS #ifdef WINDOWS
FILETIME pre_idleTime = {0}; FILETIME pre_idleTime = {0};
@ -200,7 +199,6 @@ static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
} }
static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
OS_PARAM_CHECK(cpuInfo);
int32_t code = 0; int32_t code = 0;
#ifdef WINDOWS #ifdef WINDOWS
@ -541,7 +539,6 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
// Returns the container's CPU quota if successful, otherwise returns the physical CPU cores // Returns the container's CPU quota if successful, otherwise returns the physical CPU cores
static int32_t taosCntrGetCpuCores(float *numOfCores) { static int32_t taosCntrGetCpuCores(float *numOfCores) {
OS_PARAM_CHECK(numOfCores);
#ifdef WINDOWS #ifdef WINDOWS
return TSDB_CODE_UNSUPPORT_OS; return TSDB_CODE_UNSUPPORT_OS;
#elif defined(_TD_DARWIN_64) #elif defined(_TD_DARWIN_64)
@ -1092,6 +1089,7 @@ void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes
if (transmit_bytes) *transmit_bytes = 0; if (transmit_bytes) *transmit_bytes = 0;
} }
#if 0
void taosKillSystem() { void taosKillSystem() {
#ifdef WINDOWS #ifdef WINDOWS
printf("function taosKillSystem, exit!"); printf("function taosKillSystem, exit!");
@ -1105,6 +1103,7 @@ void taosKillSystem() {
(void)kill(tsProcId, 2); (void)kill(tsProcId, 2);
#endif #endif
} }
#endif
#define UUIDLEN (36) #define UUIDLEN (36)
int32_t taosGetSystemUUIDLimit36(char *uid, int32_t uidlen) { int32_t taosGetSystemUUIDLimit36(char *uid, int32_t uidlen) {

View File

@ -15,36 +15,47 @@ ENDIF()
INCLUDE_DIRECTORIES(${TD_SOURCE_DIR}/src/util/inc) INCLUDE_DIRECTORIES(${TD_SOURCE_DIR}/src/util/inc)
if(TD_LINUX) if(TD_LINUX)
add_executable(osAtomicTests "osAtomicTests.cpp") add_executable(osAtomicTests "osAtomicTests.cpp")
target_link_libraries(osAtomicTests os util gtest_main) target_link_libraries(osAtomicTests os util gtest_main)
add_test( add_test(
NAME osAtomicTests NAME osAtomicTests
COMMAND osAtomicTests COMMAND osAtomicTests
) )
endif()
if(TD_LINUX) add_executable(osDirTests "osDirTests.cpp")
add_executable(osDirTests "osDirTests.cpp") target_link_libraries(osDirTests os util gtest_main)
target_link_libraries(osDirTests os util gtest_main) add_test(
add_test( NAME osDirTests
NAME osDirTests COMMAND osDirTests
COMMAND osDirTests )
)
add_executable(osFileTests "osFileTests.cpp")
target_link_libraries(osFileTests os util gtest_main)
add_test(
NAME osFileTests
COMMAND osFileTests
)
endif()
if(TD_LINUX) add_executable(osFileTests "osFileTests.cpp")
add_executable(osEnvTests "osEnvTests.cpp") target_link_libraries(osFileTests os util gtest_main)
target_link_libraries(osEnvTests os util gtest_main) add_test(
add_test( NAME osFileTests
NAME osEnvTests COMMAND osFileTests
COMMAND osEnvTests )
)
add_executable(osEnvTests "osEnvTests.cpp")
target_link_libraries(osEnvTests os util gtest_main)
add_test(
NAME osEnvTests
COMMAND osEnvTests
)
add_executable(osTests "osTests.cpp")
target_link_libraries(osTests os util gtest_main)
add_test(
NAME osTests
COMMAND osTests
)
add_executable(osThreadTests "osThreadTests.cpp")
target_link_libraries(osThreadTests os util gtest_main)
add_test(
NAME osThreadTests
COMMAND osThreadTests
)
endif() endif()
add_executable(osMathTests "osMathTests.cpp") add_executable(osMathTests "osMathTests.cpp")
@ -61,13 +72,6 @@ add_test(
COMMAND osSemaphoreTests COMMAND osSemaphoreTests
) )
add_executable(osSignalTests "osSignalTests.cpp")
target_link_libraries(osSignalTests os util gtest_main)
add_test(
NAME osSignalTests
COMMAND osSignalTests
)
add_executable(osSleepTests "osSleepTests.cpp") add_executable(osSleepTests "osSleepTests.cpp")
target_link_libraries(osSleepTests os util gtest_main) target_link_libraries(osSleepTests os util gtest_main)
add_test( add_test(
@ -82,13 +86,6 @@ add_test(
COMMAND osStringTests COMMAND osStringTests
) )
add_executable(osTests "osTests.cpp")
target_link_libraries(osTests os util gtest_main)
add_test(
NAME osTests
COMMAND osTests
)
add_executable(osSystemTests "osSystemTests.cpp") add_executable(osSystemTests "osSystemTests.cpp")
target_link_libraries(osSystemTests os util gtest_main) target_link_libraries(osSystemTests os util gtest_main)
add_test( add_test(
@ -96,19 +93,9 @@ add_test(
COMMAND osSystemTests COMMAND osSystemTests
) )
if(TD_LINUX)
add_executable(osThreadTests "osThreadTests.cpp")
target_link_libraries(osThreadTests os util gtest_main)
add_test(
NAME osThreadTests
COMMAND osThreadTests
)
endif()
add_executable(osTimeTests "osTimeTests.cpp") add_executable(osTimeTests "osTimeTests.cpp")
target_link_libraries(osTimeTests os util gtest_main) target_link_libraries(osTimeTests os util gtest_main)
add_test( add_test(
NAME osTimeTests NAME osTimeTests
COMMAND osTimeTests COMMAND osTimeTests
) )

View File

@ -1,40 +0,0 @@
/*
* 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 "os.h"
#include "tlog.h"
TEST(osSignalTests, taosSetSignal) {
// Set up SIGINT handler using taosSetSignal
//taosSetSignal(SIGINT, sigint_handler);
// Print PID for testing purposes
// printf("PID: %d\n", getpid());
// Wait for signal to be received
}

File diff suppressed because it is too large Load Diff

View File

@ -14,8 +14,8 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <iostream>
#include <inttypes.h> #include <inttypes.h>
#include <iostream>
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings" #pragma GCC diagnostic ignored "-Wwrite-strings"
@ -36,6 +36,230 @@
#include <arpa/inet.h> #include <arpa/inet.h>
TEST(osTest, locale) {
char *ret = taosCharsetReplace(NULL);
EXPECT_EQ(ret, nullptr);
ret = taosCharsetReplace("utf8");
EXPECT_NE(ret, nullptr);
ret = taosCharsetReplace("utf-8");
EXPECT_NE(ret, nullptr);
taosGetSystemLocale(NULL, "");
taosGetSystemLocale("", NULL);
}
TEST(osTest, memory) {
int32_t ret = taosMemoryDbgInitRestore();
EXPECT_EQ(ret, 0);
int64_t ret64 = taosMemSize(NULL);
EXPECT_EQ(ret64, 0);
}
TEST(osTest, rand2) {
char str[128] = {0};
taosRandStr2(str, 100);
}
TEST(osTest, socket2) {
int32_t ret32 = taosCloseSocket(NULL);
EXPECT_NE(ret32, 0);
ret32 = taosSetSockOpt(NULL, 0, 0, NULL, 0);
EXPECT_NE(ret32, 0);
#if defined(LINUX)
struct in_addr ipInt;
ipInt.s_addr = htonl(0x7F000001);
char buf[128] = {0};
taosInetNtop(ipInt, buf, 32);
#endif
ret32 = taosGetIpv4FromFqdn("localhost", NULL);
EXPECT_NE(ret32, 0);
uint32_t ip = 0;
ret32 = taosGetIpv4FromFqdn(NULL, &ip);
EXPECT_NE(ret32, 0);
taosInetNtoa(NULL, ip);
ret32 = taosInetAddr(NULL);
EXPECT_EQ(ret32, 0);
ret32 = taosWinSocketInit();
EXPECT_EQ(ret32, 0);
}
TEST(osTest, time2) {
taosGetLocalTimezoneOffset();
char buf[12] = {0};
char fmt[12] = {0};
void *retptr = taosStrpTime(NULL, fmt, NULL);
EXPECT_EQ(retptr, nullptr);
retptr = taosStrpTime(buf, NULL, NULL);
EXPECT_EQ(retptr, nullptr);
size_t ret = taosStrfTime(NULL, 0, fmt, NULL);
EXPECT_EQ(ret, 0);
ret = taosStrfTime(buf, 0, NULL, NULL);
EXPECT_EQ(ret, 0);
time_t tp = {0};
struct tm *retptr2 = taosGmTimeR(&tp, NULL);
EXPECT_EQ(retptr2, nullptr);
retptr2 = taosGmTimeR(NULL, NULL);
EXPECT_EQ(retptr2, nullptr);
time_t rett = taosTimeGm(NULL);
EXPECT_EQ(rett, -1);
timezone_t tz = {0};
retptr2 = taosLocalTime(&tp, NULL, NULL, 0, tz);
EXPECT_EQ(retptr2, nullptr);
retptr2 = taosLocalTime(NULL, NULL, NULL, 0, tz);
EXPECT_EQ(retptr2, nullptr);
}
TEST(osTest, system) {
#if defined(LINUX)
taosSetConsoleEcho(false);
taosSetConsoleEcho(true);
taosGetOldTerminalMode();
taosCloseCmd(NULL);
TdCmdPtr ptr = taosOpenCmd(NULL);
EXPECT_EQ(ptr, nullptr);
taosCloseCmd(&ptr);
ptr = taosOpenCmd("echo 'hello world'");
ASSERT_NE(ptr, nullptr);
char buf[256] = {0};
int64_t ret64 = taosGetsCmd(NULL, 0, NULL);
EXPECT_LE(ret64, 0);
ret64 = taosGetsCmd(ptr, 0, NULL);
EXPECT_LE(ret64, 0);
ret64 = taosGetsCmd(ptr, 255, buf);
EXPECT_GT(ret64, 0);
taosCloseCmd(&ptr);
ptr = taosOpenCmd("echoxxx 'hello world'");
ASSERT_NE(ptr, nullptr);
ret64 = taosGetsCmd(ptr, 255, buf);
EXPECT_LE(ret64, 0);
taosCloseCmd(&ptr);
ret64 = taosGetLineCmd(NULL, NULL);
EXPECT_LE(ret64, 0);
ret64 = taosGetLineCmd(ptr, NULL);
EXPECT_LE(ret64, 0);
ptr = taosOpenCmd("echo 'hello world'");
ASSERT_NE(ptr, nullptr);
char *ptrBuf = NULL;
ret64 = taosGetLineCmd(ptr, &ptrBuf);
EXPECT_GE(ret64, 0);
taosCloseCmd(&ptr);
ptr = taosOpenCmd("echoxxx 'hello world'");
ASSERT_NE(ptr, nullptr);
ret64 = taosGetLineCmd(ptr, &ptrBuf);
EXPECT_LE(ret64, 0);
taosCloseCmd(&ptr);
int32_t ret32 = taosEOFCmd(NULL);
EXPECT_EQ(ret32, 0);
#endif
}
TEST(osTest, sysinfo) {
#if defined(LINUX)
int32_t ret32 = 0;
ret32 = taosGetEmail(NULL, 0);
EXPECT_NE(ret32, 0);
ret32 = taosGetOsReleaseName(NULL, NULL, NULL, 0);
EXPECT_NE(ret32, 0);
char buf[128] = {0};
float numOfCores = 0;
ret32 = taosGetCpuInfo(buf, 0, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetCpuInfo(NULL, 0, &numOfCores);
EXPECT_NE(ret32, 0);
ret32 = taosGetCpuCores(NULL, false);
EXPECT_NE(ret32, 0);
ret32 = taosGetTotalMemory(NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcMemory(NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetSysMemory(NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetDiskSize(buf, NULL);
EXPECT_NE(ret32, 0);
SDiskSize disksize = {0};
ret32 = taosGetDiskSize(NULL, &disksize);
EXPECT_NE(ret32, 0);
int64_t tmp = 0;
ret32 = taosGetProcIO(NULL, NULL, NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIO(&tmp, NULL, NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIO(&tmp, &tmp, NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIO(&tmp, &tmp, &tmp, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIODelta(NULL, NULL, NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIODelta(&tmp, NULL, NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIODelta(&tmp, &tmp, NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetProcIODelta(&tmp, &tmp, &tmp, NULL);
EXPECT_NE(ret32, 0);
taosGetProcIODelta(NULL, NULL, NULL, NULL);
taosGetProcIODelta(&tmp, &tmp, &tmp, &tmp);
ret32 = taosGetCardInfo(NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetCardInfo(&tmp, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetCardInfoDelta(NULL, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetCardInfoDelta(&tmp, NULL);
EXPECT_NE(ret32, 0);
taosGetCardInfoDelta(NULL, NULL);
taosGetCardInfoDelta(&tmp, &tmp);
ret32 = taosGetSystemUUIDLimit36(NULL, 0);
EXPECT_NE(ret32, 0);
ret32 = taosGetSystemUUIDLen(NULL, 0);
EXPECT_NE(ret32, 0);
ret32 = taosGetSystemUUIDLen(buf, -1);
EXPECT_NE(ret32, 0);
taosSetCoreDump(false);
ret32 = taosGetlocalhostname(NULL, 0);
EXPECT_NE(ret32, 0);
#endif
}
TEST(osTest, osFQDNSuccess) { TEST(osTest, osFQDNSuccess) {
char fqdn[TD_FQDN_LEN]; char fqdn[TD_FQDN_LEN];
char ipString[INET_ADDRSTRLEN]; char ipString[INET_ADDRSTRLEN];
@ -47,8 +271,8 @@ TEST(osTest, osFQDNSuccess) {
struct in_addr addr; struct in_addr addr;
addr.s_addr = htonl(ipv4); addr.s_addr = htonl(ipv4);
(void)snprintf(ipString, INET_ADDRSTRLEN, "%u.%u.%u.%u", (unsigned int)(addr.s_addr >> 24) & 0xFF, (void)snprintf(ipString, INET_ADDRSTRLEN, "%u.%u.%u.%u", (unsigned int)(addr.s_addr >> 24) & 0xFF,
(unsigned int)(addr.s_addr >> 16) & 0xFF, (unsigned int)(addr.s_addr >> 8) & 0xFF, (unsigned int)(addr.s_addr >> 16) & 0xFF, (unsigned int)(addr.s_addr >> 8) & 0xFF,
(unsigned int)(addr.s_addr) & 0xFF); (unsigned int)(addr.s_addr) & 0xFF);
(void)printf("fqdn:%s ip:%s\n", fqdn, ipString); (void)printf("fqdn:%s ip:%s\n", fqdn, ipString);
} }
@ -56,7 +280,7 @@ TEST(osTest, osFQDNFailed) {
char fqdn[1024] = "fqdn_test_not_found"; char fqdn[1024] = "fqdn_test_not_found";
char ipString[24]; char ipString[24];
uint32_t ipv4 = 0; uint32_t ipv4 = 0;
int32_t code = taosGetIpv4FromFqdn(fqdn, &ipv4); int32_t code = taosGetIpv4FromFqdn(fqdn, &ipv4);
ASSERT_NE(code, 0); ASSERT_NE(code, 0);
terrno = TSDB_CODE_RPC_FQDN_ERROR; terrno = TSDB_CODE_RPC_FQDN_ERROR;
@ -79,7 +303,7 @@ TEST(osTest, osSystem) {
} }
void fileOperateOnFree(void *param) { void fileOperateOnFree(void *param) {
char * fname = (char *)param; char *fname = (char *)param;
TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE); TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE);
(void)printf("On free thread open file\n"); (void)printf("On free thread open file\n");
ASSERT_NE(pFile, nullptr); ASSERT_NE(pFile, nullptr);
@ -101,7 +325,7 @@ void *fileOperateOnFreeThread(void *param) {
return NULL; return NULL;
} }
void fileOperateOnBusy(void *param) { void fileOperateOnBusy(void *param) {
char * fname = (char *)param; char *fname = (char *)param;
TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE); TdFilePtr pFile = taosOpenFile(fname, TD_FILE_CREATE | TD_FILE_WRITE);
(void)printf("On busy thread open file\n"); (void)printf("On busy thread open file\n");
if (pFile == NULL) return; if (pFile == NULL) return;
@ -165,9 +389,9 @@ TEST(osTest, osFile) {
(void)taosThreadJoin(thread2, NULL); (void)taosThreadJoin(thread2, NULL);
taosThreadClear(&thread2); taosThreadClear(&thread2);
//int ret = taosRemoveFile(fname); // int ret = taosRemoveFile(fname);
//ASSERT_EQ(ret, 0); // ASSERT_EQ(ret, 0);
//printf("remove file success"); // printf("remove file success");
} }
#ifndef OSFILE_PERFORMANCE_TEST #ifndef OSFILE_PERFORMANCE_TEST
@ -178,18 +402,109 @@ TEST(osTest, osFile) {
#define TESTTIMES 1000 #define TESTTIMES 1000
char *getRandomWord() { char *getRandomWord() {
static char words[][MAX_WORD_LENGTH] = { static char words[][MAX_WORD_LENGTH] = {"Lorem",
"Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "ipsum",
"sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "dolor",
"aliqua", "Ut", "enim", "ad", "minim", "veniam", "quis", "nostrud", "exercitation", "ullamco", "sit",
"Why", "do", "programmers", "prefer", "using", "dark", "mode?", "Because", "light", "attracts", "amet",
"bugs", "and", "they", "want", "to", "code", "in", "peace,", "like", "a", "ninja", "in", "the", "shadows." "consectetur",
"aliqua", "Ut", "enim", "ad", "minim", "veniam", "quis", "nostrud", "exercitation", "ullamco", "adipiscing",
"laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat", "Duis", "aute", "irure", "elit",
"dolor", "in", "reprehenderit", "in", "voluptate", "velit", "esse", "cillum", "dolore", "eu", "sed",
"fugiat", "nulla", "pariatur", "Excepteur", "sint", "occaecat", "cupidatat", "non", "proident", "do",
"sunt", "in", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum" "eiusmod",
}; "tempor",
"incididunt",
"ut",
"labore",
"et",
"dolore",
"magna",
"aliqua",
"Ut",
"enim",
"ad",
"minim",
"veniam",
"quis",
"nostrud",
"exercitation",
"ullamco",
"Why",
"do",
"programmers",
"prefer",
"using",
"dark",
"mode?",
"Because",
"light",
"attracts",
"bugs",
"and",
"they",
"want",
"to",
"code",
"in",
"peace,",
"like",
"a",
"ninja",
"in",
"the",
"shadows."
"aliqua",
"Ut",
"enim",
"ad",
"minim",
"veniam",
"quis",
"nostrud",
"exercitation",
"ullamco",
"laboris",
"nisi",
"ut",
"aliquip",
"ex",
"ea",
"commodo",
"consequat",
"Duis",
"aute",
"irure",
"dolor",
"in",
"reprehenderit",
"in",
"voluptate",
"velit",
"esse",
"cillum",
"dolore",
"eu",
"fugiat",
"nulla",
"pariatur",
"Excepteur",
"sint",
"occaecat",
"cupidatat",
"non",
"proident",
"sunt",
"in",
"culpa",
"qui",
"officia",
"deserunt",
"mollit",
"anim",
"id",
"est",
"laborum"};
return words[taosRand() % MAX_WORDS]; return words[taosRand() % MAX_WORDS];
} }
@ -197,7 +512,7 @@ char *getRandomWord() {
int64_t fillBufferWithRandomWords(char *buffer, int64_t maxBufferSize) { int64_t fillBufferWithRandomWords(char *buffer, int64_t maxBufferSize) {
int64_t len = 0; int64_t len = 0;
while (len < maxBufferSize) { while (len < maxBufferSize) {
char * word = getRandomWord(); char *word = getRandomWord();
size_t wordLen = strlen(word); size_t wordLen = strlen(word);
if (len + wordLen + 1 < maxBufferSize) { if (len + wordLen + 1 < maxBufferSize) {
@ -246,11 +561,11 @@ TEST(osTest, osFilePerformance) {
int64_t OpenForWriteCloseFileCost; int64_t OpenForWriteCloseFileCost;
int64_t OpenForReadCloseFileCost; int64_t OpenForReadCloseFileCost;
char * buffer; char *buffer;
char * writeBuffer = (char *)taosMemoryCalloc(1, MAX_TEST_FILE_SIZE); char *writeBuffer = (char *)taosMemoryCalloc(1, MAX_TEST_FILE_SIZE);
char * readBuffer = (char *)taosMemoryCalloc(1, MAX_TEST_FILE_SIZE); char *readBuffer = (char *)taosMemoryCalloc(1, MAX_TEST_FILE_SIZE);
int64_t size = fillBufferWithRandomWords(writeBuffer, MAX_TEST_FILE_SIZE); int64_t size = fillBufferWithRandomWords(writeBuffer, MAX_TEST_FILE_SIZE);
char * fname = "./osFilePerformanceTest.txt"; char *fname = "./osFilePerformanceTest.txt";
TdFilePtr pOutFD = taosCreateFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC); TdFilePtr pOutFD = taosCreateFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
ASSERT_NE(pOutFD, nullptr); ASSERT_NE(pOutFD, nullptr);
@ -342,6 +657,6 @@ TEST(osTest, osFilePerformance) {
(void)printf("Test OpenForRead & Close file %d times, cost: %" PRId64 "us\n", TESTTIMES, OpenForReadCloseFileCost); (void)printf("Test OpenForRead & Close file %d times, cost: %" PRId64 "us\n", TESTTIMES, OpenForReadCloseFileCost);
} }
#endif // OSFILE_PERFORMANCE_TEST #endif // OSFILE_PERFORMANCE_TEST
#pragma GCC diagnostic pop #pragma GCC diagnostic pop

View File

@ -110,7 +110,7 @@ TEST(osThreadTests, thread) {
taosMsleep(300); taosMsleep(300);
(void)taosThreadCancel(tid1); (void)taosThreadCancel(tid1);
reti = taosThreadCreate(&tid2, NULL, funcPtr501, NULL); reti = taosThreadCreate(&tid2, NULL, funcPtr501, NULL);
EXPECT_EQ(reti, 0); EXPECT_EQ(reti, 0);
taosMsleep(1000); taosMsleep(1000);
@ -498,4 +498,42 @@ TEST(osThreadTests, spinlock) {
TEST(osThreadTests, others) { TEST(osThreadTests, others) {
taosThreadTestCancel(); taosThreadTestCancel();
taosThreadClear(NULL); taosThreadClear(NULL);
TdThread tid1 = {0};
TdThread tid2 = {0};
TdThread *thread = &tid1;
taosResetPthread(NULL);
taosResetPthread(thread);
bool retb = taosComparePthread(tid1, tid2);
EXPECT_TRUE(retb);
int32_t ret32 = taosGetAppName(NULL, NULL);
EXPECT_NE(ret32, 0);
char name[32] = {0};
int32_t pid;
ret32 = taosGetPIdByName(name, NULL);
EXPECT_NE(ret32, 0);
ret32 = taosGetPIdByName(NULL, &pid);
EXPECT_NE(ret32, 0);
ret32 = tsem_timewait(NULL, 124);
EXPECT_NE(ret32, 0);
ret32 = tsem_wait(NULL);
EXPECT_NE(ret32, 0);
ret32 = tsem2_init(NULL, 0, 0);
EXPECT_NE(ret32, 0);
ret32 = tsem_post(NULL);
EXPECT_NE(ret32, 0);
ret32 = tsem_destroy(NULL);
EXPECT_NE(ret32, 0);
ret32 = tsem2_post(NULL);
EXPECT_NE(ret32, 0);
ret32 = tsem2_destroy(NULL);
EXPECT_NE(ret32, 0);
ret32 = tsem2_wait(NULL);
EXPECT_NE(ret32, 0);
ret32 = tsem2_timewait(NULL, 128);
EXPECT_NE(ret32, 0);
} }