test: for unittest
This commit is contained in:
parent
1f324fe520
commit
95ef1ca656
|
@ -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);
|
||||||
|
@ -341,12 +341,10 @@ int32_t taosThreadDetach(TdThread thread) {
|
||||||
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,9 +357,7 @@ 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);
|
||||||
|
@ -847,9 +843,7 @@ 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;
|
||||||
|
|
|
@ -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, ¶);
|
||||||
|
taosThreadGetSchedParam(thread, NULL, ¶);
|
||||||
|
taosThreadGetSchedParam(thread, &policy, NULL);
|
||||||
|
taosThreadSetSchedParam(NULL, 0, ¶);
|
||||||
|
taosThreadSetSchedParam(thread, 0, ¶);
|
||||||
|
|
||||||
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, ¶m);
|
taosThreadGetSchedParam(tid2, &policy, ¶);
|
||||||
taosThreadGetSchedParam(tid2, NULL, ¶m);
|
taosThreadGetSchedParam(tid2, NULL, ¶);
|
||||||
taosThreadGetSchedParam(tid2, &policy, NULL);
|
taosThreadGetSchedParam(tid2, &policy, NULL);
|
||||||
|
taosThreadSetSchedParam(NULL, 0, ¶);
|
||||||
|
taosThreadSetSchedParam(tid2, 0, ¶);
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue