Skip to content

Commit 2d9c878

Browse files
authored
Added more core tests, reacfor, remove useless code. (#5774)
1 parent a39a9f0 commit 2d9c878

33 files changed

+822
-669
lines changed

core-tests/include/test_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define TEST_STR2 "I am Rango\n"
4343

4444
#define TEST_LOG_FILE "/tmp/swoole.log"
45+
#define TEST_SOCK_FILE "/tmp/swoole-core-tests.sock"
4546

4647
#define TEST_REQUEST_BAIDU \
4748
"GET / HTTP/1.1\r\n" \

core-tests/src/core/wheel_timer.cpp

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

core-tests/src/coroutine/hook.cpp

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,37 @@ TEST(coroutine_hook, getaddrinfo) {
140140
});
141141
}
142142

143-
TEST(coroutine_hook, fstat) {
144-
coroutine::run([](void *arg) {
145-
int fd = swoole_coroutine_open(TEST_TMP_FILE, O_RDONLY, 0);
146-
struct stat statbuf_1;
147-
swoole_coroutine_fstat(fd, &statbuf_1);
143+
static void test_fstat() {
144+
int fd = swoole_coroutine_open(TEST_TMP_FILE, O_RDONLY, 0);
145+
struct stat statbuf_1;
146+
swoole_coroutine_fstat(fd, &statbuf_1);
148147

149-
struct stat statbuf_2;
150-
fstat(fd, &statbuf_2);
148+
struct stat statbuf_2;
149+
fstat(fd, &statbuf_2);
151150

152-
ASSERT_EQ(memcmp(&statbuf_1, &statbuf_2, sizeof(statbuf_2)), 0);
151+
ASSERT_EQ(memcmp(&statbuf_1, &statbuf_2, sizeof(statbuf_2)), 0);
153152

154-
swoole_coroutine_close(fd);
155-
});
153+
swoole_coroutine_close(fd);
156154
}
157155

158-
TEST(coroutine_hook, statvfs) {
159-
coroutine::run([](void *arg) {
160-
struct statvfs statbuf_1;
161-
swoole_coroutine_statvfs("/tmp", &statbuf_1);
156+
TEST(coroutine_hook, fstat) {
157+
coroutine::run([](void *arg) { test_fstat(); });
158+
test_fstat();
159+
}
162160

163-
struct statvfs statbuf_2;
164-
statvfs("/tmp", &statbuf_2);
161+
static void test_statvfs() {
162+
struct statvfs statbuf_1;
163+
swoole_coroutine_statvfs("/tmp", &statbuf_1);
165164

166-
ASSERT_EQ(memcmp(&statbuf_1, &statbuf_2, sizeof(statbuf_2)), 0);
167-
});
165+
struct statvfs statbuf_2;
166+
statvfs("/tmp", &statbuf_2);
167+
168+
ASSERT_EQ(memcmp(&statbuf_1, &statbuf_2, sizeof(statbuf_2)), 0);
169+
}
170+
171+
TEST(coroutine_hook, statvfs) {
172+
coroutine::run([](void *arg) { test_statvfs(); });
173+
test_statvfs();
168174
}
169175

170176
static void test_hook_dir() {
@@ -403,7 +409,13 @@ TEST(coroutine_hook, timeout) {
403409
size_t length = text.length();
404410

405411
// unregister fd
412+
errno = 0;
406413
ASSERT_EQ(swoole_coroutine_socket_set_timeout(pairs[0], SO_SNDTIMEO, 0.05), -1);
414+
ASSERT_EQ(errno, EINVAL);
415+
416+
errno = 0;
417+
ASSERT_EQ(swoole_coroutine_socket_set_connect_timeout(pairs[0], 0.05), -1);
418+
ASSERT_EQ(errno, EINVAL);
407419

408420
swoole::Coroutine::create([&](void *) {
409421
ASSERT_EQ(swoole_coroutine_socket_create(pairs[0]), 0);
@@ -665,3 +677,60 @@ TEST(coroutine_hook, poll_fake) {
665677
ASSERT_TRUE(fds[0].revents & POLLOUT);
666678
});
667679
}
680+
681+
TEST(coroutine_hook, unwrap) {
682+
auto pair = create_socket_pair();
683+
auto _sock0 = pair.first;
684+
auto _sock1 = pair.second;
685+
686+
ASSERT_EQ(swoole_coroutine_socket_unwrap(_sock0->get_fd()), -1);
687+
688+
coroutine::run([&](void *arg) {
689+
ASSERT_EQ(swoole_coroutine_socket_unwrap(999999), -1);
690+
ASSERT_EQ(swoole_coroutine_socket_create(_sock0->get_fd()), 0);
691+
ASSERT_GT(swoole_coroutine_write(_sock0->get_fd(), SW_STRL(TEST_STR)), 0);
692+
693+
// blocking, wrap-socket not exists
694+
char buf[128];
695+
ASSERT_EQ(swoole_coroutine_read(_sock1->get_fd(), buf, sizeof(buf)), strlen(TEST_STR));
696+
ASSERT_EQ(swoole_coroutine_socket_unwrap(_sock1->get_fd()), -1);
697+
// unwrap
698+
ASSERT_EQ(swoole_coroutine_socket_unwrap(_sock0->get_fd()), 0);
699+
// fail to unwrap
700+
ASSERT_EQ(swoole_coroutine_socket_unwrap(_sock0->get_fd()), -1);
701+
});
702+
}
703+
704+
static void test_freopen() {
705+
auto output_file = "/tmp/output.txt";
706+
unlink(output_file);
707+
unlink(TEST_LOG_FILE);
708+
709+
auto fp = swoole_coroutine_fopen(TEST_LOG_FILE, "w");
710+
if (fp == NULL) {
711+
fprintf(stderr, "Failed to open '%s': %s (errno=%d)\n", TEST_LOG_FILE, strerror(errno), errno);
712+
}
713+
ASSERT_NE(fp, nullptr);
714+
swoole_coroutine_fputs("hello\n", fp);
715+
716+
ASSERT_NE(swoole_coroutine_freopen(output_file, "w", fp), nullptr);
717+
swoole_coroutine_fputs("world\n", fp);
718+
719+
swoole_coroutine_fclose(fp);
720+
721+
auto rs1 = swoole::file_get_contents(output_file);
722+
ASSERT_FALSE(rs1->contains("hello\n"));
723+
ASSERT_TRUE(rs1->contains("world\n"));
724+
725+
auto rs2 = swoole::file_get_contents(TEST_LOG_FILE);
726+
ASSERT_TRUE(rs2->contains("hello\n"));
727+
ASSERT_FALSE(rs2->contains("world\n"));
728+
729+
unlink(TEST_LOG_FILE);
730+
unlink(output_file);
731+
}
732+
733+
TEST(coroutine_hook, freopen) {
734+
coroutine::run([&](void *arg) { test_freopen(); });
735+
test_freopen();
736+
}

core-tests/src/network/client.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,4 @@ TEST(client, ssl) {
773773

774774
ASSERT_TRUE(sw_tg_buffer()->contains("中华人民共和国"));
775775
}
776-
777-
TEST(client, ssl_reinit) {
778-
swoole_ssl_destroy();
779-
swoole_ssl_init();
780-
test_ssl_get_baidu();
781-
}
782776
#endif

core-tests/src/network/socket.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,18 +609,21 @@ TEST(socket, get_domain_and_type) {
609609
test_sock_type(SW_SOCK_UDP6, AF_INET6, SOCK_DGRAM);
610610
test_sock_type(SW_SOCK_UNIX_STREAM, AF_LOCAL, SOCK_STREAM);
611611
test_sock_type(SW_SOCK_UNIX_DGRAM, AF_LOCAL, SOCK_DGRAM);
612+
test_sock_type(SW_SOCK_RAW, AF_INET, SOCK_RAW);
613+
test_sock_type(SW_SOCK_RAW6, AF_INET6, SOCK_RAW);
612614

613615
int sock_domain, sock_type;
614-
ASSERT_EQ(network::Socket::get_domain_and_type(SW_SOCK_RAW, &sock_domain, &sock_type), SW_ERR);
616+
ASSERT_EQ(network::Socket::get_domain_and_type((swSocketType) (SW_SOCK_RAW6 + 1), &sock_domain, &sock_type),
617+
SW_ERR);
615618
}
616619

617620
TEST(socket, make_socket) {
618621
network::Socket *sock;
619622

620623
sock = make_socket(SW_SOCK_RAW, SW_FD_STREAM, 0);
621624
ASSERT_EQ(sock, nullptr);
622-
ASSERT_EQ(errno, ESOCKTNOSUPPORT);
623-
ASSERT_EQ(swoole_get_last_error(), ESOCKTNOSUPPORT);
625+
ASSERT_EQ(errno, EPROTONOSUPPORT);
626+
ASSERT_EQ(swoole_get_last_error(), EPROTONOSUPPORT);
624627

625628
sock = make_socket(SW_SOCK_TCP, SW_FD_STREAM, AF_INET6, SOCK_RDM, 999, 0);
626629
ASSERT_EQ(sock, nullptr);
@@ -635,8 +638,13 @@ TEST(socket, make_server_socket) {
635638

636639
sock = make_server_socket(SW_SOCK_RAW, bad_addr);
637640
ASSERT_EQ(sock, nullptr);
638-
ASSERT_EQ(errno, ESOCKTNOSUPPORT);
639-
ASSERT_EQ(swoole_get_last_error(), ESOCKTNOSUPPORT);
641+
if (geteuid() == 0) { // root
642+
ASSERT_EQ(errno, EPROTONOSUPPORT);
643+
ASSERT_EQ(swoole_get_last_error(), EPROTONOSUPPORT);
644+
} else {
645+
ASSERT_EQ(errno, ESOCKTNOSUPPORT);
646+
ASSERT_EQ(swoole_get_last_error(), ESOCKTNOSUPPORT);
647+
}
640648

641649
sock = make_server_socket(SW_SOCK_TCP, bad_addr);
642650
ASSERT_EQ(sock, nullptr);

core-tests/src/os/async.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,34 @@ TEST(async, schedule) {
109109
ASSERT_EQ(handle_count, N);
110110
ASSERT_EQ(callback_count, N);
111111
}
112+
113+
TEST(async, misc) {
114+
callback_count = 0;
115+
std::atomic<int> handle_count(0);
116+
AsyncEvent event = {};
117+
AsyncEvent *rv;
118+
event.object = &handle_count;
119+
event.callback = [](AsyncEvent *event) { callback_count++; };
120+
event.handler = [](AsyncEvent *event) { ++(*static_cast<std::atomic<int> *>(event->object)); };
121+
122+
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
123+
124+
auto ret = swoole::async::dispatch(&event);
125+
EXPECT_EQ(ret->object, event.object);
126+
127+
sw_async_threads()->notify_one();
128+
129+
AsyncEvent event2 = {};
130+
event2.callback = [](AsyncEvent *event) {
131+
ASSERT_EQ(event->retval, -1);
132+
ASSERT_EQ(event->error, SW_ERROR_AIO_BAD_REQUEST);
133+
callback_count++;
134+
};
135+
rv = swoole::async::dispatch(&event2);
136+
EXPECT_NE(rv, nullptr);
137+
138+
swoole_event_wait();
139+
140+
ASSERT_EQ(handle_count, 1);
141+
ASSERT_EQ(callback_count, 2);
142+
}

core-tests/src/os/os.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ TEST(os, thread_name) {
7777
});
7878
t.join();
7979
}
80+
81+
TEST(os, thread_id) {
82+
auto tid = swoole_thread_id_to_str(std::this_thread::get_id());
83+
DEBUG() << "current thread id: " << tid << "\n";
84+
ASSERT_FALSE(tid.empty());
85+
}

core-tests/src/os/process_pool.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ static void test_func(ProcessPool &pool) {
2121
data.info.len = size;
2222
memcpy(data.data, rmem.value(), size);
2323

24+
DEBUG() << "dispatch: " << size << " bytes\n";
25+
2426
int worker_id = -1;
2527
ASSERT_EQ(pool.dispatch_sync(&data, &worker_id), SW_OK);
2628

@@ -48,6 +50,8 @@ static void test_func_message_protocol(ProcessPool &pool) {
4850
pool->running = false;
4951
String *_data = (String *) pool->ptr;
5052
usleep(10000);
53+
54+
DEBUG() << "received: " << rdata->info.len << " bytes\n";
5155
EXPECT_MEMEQ(_data->str, rdata->data, rdata->info.len);
5256
};
5357
test_func(pool);
@@ -60,6 +64,8 @@ static void test_func_stream_protocol(ProcessPool &pool) {
6064
String *_data = (String *) pool->ptr;
6165
EventData *msg = (EventData *) rdata->data;
6266
usleep(10000);
67+
68+
DEBUG() << "received: " << rdata->info.len << " bytes\n";
6369
EXPECT_MEMEQ(_data->str, msg->data, msg->len());
6470
};
6571
test_func(pool);
@@ -93,6 +99,10 @@ TEST(process_pool, unix_sock) {
9399
ProcessPool pool{};
94100
signal(SIGPIPE, SIG_IGN);
95101
ASSERT_EQ(pool.create(1, 0, SW_IPC_UNIXSOCK), SW_OK);
102+
ASSERT_EQ(pool.listen(TEST_HOST, TEST_PORT, 128), SW_ERR);
103+
ASSERT_ERREQ(SW_ERROR_OPERATION_NOT_SUPPORT);
104+
ASSERT_EQ(pool.listen(TEST_SOCK_FILE, 128), SW_ERR);
105+
ASSERT_ERREQ(SW_ERROR_OPERATION_NOT_SUPPORT);
96106

97107
test_func_task_protocol(pool);
98108
}
@@ -144,6 +154,13 @@ TEST(process_pool, stream_protocol) {
144154
test_func_stream_protocol(pool);
145155
}
146156

157+
TEST(process_pool, stream_protocol_with_msgq) {
158+
ProcessPool pool{};
159+
ASSERT_EQ(pool.create(1, 0x9501, SW_IPC_MSGQUEUE), SW_OK);
160+
161+
test_func_stream_protocol(pool);
162+
}
163+
147164
constexpr int magic_number = 99900011;
148165
static ProcessPool *current_pool = nullptr;
149166
static Worker *current_worker = nullptr;
@@ -542,10 +559,57 @@ TEST(process_pool, listen_unixsock) {
542559
t1.join();
543560
}
544561

545-
TEST(process_pool, max_request_grace) {
562+
TEST(process_pool, worker) {
546563
Worker worker{};
564+
worker.init();
565+
566+
ASSERT_TRUE(worker.is_running());
567+
ASSERT_GT(worker.start_time, 0);
547568
worker.set_max_request(1000, 200);
548569

549570
ASSERT_GT(SwooleWG.max_request, 1000);
550571
ASSERT_LE(SwooleWG.max_request, 1200);
572+
573+
worker.shutdown();
574+
ASSERT_TRUE(worker.is_shutdown());
575+
576+
swoole_set_worker_type(SW_USER_WORKER);
577+
ASSERT_EQ(swoole_get_worker_symbol(), '@');
578+
579+
swoole_set_worker_type(SW_TASK_WORKER);
580+
ASSERT_EQ(swoole_get_worker_symbol(), '^');
581+
582+
swoole_set_worker_type(SW_WORKER);
583+
ASSERT_EQ(swoole_get_worker_symbol(), '*');
584+
585+
swoole_set_worker_type(SW_MASTER);
586+
ASSERT_EQ(swoole_get_worker_symbol(), '#');
587+
588+
swoole_set_worker_type(SW_MANAGER);
589+
ASSERT_EQ(swoole_get_worker_symbol(), '$');
590+
591+
worker.set_status_to_idle();
592+
ASSERT_TRUE(worker.is_idle());
593+
ASSERT_FALSE(worker.is_busy());
594+
595+
worker.set_status_to_busy();
596+
ASSERT_FALSE(worker.is_idle());
597+
ASSERT_TRUE(worker.is_busy());
598+
599+
worker.set_status(SW_WORKER_EXIT);
600+
ASSERT_FALSE(worker.is_idle());
601+
ASSERT_FALSE(worker.is_busy());
602+
}
603+
604+
TEST(process_pool, add_worker) {
605+
Worker worker{};
606+
worker.pid = getpid();
607+
608+
ProcessPool pool{};
609+
ASSERT_EQ(pool.create(1, 0, SW_IPC_UNIXSOCK), SW_OK);
610+
611+
pool.add_worker(&worker);
612+
613+
auto *worker2 = pool.get_worker_by_pid(getpid());
614+
ASSERT_EQ(&worker, worker2);
551615
}

core-tests/src/protocol/base.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,6 @@ TEST(protocol, unpack) {
322322
{
323323
char buffer[8];
324324

325-
// Create a test value that will be different when byte-swapped
326-
uint16_t test16 = 0x1234;
327-
uint32_t test32 = 0x12345678;
328-
uint64_t test64 = 0x123456789ABCDEF0ULL;
329-
330325
// Test that 'n' and 'v' formats handle endianness correctly
331326
buffer[0] = 0x12;
332327
buffer[1] = 0x34;

0 commit comments

Comments
 (0)