Skip to content

Commit a76728f

Browse files
committed
Add tests [17] --filter=[core] --asan
1 parent 23900ba commit a76728f

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

core-tests/src/coroutine/hook.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,14 @@ TEST(coroutine_hook, rename) {
242242
}
243243

244244
TEST(coroutine_hook, flock) {
245-
if (is_github_ci()) {
246-
return;
247-
}
248245
long start_time = swoole::time<std::chrono::milliseconds>();
249246
coroutine::run([&](void *arg) {
250-
swoole::Coroutine::create([&](void *arg) {
247+
Coroutine::create([&](void *arg) {
251248
int fd = swoole_coroutine_open(TEST_TMP_FILE, O_WRONLY, 0);
249+
250+
ASSERT_EQ(swoole_coroutine_flock(fd, 16), SW_ERR);
251+
ASSERT_ERREQ(EINVAL);
252+
252253
ASSERT_EQ(swoole_coroutine_flock(fd, LOCK_EX), 0);
253254
System::sleep(0.1);
254255
ASSERT_EQ(swoole_coroutine_flock(fd, LOCK_UN), 0);
@@ -258,10 +259,10 @@ TEST(coroutine_hook, flock) {
258259
ASSERT_LE(swoole::time<std::chrono::milliseconds>() - start_time, 1000);
259260
swoole_coroutine_close(fd);
260261
});
261-
swoole::Coroutine::create([&](void *arg) {
262+
Coroutine::create([&](void *arg) {
262263
int fd = swoole_coroutine_open(TEST_TMP_FILE, O_WRONLY, 0);
263264
ASSERT_EQ(swoole_coroutine_flock(fd, LOCK_SH), 0);
264-
System::sleep(2);
265+
System::sleep(0.5);
265266
ASSERT_EQ(swoole_coroutine_flock(fd, LOCK_UN), 0);
266267
swoole_coroutine_close(fd);
267268
});

core-tests/src/coroutine/system.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,17 @@ TEST(coroutine_system, waitpid) {
369369
kill(pid, SIGKILL);
370370
});
371371
}
372+
373+
TEST(coroutine_system, read_file_fail) {
374+
test::coroutine::run([](void *arg) {
375+
ASSERT_EQ(System::read_file("/tmp/not-exists", true), nullptr);
376+
ASSERT_EQ(errno, ENOENT);
377+
});
378+
}
379+
380+
TEST(coroutine_system, write_file_fail) {
381+
test::coroutine::run([](void *arg) {
382+
ASSERT_EQ(System::write_file("/tmp/not-exists/file.log", SW_STRL(TEST_STR)), -1);
383+
ASSERT_EQ(errno, ENOENT);
384+
});
385+
}

core-tests/src/server/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ TEST(server, task_worker) {
11961196
ASSERT_EQ(serv.gs->task_count, 2);
11971197
}
11981198

1199-
static void test_task(Server::Mode mode, uint8_t task_ipc_mode = Server::TASK_IPC_UNIXSOCK) {
1199+
static void test_task(Server::Mode mode, uint8_t task_ipc_mode = Server::TASK_IPC_UNIXSOCK) {
12001200
Server serv(mode);
12011201
serv.worker_num = 2;
12021202
serv.task_ipc_mode = task_ipc_mode;

include/swoole_coroutine_system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class System {
4949
static int sleep(double sec);
5050
/* file */
5151
static std::shared_ptr<String> read_file(const char *file, bool lock = false);
52-
static ssize_t write_file(const char *file, char *buf, size_t length, bool lock = 0, int flags = 0);
52+
static ssize_t write_file(const char *file, const char *buf, size_t length, bool lock = 0, int flags = 0);
5353
/* dns */
5454
static std::string gethostbyname(const std::string &hostname, int domain, double timeout = -1);
5555
static std::vector<std::string> getaddrinfo(const std::string &hostname,

src/coroutine/system.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ std::shared_ptr<String> System::read_file(const char *file, bool lock) {
135135
return result;
136136
}
137137

138-
ssize_t System::write_file(const char *file, char *buf, size_t length, bool lock, int flags) {
138+
ssize_t System::write_file(const char *file, const char *buf, size_t length, bool lock, int flags) {
139139
ssize_t retval = -1;
140140
int file_flags = flags | O_CREAT | O_WRONLY;
141141
async([&]() {

0 commit comments

Comments
 (0)