test: for unittest

This commit is contained in:
Shengliang Guan 2024-12-17 15:38:36 +08:00
parent 1f324fe520
commit 95ef1ca656
2 changed files with 203 additions and 45 deletions

View File

@ -28,9 +28,9 @@ int32_t taosThreadCreate(TdThread *tid, const TdThreadAttr *attr, void *(*start)
return code; return code;
} }
int32_t taosThreadAttrDestroy(TdThreadAttr *attr) { int32_t taosThreadAttrDestroy(TdThreadAttr *attr) {
OS_PARAM_CHECK(attr); OS_PARAM_CHECK(attr);
int32_t code = pthread_attr_destroy(attr); int32_t code = pthread_attr_destroy(attr);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -99,7 +99,7 @@ int32_t taosThreadAttrGetStackSize(const TdThreadAttr *attr, size_t *stacksize)
int32_t taosThreadAttrInit(TdThreadAttr *attr) { int32_t taosThreadAttrInit(TdThreadAttr *attr) {
OS_PARAM_CHECK(attr); OS_PARAM_CHECK(attr);
int32_t code = pthread_attr_init(attr); int32_t code = pthread_attr_init(attr);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -160,8 +160,8 @@ int32_t taosThreadAttrSetStackSize(TdThreadAttr *attr, size_t stacksize) {
return code; return code;
} }
int32_t taosThreadCancel(TdThread thread) { int32_t taosThreadCancel(TdThread thread) {
int32_t code = pthread_cancel(thread); int32_t code = pthread_cancel(thread);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -253,7 +253,7 @@ int32_t taosThreadCondTimedWait(TdThreadCond *cond, TdThreadMutex *mutex, const
return TAOS_SYSTEM_WINAPI_ERROR(error); return TAOS_SYSTEM_WINAPI_ERROR(error);
#else #else
int32_t code = pthread_cond_timedwait(cond, mutex, abstime); int32_t code = pthread_cond_timedwait(cond, mutex, abstime);
if(code == ETIMEDOUT) { if (code == ETIMEDOUT) {
return TSDB_CODE_TIMEOUT_ERROR; return TSDB_CODE_TIMEOUT_ERROR;
} else if (code) { } else if (code) {
return TAOS_SYSTEM_ERROR(code); return TAOS_SYSTEM_ERROR(code);
@ -333,20 +333,18 @@ int32_t taosThreadCondAttrSetPshared(TdThreadCondAttr *attr, int32_t pshared) {
#endif #endif
} }
int32_t taosThreadDetach(TdThread thread) { int32_t taosThreadDetach(TdThread thread) {
int32_t code = pthread_detach(thread); int32_t code = pthread_detach(thread);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
return code; return code;
} }
int32_t taosThreadEqual(TdThread t1, TdThread t2) { int32_t taosThreadEqual(TdThread t1, TdThread t2) { return pthread_equal(t1, t2); }
return pthread_equal(t1, t2);
}
void taosThreadExit(void *valuePtr) { void taosThreadExit(void *valuePtr) {
if(valuePtr) return pthread_exit(valuePtr); if (valuePtr) return pthread_exit(valuePtr);
} }
int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) { int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_param *param) {
@ -359,12 +357,10 @@ int32_t taosThreadGetSchedParam(TdThread thread, int32_t *policy, struct sched_p
return code; return code;
} }
void *taosThreadGetSpecific(TdThreadKey key) { void *taosThreadGetSpecific(TdThreadKey key) { return pthread_getspecific(key); }
return pthread_getspecific(key);
}
int32_t taosThreadJoin(TdThread thread, void **valuePtr) { int32_t taosThreadJoin(TdThread thread, void **valuePtr) {
int32_t code = pthread_join(thread, valuePtr); int32_t code = pthread_join(thread, valuePtr);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -380,16 +376,16 @@ int32_t taosThreadKeyCreate(TdThreadKey *key, void (*destructor)(void *)) {
return code; return code;
} }
int32_t taosThreadKeyDelete(TdThreadKey key) { int32_t taosThreadKeyDelete(TdThreadKey key) {
int32_t code = pthread_key_delete(key); int32_t code = pthread_key_delete(key);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
return code; return code;
} }
int32_t taosThreadKill(TdThread thread, int32_t sig) { int32_t taosThreadKill(TdThread thread, int32_t sig) {
int32_t code = pthread_kill(thread, sig); int32_t code = pthread_kill(thread, sig);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -584,7 +580,7 @@ int32_t taosThreadRwlockDestroy(TdThreadRwlock *rwlock) {
*/ */
return 0; return 0;
#else #else
OS_PARAM_CHECK(rwlock); OS_PARAM_CHECK(rwlock);
int32_t code = pthread_rwlock_destroy(rwlock); int32_t code = pthread_rwlock_destroy(rwlock);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
@ -747,16 +743,16 @@ int32_t taosThreadRwlockAttrSetPshared(TdThreadRwlockAttr *attr, int32_t pshared
TdThread taosThreadSelf(void) { return pthread_self(); } TdThread taosThreadSelf(void) { return pthread_self(); }
int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) { int32_t taosThreadSetCancelState(int32_t state, int32_t *oldstate) {
int32_t code = pthread_setcancelstate(state, oldstate); int32_t code = pthread_setcancelstate(state, oldstate);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
return code; return code;
} }
int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) { int32_t taosThreadSetCancelType(int32_t type, int32_t *oldtype) {
int32_t code = pthread_setcanceltype(type, oldtype); int32_t code = pthread_setcanceltype(type, oldtype);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -772,9 +768,9 @@ int32_t taosThreadSetSchedParam(TdThread thread, int32_t policy, const struct sc
return code; return code;
} }
int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) { int32_t taosThreadSetSpecific(TdThreadKey key, const void *value) {
OS_PARAM_CHECK(value); OS_PARAM_CHECK(value);
int32_t code = pthread_setspecific(key, value); int32_t code = pthread_setspecific(key, value);
if (code) { if (code) {
return (terrno = TAOS_SYSTEM_ERROR(code)); return (terrno = TAOS_SYSTEM_ERROR(code));
} }
@ -847,13 +843,11 @@ int32_t taosThreadSpinUnlock(TdThreadSpinlock *lock) {
#endif #endif
} }
void taosThreadTestCancel(void) { void taosThreadTestCancel(void) { return pthread_testcancel(); }
return pthread_testcancel();
}
void taosThreadClear(TdThread *thread) { void taosThreadClear(TdThread *thread) {
if (!thread) return; if (!thread) return;
(void)memset(thread, 0, sizeof(TdThread)); (void)memset(thread, 0, sizeof(TdThread));
} }
#ifdef WINDOWS #ifdef WINDOWS

View File

@ -31,13 +31,40 @@
static int32_t globalVar = 0; static int32_t globalVar = 0;
static void *funcPtr100(void *param) { static void funcPtrKey(void *param) { taosMsleep(100); }
taosMsleep(100);
static void *funcPtr200(void *param) {
taosMsleep(200);
TdThread thread = taosThreadSelf();
TdThreadKey key = {0};
taosThreadKeyCreate(&key, funcPtrKey);
void *oldVal = taosThreadGetSpecific(key);
taosThreadSetSpecific(key, oldVal);
taosThreadKeyDelete(key);
int32_t oldType = 0;
taosThreadSetCancelType(-1, &oldType);
taosThreadSetCancelType(0, &oldType);
int32_t oldState = 0;
taosThreadSetCancelState(-1, &oldState);
taosThreadSetCancelState(0, &oldState);
int32_t policy;
struct sched_param para;
taosThreadGetSchedParam(thread, &policy, &para);
taosThreadGetSchedParam(thread, NULL, &para);
taosThreadGetSchedParam(thread, &policy, NULL);
taosThreadSetSchedParam(NULL, 0, &para);
taosThreadSetSchedParam(thread, 0, &para);
return NULL; return NULL;
} }
static void *funcPtr500(void *param) { static void *funcPtr500(void *param) {
taosMsleep(500); taosMsleep(500);
TdThread thread = taosThreadSelf();
return NULL; return NULL;
} }
@ -51,18 +78,16 @@ static void *funcPtrExit2(void *param) {
return NULL; return NULL;
} }
static void funcPtrKey(void *param) { taosMsleep(100); }
TEST(osThreadTests, thread) { TEST(osThreadTests, thread) {
TdThread tid1 = {0}; TdThread tid1 = {0};
TdThread tid2 = {0}; TdThread tid2 = {0};
int32_t reti = 0; int32_t reti = 0;
reti = taosThreadCreate(NULL, NULL, funcPtr100, NULL); reti = taosThreadCreate(NULL, NULL, funcPtr200, NULL);
EXPECT_NE(reti, 0); EXPECT_NE(reti, 0);
reti = taosThreadCreate(&tid1, NULL, NULL, NULL); reti = taosThreadCreate(&tid1, NULL, NULL, NULL);
EXPECT_NE(reti, 0); EXPECT_NE(reti, 0);
reti = taosThreadCreate(&tid1, NULL, funcPtr100, NULL); reti = taosThreadCreate(&tid1, NULL, funcPtr200, NULL);
EXPECT_EQ(reti, 0); EXPECT_EQ(reti, 0);
reti = taosThreadCancel(tid1); reti = taosThreadCancel(tid1);
@ -91,14 +116,17 @@ TEST(osThreadTests, thread) {
taosThreadKill(tid2, SIGINT); taosThreadKill(tid2, SIGINT);
int32_t policy; int32_t policy;
struct sched_param param; struct sched_param para;
taosThreadGetSchedParam(tid2, &policy, &param); taosThreadGetSchedParam(tid2, &policy, &para);
taosThreadGetSchedParam(tid2, NULL, &param); taosThreadGetSchedParam(tid2, NULL, &para);
taosThreadGetSchedParam(tid2, &policy, NULL); taosThreadGetSchedParam(tid2, &policy, NULL);
taosThreadSetSchedParam(NULL, 0, &para);
taosThreadSetSchedParam(tid2, 0, &para);
TdThreadKey key; TdThreadKey key = {0};
taosThreadKeyCreate(&key, funcPtrKey); taosThreadKeyCreate(&key, funcPtrKey);
taosThreadGetSpecific(key); void *oldVal = taosThreadGetSpecific(key);
taosThreadSetSpecific(key, oldVal);
taosThreadKeyDelete(key); taosThreadKeyDelete(key);
} }
@ -247,6 +275,142 @@ TEST(osThreadTests, cond) {
EXPECT_EQ(reti, 0); EXPECT_EQ(reti, 0);
} }
TEST(osThreadTests, mutex) {
int32_t reti = 0;
TdThreadMutex mutex;
reti = taosThreadMutexInit(NULL, 0);
EXPECT_NE(reti, 0);
reti = taosThreadMutexInit(&mutex, 0);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexTryLock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexTryLock(&mutex);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexTryLock(&mutex);
EXPECT_NE(reti, 0);
reti = taosThreadMutexUnlock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexUnlock(&mutex);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexLock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexLock(&mutex);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexUnlock(&mutex);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexDestroy(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexDestroy(&mutex);
EXPECT_EQ(reti, 0);
}
TEST(osThreadTests, mutexAttr) {
int32_t reti = 0;
TdThreadMutexAttr mutexAttr;
reti = taosThreadMutexAttrInit(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrInit(&mutexAttr);
EXPECT_EQ(reti, 0);
int32_t pshared;
reti = taosThreadMutexAttrGetPshared(&mutexAttr, &pshared);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexAttrSetPshared(&mutexAttr, pshared);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexAttrSetPshared(&mutexAttr, -1);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrSetPshared(NULL, pshared);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrGetPshared(NULL, &pshared);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrGetPshared(&mutexAttr, NULL);
EXPECT_NE(reti, 0);
int32_t kind;
reti = taosThreadMutexAttrGetType(&mutexAttr, &kind);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexAttrSetType(&mutexAttr, kind);
EXPECT_EQ(reti, 0);
reti = taosThreadMutexAttrSetType(&mutexAttr, -1);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrSetType(NULL, kind);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrGetType(NULL, &kind);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrGetType(&mutexAttr, NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrDestroy(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadMutexAttrDestroy(&mutexAttr);
EXPECT_EQ(reti, 0);
}
TEST(osThreadTests, rwlock) {
int32_t reti = 0;
TdThreadRwlock rwlock;
reti = taosThreadRwlockInit(NULL, 0);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockInit(&rwlock, 0);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockTryRdlock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockTryRdlock(&rwlock);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockTryRdlock(&rwlock);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockUnlock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockUnlock(&rwlock);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockRdlock(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockRdlock(&rwlock);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockUnlock(&rwlock);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockDestroy(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockDestroy(&rwlock);
EXPECT_EQ(reti, 0);
}
TEST(osThreadTests, rdlockAttr) {
int32_t reti = 0;
TdThreadRwlockAttr rdlockAttr;
reti = taosThreadRwlockAttrInit(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockAttrInit(&rdlockAttr);
EXPECT_EQ(reti, 0);
int32_t pshared;
reti = taosThreadRwlockAttrGetPshared(&rdlockAttr, &pshared);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockAttrSetPshared(&rdlockAttr, pshared);
EXPECT_EQ(reti, 0);
reti = taosThreadRwlockAttrSetPshared(&rdlockAttr, -1);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockAttrSetPshared(NULL, pshared);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockAttrGetPshared(NULL, &pshared);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockAttrGetPshared(&rdlockAttr, NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockAttrDestroy(NULL);
EXPECT_NE(reti, 0);
reti = taosThreadRwlockAttrDestroy(&rdlockAttr);
EXPECT_EQ(reti, 0);
}
TEST(osThreadTests, spinlock) { TEST(osThreadTests, spinlock) {
int32_t reti = 0; int32_t reti = 0;