Skip to content

Commit da6a766

Browse files
Added iouring core test (#5783)
1 parent 559ee6d commit da6a766

File tree

4 files changed

+158
-2
lines changed

4 files changed

+158
-2
lines changed

.github/workflows/core.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ jobs:
4040
- uses: actions/checkout@v4
4141

4242
- name: install dependencies
43-
run: sudo apt update -y && sudo apt install -y googletest libgtest-dev libnghttp2-dev libboost-stacktrace-dev libbrotli-dev redis-server nodejs npm nghttp2-client
43+
run: sudo apt update -y && sudo apt install -y googletest libgtest-dev libnghttp2-dev libboost-stacktrace-dev libbrotli-dev redis-server nodejs npm nghttp2-client liburing-dev
4444

4545
- name: configure
46-
run: phpize && ./configure --enable-sockets --enable-mysqlnd --enable-openssl
46+
run: phpize && ./configure --enable-sockets --enable-mysqlnd --enable-openssl --enable-iouring
4747

4848
- name: make
4949
run: |

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ endif()
2424
if (UNIX AND NOT APPLE)
2525
find_library(URING_LIBRARIES uring)
2626
if (URING_LIBRARIES)
27+
message(STATUS "Found iouring")
2728
list(APPEND SWOOLE_LINK_LIBRARIES ${URING_LIBRARIES})
2829
else()
2930
message(WARNING "liburing not found.")

core-tests/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ else()
6363
endif()
6464
endif()
6565

66+
# find iouring
67+
if (UNIX AND NOT APPLE)
68+
find_library(URING_LIBRARIES uring)
69+
if (URING_LIBRARIES)
70+
message(STATUS "Found iouring")
71+
list(APPEND SWOOLE_LINK_LIBRARIES ${URING_LIBRARIES})
72+
else()
73+
message(WARNING "liburing not found.")
74+
endif()
75+
endif()
76+
6677
if (DEFINED enable_asan)
6778
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
6879
endif()

core-tests/src/coroutine/iouring.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Swoole |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 2.0 of the Apache license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.apache.org/licenses/LICENSE-2.0.html |
9+
| If you did not receive a copy of the Apache2.0 license and are unable|
10+
| to obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: NathanFreeman <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include <sys/file.h>
18+
#include <sys/stat.h>
19+
#include "test_coroutine.h"
20+
#include "swoole_coroutine_c_api.h"
21+
22+
#ifdef SW_USE_IOURING
23+
using swoole::Reactor;
24+
using swoole::test::coroutine;
25+
26+
TEST(coroutine_iouring, open_and_close) {
27+
coroutine::run([](void *arg) {
28+
const char *test_file = "/tmp/file_1";
29+
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT, 0666);
30+
ASSERT_TRUE(fd > 0);
31+
32+
int result = swoole_coroutine_iouring_close_file(fd);
33+
ASSERT_TRUE(result == 0);
34+
35+
result = swoole_coroutine_iouring_unlink(test_file);
36+
ASSERT_TRUE(result == 0);
37+
});
38+
}
39+
40+
TEST(coroutine_iouring, mkdir_and_rmdir) {
41+
coroutine::run([](void *arg) {
42+
const char *directory = "/tmp/aaaa";
43+
int result = swoole_coroutine_iouring_mkdir(directory, 0755);
44+
ASSERT_TRUE(result == 0);
45+
46+
result = swoole_coroutine_iouring_rmdir(directory);
47+
ASSERT_TRUE(result == 0);
48+
});
49+
}
50+
51+
TEST(coroutine_iouring, write_and_read) {
52+
coroutine::run([](void *arg) {
53+
const char *test_file = "/tmp/file_2";
54+
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT | O_RDWR, 0666);
55+
ASSERT_TRUE(fd > 0);
56+
57+
const char *data = "aaaaaaaaaaaaaaaaaaaaaaa";
58+
size_t length = strlen(data);
59+
ssize_t result = swoole_coroutine_iouring_write(fd, (const void *) data, length);
60+
ASSERT_TRUE(result > 0);
61+
ASSERT_TRUE(result == static_cast<ssize_t>(length));
62+
63+
lseek(fd, 0, SEEK_SET);
64+
65+
char buf[128];
66+
result = swoole_coroutine_iouring_read(fd, (void *) buf, 128);
67+
ASSERT_TRUE(result > 0);
68+
ASSERT_TRUE(result == static_cast<ssize_t>(length));
69+
buf[result] = '\0';
70+
ASSERT_STREQ(data, buf);
71+
72+
result = swoole_coroutine_iouring_close_file(fd);
73+
ASSERT_TRUE(result == 0);
74+
75+
result = swoole_coroutine_iouring_unlink(test_file);
76+
ASSERT_TRUE(result == 0);
77+
});
78+
}
79+
80+
TEST(coroutine_iouring, rename) {
81+
coroutine::run([](void *arg) {
82+
const char *oldpath = "/tmp/file_2";
83+
const char *newpath = "/tmp/file_3";
84+
int fd = swoole_coroutine_iouring_open(oldpath, O_CREAT | O_RDWR, 0666);
85+
ASSERT_TRUE(fd > 0);
86+
87+
int result = swoole_coroutine_iouring_close_file(fd);
88+
ASSERT_TRUE(result == 0);
89+
90+
result = swoole_coroutine_iouring_rename(oldpath, newpath);
91+
ASSERT_TRUE(result == 0);
92+
93+
result = swoole_coroutine_iouring_unlink(newpath);
94+
ASSERT_TRUE(result == 0);
95+
});
96+
}
97+
98+
TEST(coroutine_iouring, fstat_and_stat) {
99+
coroutine::run([](void *arg) {
100+
struct stat statbuf {};
101+
int fd = swoole_coroutine_iouring_open(TEST_TMP_FILE, O_RDWR, 0666);
102+
ASSERT_TRUE(fd > 0);
103+
int result = swoole_coroutine_iouring_fstat(fd, &statbuf);
104+
ASSERT_TRUE(result == 0);
105+
ASSERT_TRUE(statbuf.st_size > 0);
106+
107+
result = swoole_coroutine_iouring_close_file(fd);
108+
ASSERT_TRUE(result == 0);
109+
110+
statbuf = {};
111+
result = swoole_coroutine_iouring_stat(TEST_TMP_FILE, &statbuf);
112+
ASSERT_TRUE(result == 0);
113+
ASSERT_TRUE(statbuf.st_size > 0);
114+
});
115+
}
116+
117+
TEST(coroutine_iouring, fsync_and_fdatasync) {
118+
coroutine::run([](void *arg) {
119+
const char *test_file = "/tmp/file_2";
120+
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT | O_RDWR, 0666);
121+
ASSERT_TRUE(fd > 0);
122+
123+
const char *data = "aaaaaaaaaaaaaaaaaaaaaaa";
124+
size_t length = strlen(data);
125+
ssize_t write_length = swoole_coroutine_iouring_write(fd, (const void *) data, length);
126+
ASSERT_TRUE(write_length == static_cast<ssize_t>(length));
127+
128+
int result = swoole_coroutine_iouring_fsync(fd);
129+
ASSERT_TRUE(result == 0);
130+
131+
write_length = swoole_coroutine_iouring_write(fd, (const void *) data, length);
132+
ASSERT_TRUE(write_length == static_cast<ssize_t>(length));
133+
134+
result = swoole_coroutine_iouring_fdatasync(fd);
135+
ASSERT_TRUE(result == 0);
136+
137+
result = swoole_coroutine_iouring_close_file(fd);
138+
ASSERT_TRUE(result == 0);
139+
140+
result = swoole_coroutine_iouring_unlink(test_file);
141+
ASSERT_TRUE(result == 0);
142+
});
143+
}
144+
#endif

0 commit comments

Comments
 (0)