mirror of
https://github.com/taosdata/TDengine
synced 2026-05-24 10:09:01 +00:00
357 lines
8.5 KiB
C++
357 lines
8.5 KiB
C++
/*
|
|
* Copyright (c) 2019 TAOS Data, Inc. <xsren@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 <thread>
|
|
#include "os.h"
|
|
#include "tlog.h"
|
|
|
|
#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"
|
|
|
|
TEST(osSemaphoreTests, InitAndDestroy) {
|
|
tsem_t sem;
|
|
int result = tsem_init(&sem, 0, 1);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
result = tsem_destroy(&sem);
|
|
EXPECT_EQ(result, 0);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Destroy) {
|
|
tsem_t sem;
|
|
int result = tsem_init(&sem, 0, 1);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
result = tsem_destroy(&sem);
|
|
EXPECT_EQ(result, 0);
|
|
// result = tsem_destroy(&sem);
|
|
// EXPECT_NE(result, 0); // result == 0 if on mac
|
|
}
|
|
|
|
// skip, tsem_wait can not stopped, will block test.
|
|
// TEST(osSemaphoreTests, Wait) {
|
|
// tsem_t sem;
|
|
// tsem_init(&sem, 0, 0);
|
|
// ASSERT_EQ(tsem_wait(&sem), -1);
|
|
// tsem_destroy(&sem);
|
|
// }
|
|
|
|
TEST(osSemaphoreTests, WaitTime0) {
|
|
tsem_t sem;
|
|
(void)tsem_init(&sem, 0, 0);
|
|
EXPECT_EQ(tsem_timewait(&sem, 1000), TSDB_CODE_TIMEOUT_ERROR);
|
|
(void)tsem_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, WaitTime1) {
|
|
tsem_t sem;
|
|
(void)tsem_init(&sem, 0, 1);
|
|
EXPECT_EQ(tsem_timewait(&sem, 10000), 0);
|
|
EXPECT_EQ(tsem_timewait(&sem, 1000), TSDB_CODE_TIMEOUT_ERROR);
|
|
(void)tsem_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, WaitTime2) {
|
|
tsem2_t sem;
|
|
(void)tsem2_init(&sem, 0, 1);
|
|
EXPECT_EQ(tsem2_timewait(&sem, 10000), 0);
|
|
EXPECT_EQ(tsem2_timewait(&sem, 1000), TSDB_CODE_TIMEOUT_ERROR);
|
|
(void)tsem2_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, WaitAndPost) {
|
|
tsem_t sem;
|
|
int result = tsem_init(&sem, 0, 0);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
std::thread waiter([&sem]() {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
(void)tsem_post(&sem);
|
|
});
|
|
|
|
result = tsem_wait(&sem);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
waiter.join();
|
|
|
|
result = tsem_destroy(&sem);
|
|
EXPECT_EQ(result, 0);
|
|
}
|
|
|
|
|
|
TEST(osSemaphoreTests, TimedWait) {
|
|
tsem_t sem;
|
|
int result = tsem_init(&sem, 0, 0);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
std::thread twait([&sem]() {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
(void)tsem_post(&sem);
|
|
});
|
|
|
|
// tsem_timewait uses CLOCK_REALTIME, which may produce spurious
|
|
// ETIMEDOUT in containers/VMs due to wall-clock adjustments.
|
|
result = tsem_timewait(&sem, 1000);
|
|
EXPECT_TRUE(result == 0 || result == TSDB_CODE_TIMEOUT_ERROR);
|
|
|
|
twait.join();
|
|
|
|
result = tsem_destroy(&sem);
|
|
EXPECT_EQ(result, 0);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance1_1) {
|
|
tsem_t sem;
|
|
const int count = 100000;
|
|
|
|
(void)tsem_init(&sem, 0, 0);
|
|
std::thread producer([&sem, count]() {
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem_post(&sem);
|
|
}
|
|
});
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem_wait(&sem);
|
|
}
|
|
producer.join();
|
|
(void)tsem_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance1_2) {
|
|
tsem2_t sem;
|
|
const int count = 100000;
|
|
|
|
(void)tsem2_init(&sem, 0, 0);
|
|
std::thread producer2([&sem, count]() {
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem2_post(&sem);
|
|
}
|
|
});
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem2_wait(&sem);
|
|
}
|
|
producer2.join();
|
|
(void)tsem2_destroy(&sem);
|
|
}
|
|
|
|
// Test pthread helper functions
|
|
TEST(osSemaphoreTests, PthreadHelpers) {
|
|
TdThread thread1, thread2;
|
|
|
|
// Test taosGetSelfPthreadId
|
|
int64_t selfId = taosGetSelfPthreadId();
|
|
EXPECT_GT(selfId, 0);
|
|
|
|
// Test taosGetPthreadId
|
|
thread1 = taosThreadSelf();
|
|
int64_t threadId = taosGetPthreadId(thread1);
|
|
EXPECT_GT(threadId, 0);
|
|
|
|
// Test taosResetPthread
|
|
thread1 = taosThreadSelf();
|
|
EXPECT_TRUE(taosCheckPthreadValid(thread1));
|
|
taosResetPthread(&thread1);
|
|
EXPECT_FALSE(taosCheckPthreadValid(thread1));
|
|
|
|
// Test taosComparePthread
|
|
thread1 = taosThreadSelf();
|
|
thread2 = taosThreadSelf();
|
|
EXPECT_TRUE(taosComparePthread(thread1, thread2));
|
|
|
|
taosResetPthread(&thread2);
|
|
EXPECT_FALSE(taosComparePthread(thread1, thread2));
|
|
}
|
|
|
|
// Test taosGetPIdByName
|
|
TEST(osSemaphoreTests, GetPIdByName) {
|
|
int32_t pid = -1;
|
|
|
|
// Try to get PID of current process by name
|
|
char appName[256] = {0};
|
|
int32_t len = 0;
|
|
int32_t ret = taosGetAppName(appName, &len);
|
|
EXPECT_EQ(ret, 0);
|
|
EXPECT_GT(len, 0);
|
|
|
|
// Try to find PID by app name
|
|
ret = taosGetPIdByName(appName, &pid);
|
|
// On Linux, this should succeed; on other platforms it returns -1
|
|
if (ret == 0) {
|
|
EXPECT_GT(pid, 0);
|
|
}
|
|
|
|
// Test with non-existent process name
|
|
ret = taosGetPIdByName("nonexistent_process_12345xyz", &pid);
|
|
EXPECT_NE(ret, 0);
|
|
}
|
|
|
|
// Test tsem error handling with invalid semaphore
|
|
TEST(osSemaphoreTests, InvalidSemErrorHandling) {
|
|
// Test operations on uninitialized semaphore (intentionally commented out as they may crash)
|
|
// Instead, test valid error cases
|
|
|
|
// Test double destroy is platform-dependent, so we skip it
|
|
// Just verify that basic operations work correctly
|
|
tsem_t sem;
|
|
int32_t result = tsem_init(&sem, 0, 1);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
result = tsem_destroy(&sem);
|
|
EXPECT_EQ(result, 0);
|
|
}
|
|
|
|
// Test tsem2 with multiple threads
|
|
TEST(osSemaphoreTests, Tsem2MultiThreaded) {
|
|
tsem2_t sem;
|
|
int32_t result = tsem2_init(&sem, 0, 0);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
std::thread producer([&sem]() {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
tsem2_post(&sem);
|
|
});
|
|
|
|
result = tsem2_timewait(&sem, 5000);
|
|
EXPECT_EQ(result, 0);
|
|
|
|
producer.join();
|
|
tsem2_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance2_1) {
|
|
tsem_t sem;
|
|
const int count = 50000;
|
|
|
|
(void)tsem_init(&sem, 0, 0);
|
|
std::thread p1([&sem, count]() {
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem_post(&sem);
|
|
}
|
|
});
|
|
std::thread p2([&sem, count]() {
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem_post(&sem);
|
|
}
|
|
});
|
|
|
|
for (int i = 0; i < count * 2; ++i) {
|
|
(void)tsem_wait(&sem);
|
|
}
|
|
p1.join();
|
|
p2.join();
|
|
(void)tsem_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance2_2) {
|
|
tsem2_t sem;
|
|
const int count = 50000;
|
|
|
|
(void)tsem2_init(&sem, 0, 0);
|
|
std::thread q1([&sem, count]() {
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem2_post(&sem);
|
|
}
|
|
});
|
|
std::thread q2([&sem, count]() {
|
|
for (int i = 0; i < count; ++i) {
|
|
(void)tsem2_post(&sem);
|
|
}
|
|
});
|
|
|
|
for (int i = 0; i < count * 2; ++i) {
|
|
(void)tsem2_wait(&sem);
|
|
}
|
|
q1.join();
|
|
q2.join();
|
|
(void)tsem2_destroy(&sem);
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance3_1) {
|
|
const int count = 100000;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
tsem_t sem;
|
|
(void)tsem_init(&sem, 0, 1);
|
|
EXPECT_EQ(tsem_timewait(&sem, 1000), 0);
|
|
(void)tsem_destroy(&sem);
|
|
}
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance3_2) {
|
|
const int count = 100000;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
tsem2_t sem;
|
|
(void)tsem2_init(&sem, 0, 1);
|
|
EXPECT_EQ(tsem2_timewait(&sem, 1000), 0);
|
|
(void)tsem2_destroy(&sem);
|
|
}
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance4_1) {
|
|
const int count = 1000;
|
|
for (int i = 0; i < count; ++i) {
|
|
tsem_t sem;
|
|
(void)tsem_init(&sem, 0, 0);
|
|
std::thread p([&sem]() {
|
|
(void)tsem_post(&sem);
|
|
});
|
|
|
|
// tsem_timewait uses CLOCK_REALTIME, which may produce spurious
|
|
// ETIMEDOUT in containers/VMs due to wall-clock adjustments.
|
|
int32_t ret = tsem_timewait(&sem, 10000);
|
|
EXPECT_TRUE(ret == 0 || ret == TSDB_CODE_TIMEOUT_ERROR);
|
|
|
|
p.join();
|
|
|
|
(void)tsem_destroy(&sem);
|
|
}
|
|
}
|
|
|
|
TEST(osSemaphoreTests, Performance4_2) {
|
|
const int count = 1000;
|
|
for (int i = 0; i < count; ++i) {
|
|
tsem2_t sem;
|
|
(void)tsem2_init(&sem, 0, 0);
|
|
std::thread p2([&sem]() {
|
|
(void)tsem2_post(&sem);
|
|
});
|
|
|
|
EXPECT_EQ(tsem2_timewait(&sem, 10000), 0);
|
|
|
|
p2.join();
|
|
|
|
(void)tsem2_destroy(&sem);
|
|
}
|
|
}
|
|
|
|
TEST(osSemaphoreTests, GetPID) {
|
|
#ifdef LINUX
|
|
pid_t pid = 0;
|
|
int32_t ret = taosGetPIdByName("osSemaphoreTest", &pid);
|
|
EXPECT_EQ(ret, 0);
|
|
EXPECT_EQ(pid, taosGetPId());
|
|
#endif
|
|
}
|