Skip to content

[6.0]iouring core test #5783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
- uses: actions/checkout@v4

- name: install dependencies
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
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

- name: configure
run: phpize && ./configure --enable-sockets --enable-mysqlnd --enable-openssl
run: phpize && ./configure --enable-sockets --enable-mysqlnd --enable-openssl --enable-iouring

- name: make
run: |
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ endif()
if (UNIX AND NOT APPLE)
find_library(URING_LIBRARIES uring)
if (URING_LIBRARIES)
message(STATUS "Found iouring")
list(APPEND SWOOLE_LINK_LIBRARIES ${URING_LIBRARIES})
else()
message(WARNING "liburing not found.")
Expand Down
11 changes: 11 additions & 0 deletions core-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ else()
endif()
endif()

# find iouring
if (UNIX AND NOT APPLE)
find_library(URING_LIBRARIES uring)
if (URING_LIBRARIES)
message(STATUS "Found iouring")
list(APPEND SWOOLE_LINK_LIBRARIES ${URING_LIBRARIES})
else()
message(WARNING "liburing not found.")
endif()
endif()

if (DEFINED enable_asan)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
endif()
Expand Down
144 changes: 144 additions & 0 deletions core-tests/src/coroutine/iouring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: NathanFreeman <mariasocute@163.com> |
+----------------------------------------------------------------------+
*/

#include <sys/file.h>
#include <sys/stat.h>
#include "test_coroutine.h"
#include "swoole_coroutine_c_api.h"

#ifdef SW_USE_IOURING
using swoole::Reactor;
using swoole::test::coroutine;

TEST(coroutine_iouring, open_and_close) {
coroutine::run([](void *arg) {
const char *test_file = "/tmp/file_1";
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT, 0666);
ASSERT_TRUE(fd > 0);

int result = swoole_coroutine_iouring_close_file(fd);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_unlink(test_file);
ASSERT_TRUE(result == 0);
});
}

TEST(coroutine_iouring, mkdir_and_rmdir) {
coroutine::run([](void *arg) {
const char *directory = "/tmp/aaaa";
int result = swoole_coroutine_iouring_mkdir(directory, 0755);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_rmdir(directory);
ASSERT_TRUE(result == 0);
});
}

TEST(coroutine_iouring, write_and_read) {
coroutine::run([](void *arg) {
const char *test_file = "/tmp/file_2";
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT | O_RDWR, 0666);
ASSERT_TRUE(fd > 0);

const char *data = "aaaaaaaaaaaaaaaaaaaaaaa";
size_t length = strlen(data);
ssize_t result = swoole_coroutine_iouring_write(fd, (const void *) data, length);
ASSERT_TRUE(result > 0);
ASSERT_TRUE(result == static_cast<ssize_t>(length));

lseek(fd, 0, SEEK_SET);

char buf[128];
result = swoole_coroutine_iouring_read(fd, (void *) buf, 128);
ASSERT_TRUE(result > 0);
ASSERT_TRUE(result == static_cast<ssize_t>(length));
buf[result] = '\0';
ASSERT_STREQ(data, buf);

result = swoole_coroutine_iouring_close_file(fd);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_unlink(test_file);
ASSERT_TRUE(result == 0);
});
}

TEST(coroutine_iouring, rename) {
coroutine::run([](void *arg) {
const char *oldpath = "/tmp/file_2";
const char *newpath = "/tmp/file_3";
int fd = swoole_coroutine_iouring_open(oldpath, O_CREAT | O_RDWR, 0666);
ASSERT_TRUE(fd > 0);

int result = swoole_coroutine_iouring_close_file(fd);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_rename(oldpath, newpath);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_unlink(newpath);
ASSERT_TRUE(result == 0);
});
}

TEST(coroutine_iouring, fstat_and_stat) {
coroutine::run([](void *arg) {
struct stat statbuf {};
int fd = swoole_coroutine_iouring_open(TEST_TMP_FILE, O_RDWR, 0666);
ASSERT_TRUE(fd > 0);
int result = swoole_coroutine_iouring_fstat(fd, &statbuf);
ASSERT_TRUE(result == 0);
ASSERT_TRUE(statbuf.st_size > 0);

result = swoole_coroutine_iouring_close_file(fd);
ASSERT_TRUE(result == 0);

statbuf = {};
result = swoole_coroutine_iouring_stat(TEST_TMP_FILE, &statbuf);
ASSERT_TRUE(result == 0);
ASSERT_TRUE(statbuf.st_size > 0);
});
}

TEST(coroutine_iouring, fsync_and_fdatasync) {
coroutine::run([](void *arg) {
const char *test_file = "/tmp/file_2";
int fd = swoole_coroutine_iouring_open(test_file, O_CREAT | O_RDWR, 0666);
ASSERT_TRUE(fd > 0);

const char *data = "aaaaaaaaaaaaaaaaaaaaaaa";
size_t length = strlen(data);
ssize_t write_length = swoole_coroutine_iouring_write(fd, (const void *) data, length);
ASSERT_TRUE(write_length == static_cast<ssize_t>(length));

int result = swoole_coroutine_iouring_fsync(fd);
ASSERT_TRUE(result == 0);

write_length = swoole_coroutine_iouring_write(fd, (const void *) data, length);
ASSERT_TRUE(write_length == static_cast<ssize_t>(length));

result = swoole_coroutine_iouring_fdatasync(fd);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_close_file(fd);
ASSERT_TRUE(result == 0);

result = swoole_coroutine_iouring_unlink(test_file);
ASSERT_TRUE(result == 0);
});
}
#endif
Loading