Skip to content

Commit c0a21c4

Browse files
authored
Optimize code, refactor, add tests, and improve test coverage to 85% (#5787)
1 parent 039f9a5 commit c0a21c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1311
-790
lines changed

.github/workflows/iouring.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
php: [ '8.1', '8.2', '8.3', '8.4', '8.1-zts', '8.2-zts', '8.3-zts', '8.4-zts' ]
11+
php: [ '8.1', '8.2', '8.3', '8.4' ]
1212
os: [ ubuntu-24.04, ubuntu-24.04-arm ]
1313
name: ${{ matrix.php }}-${{ matrix.os }}-iouring
1414
runs-on: ${{ matrix.os }}

.github/workflows/pecl-package.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ project(libswoole)
44
enable_language(ASM)
55
set(SWOOLE_VERSION 6.1.0-dev)
66

7-
set(CMAKE_CXX_STANDARD 11)
8-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -g")
99
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
1010

1111
file(READ ./config.h SWOOLE_CONFIG_FILE)
@@ -192,6 +192,27 @@ set_target_properties(ext-swoole PROPERTIES PREFIX "")
192192
set_target_properties(ext-swoole PROPERTIES OUTPUT_NAME "swoole")
193193
add_dependencies(ext-swoole lib-swoole)
194194

195+
# core-tests
196+
pkg_check_modules(NGHTTP2 REQUIRED libnghttp2)
197+
if (${NGHTTP2_FOUND})
198+
message(STATUS "Found nghttp2")
199+
else()
200+
message(STATUS "Not found nghttp2")
201+
endif()
202+
203+
# find GTest
204+
find_package(GTest REQUIRED)
205+
if (!${GTEST_FOUND})
206+
message(FATAL_ERROR "Not found GTest")
207+
endif()
208+
message(STATUS "Found GTest")
209+
210+
file(GLOB_RECURSE core_test_files core-tests/src/*.cpp core-tests/deps/llhttp/src/*.c)
211+
add_executable(core-tests ${core_test_files})
212+
add_dependencies(core-tests lib-swoole)
213+
include_directories(BEFORE core-tests/include thirdparty thirdparty/hiredis core-tests/deps/llhttp/include ${GTEST_INCLUDE_DIRS} ${NGHTTP2_INCLUDE_DIR})
214+
target_link_libraries(core-tests swoole pthread ssl crypto ${GTEST_BOTH_LIBRARIES} ${NGHTTP2_LIBRARIES})
215+
195216
if (DEFINED enable_thread)
196217
add_definitions(-DSW_THREAD)
197218
endif()

core-tests/fuzz/cases/req1.bin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
POST / HTTP/1.1
1+
POST /index.html?test=hello&str=world HTTP/1.1
22
Host: 127.0.0.1:35347
33
Accept: */*
44
Content-Length: 11199

core-tests/include/test_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ void counter_set(int index, int value);
103103
void counter_incr_and_put_log(int index, const char *msg);
104104

105105
int dump_cert_info(const char *data, size_t len);
106+
int recursive_rmdir(const char *path);
106107

107108
static inline int dump_cert_info(const String *str) {
108109
return dump_cert_info(str->str, str->length);

core-tests/src/core/base.cpp

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -194,111 +194,6 @@ TEST(base, dirname) {
194194
ASSERT_EQ(dirname("/root"), "/");
195195
ASSERT_EQ(dirname("/"), "/");
196196
}
197-
/**
198-
* 检查目录是否为空
199-
* @param path 目录路径
200-
* @return 如果目录为空返回1,否则返回0
201-
*/
202-
int is_directory_empty(const char *path) {
203-
DIR *dir = opendir(path);
204-
if (dir == NULL) {
205-
perror("opendir");
206-
return 0;
207-
}
208-
209-
int is_empty = 1;
210-
struct dirent *entry;
211-
212-
while ((entry = readdir(dir)) != NULL) {
213-
// 跳过 "." 和 ".." 目录
214-
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
215-
is_empty = 0;
216-
break;
217-
}
218-
}
219-
220-
closedir(dir);
221-
return is_empty;
222-
}
223-
224-
/**
225-
* 检查路径是否为目录
226-
* @param path 路径
227-
* @return 如果是目录返回1,否则返回0
228-
*/
229-
int is_directory(const char *path) {
230-
struct stat path_stat;
231-
if (stat(path, &path_stat) != 0) {
232-
return 0;
233-
}
234-
return S_ISDIR(path_stat.st_mode);
235-
}
236-
237-
/**
238-
* 获取父目录路径
239-
* @param path 当前路径
240-
* @param parent_path 用于存储父目录路径的缓冲区
241-
* @param size 缓冲区大小
242-
* @return 成功返回1,失败返回0
243-
*/
244-
int get_parent_directory(const char *path, char *parent_path, size_t size) {
245-
auto last_slash = strrchr(path, '/');
246-
if (last_slash == NULL || last_slash == path) {
247-
// 没有斜杠或者斜杠是第一个字符(根目录)
248-
return 0;
249-
}
250-
251-
size_t parent_length = last_slash - path;
252-
if (parent_length >= size) {
253-
return 0;
254-
}
255-
256-
strncpy(parent_path, path, parent_length);
257-
parent_path[parent_length] = '\0';
258-
259-
// 处理路径只有一个斜杠的情况
260-
if (parent_length == 0) {
261-
parent_path[0] = '/';
262-
parent_path[1] = '\0';
263-
}
264-
265-
return 1;
266-
}
267-
268-
/**
269-
* 递归删除空目录
270-
* @param path 要删除的目录路径
271-
* @return 成功删除的目录数量
272-
*/
273-
int recursive_rmdir(const char *path) {
274-
// 检查路径是否存在且是目录
275-
if (!is_directory(path)) {
276-
return 0;
277-
}
278-
279-
// 检查目录是否为空
280-
if (!is_directory_empty(path)) {
281-
return 0;
282-
}
283-
284-
int deleted_count = 0;
285-
286-
// 删除当前空目录
287-
if (rmdir(path) == 0) {
288-
deleted_count++;
289-
290-
// 获取父目录
291-
char parent_path[PATH_MAX];
292-
if (get_parent_directory(path, parent_path, PATH_MAX)) {
293-
// 如果父目录存在且不是当前目录,则尝试删除父目录
294-
if (strcmp(parent_path, path) != 0) {
295-
deleted_count += recursive_rmdir(parent_path);
296-
}
297-
}
298-
}
299-
300-
return deleted_count;
301-
}
302197

303198
TEST(base, mkdir_recursive) {
304199
String dir(PATH_MAX + 2);
@@ -343,7 +238,7 @@ TEST(base, set_task_tmpdir) {
343238
std::string dir(buf2);
344239
ASSERT_FALSE(swoole_set_task_tmpdir(dir));
345240

346-
recursive_rmdir(dir.c_str());
241+
test::recursive_rmdir(dir.c_str());
347242
}
348243

349244
TEST(base, version) {

core-tests/src/coroutine/iouring.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,33 @@
1717
#include <sys/file.h>
1818
#include <sys/stat.h>
1919
#include "test_coroutine.h"
20+
#include "swoole_iouring.h"
2021
#include "swoole_coroutine_c_api.h"
2122

2223
#ifdef SW_USE_IOURING
24+
using swoole::Iouring;
2325
using swoole::Reactor;
2426
using swoole::test::coroutine;
2527

26-
TEST(coroutine_iouring, open_and_close) {
28+
TEST(iouring, create) {
29+
coroutine::run([](void *arg) {
30+
SwooleG.iouring_entries = 4;
31+
SwooleG.iouring_workers = 65536;
32+
auto fd = Iouring::open(TEST_TMP_FILE, O_CREAT, 0666);
33+
ASSERT_GE(fd, 0);
34+
ASSERT_NE(Iouring::close(fd), -1);
35+
});
36+
}
37+
38+
TEST(iouring, list_all_opcode) {
39+
auto list = Iouring::list_all_opcode();
40+
for (auto &item : list) {
41+
DEBUG() << "opcode: " << item.first << ", value: " << item.second << "\n";
42+
}
43+
ASSERT_TRUE(list.size() > 0);
44+
}
45+
46+
TEST(iouring, open_and_close) {
2747
coroutine::run([](void *arg) {
2848
const char *test_file = "/tmp/file_1";
2949
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT, 0666);
@@ -37,7 +57,7 @@ TEST(coroutine_iouring, open_and_close) {
3757
});
3858
}
3959

40-
TEST(coroutine_iouring, mkdir_and_rmdir) {
60+
TEST(iouring, mkdir_and_rmdir) {
4161
coroutine::run([](void *arg) {
4262
const char *directory = "/tmp/aaaa";
4363
int result = swoole_coroutine_iouring_mkdir(directory, 0755);
@@ -48,7 +68,7 @@ TEST(coroutine_iouring, mkdir_and_rmdir) {
4868
});
4969
}
5070

51-
TEST(coroutine_iouring, write_and_read) {
71+
TEST(iouring, write_and_read) {
5272
coroutine::run([](void *arg) {
5373
const char *test_file = "/tmp/file_2";
5474
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT | O_RDWR, 0666);
@@ -77,7 +97,7 @@ TEST(coroutine_iouring, write_and_read) {
7797
});
7898
}
7999

80-
TEST(coroutine_iouring, rename) {
100+
TEST(iouring, rename) {
81101
coroutine::run([](void *arg) {
82102
const char *oldpath = "/tmp/file_2";
83103
const char *newpath = "/tmp/file_3";
@@ -95,7 +115,7 @@ TEST(coroutine_iouring, rename) {
95115
});
96116
}
97117

98-
TEST(coroutine_iouring, fstat_and_stat) {
118+
TEST(iouring, fstat_and_stat) {
99119
coroutine::run([](void *arg) {
100120
struct stat statbuf {};
101121
int fd = swoole_coroutine_iouring_open(TEST_TMP_FILE, O_RDWR, 0666);
@@ -114,7 +134,7 @@ TEST(coroutine_iouring, fstat_and_stat) {
114134
});
115135
}
116136

117-
TEST(coroutine_iouring, fsync_and_fdatasync) {
137+
TEST(iouring, fsync_and_fdatasync) {
118138
coroutine::run([](void *arg) {
119139
const char *test_file = "/tmp/file_2";
120140
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT | O_RDWR, 0666);

core-tests/src/coroutine/system.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,32 @@ TEST(coroutine_system, exec) {
359359
}
360360

361361
TEST(coroutine_system, waitpid) {
362-
auto pid = test::spawn_exec([]() { sleep(2000); });
362+
auto pid = spawn_exec([]() { sleep(2000); });
363363

364364
test::coroutine::run([pid](void *arg) {
365365
int status;
366+
ASSERT_EQ(System::waitpid(pid, &status, WNOHANG, -1), 0);
366367
ASSERT_EQ(System::waitpid(pid, &status, 0, 0.1), -1);
367368
ASSERT_ERREQ(ETIMEDOUT);
368369

369370
kill(pid, SIGKILL);
371+
System::sleep(0.3);
372+
ASSERT_EQ(System::waitpid(pid, &status, 0, 0.1), pid);
373+
});
374+
}
375+
376+
TEST(coroutine_system, waitpid_any) {
377+
auto pid = spawn_exec([]() { sleep(2000); });
378+
379+
test::coroutine::run([pid](void *arg) {
380+
int status;
381+
ASSERT_EQ(System::waitpid(pid, &status, WNOHANG, -1), 0);
382+
ASSERT_EQ(System::waitpid(pid, &status, 0, 0.1), -1);
383+
ASSERT_ERREQ(ETIMEDOUT);
384+
385+
kill(pid, SIGKILL);
386+
System::sleep(0.3);
387+
ASSERT_EQ(System::waitpid(-1, &status, 0, 0.1), pid);
370388
});
371389
}
372390

0 commit comments

Comments
 (0)