|
| 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