Skip to content

Commit 26223cb

Browse files
authored
Optimzie core tests (#5748)
* Optimize core tests --filter=[core] * Optimize core tests [2] --filter=[core] * Optimize core tests [3] --filter=[core] * Optimize core tests [4] --filter=[core] * Optimize core tests [5] --filter=[core]
1 parent f9f4312 commit 26223cb

File tree

21 files changed

+282
-74
lines changed

21 files changed

+282
-74
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ if(CODE_COVERAGE)
4242
target_compile_options(coverage_config INTERFACE
4343
-O0
4444
-g
45+
-fprofile-update=atomic
4546
--coverage
4647
)
4748
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)

core-tests/include/test_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ std::string get_ssl_dir();
5656
std::string get_jpg_file();
5757
bool is_github_ci();
5858
int exec_js_script(const std::string &file, const std::string &args);
59+
std::string http_get_request(const std::string &domain, const std::string &path);
5960
int get_random_port();
6061

6162
Socks5Proxy *create_socks5_proxy();
6263
HttpProxy *create_http_proxy();
6364

65+
pid_t child_proc(const std::function<void(void)> &fn);
66+
6467
} // namespace test
6568
}; // namespace swoole

core-tests/src/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ string get_jpg_file() {
5858
return root_path + TEST_JPG_FILE;
5959
}
6060

61+
string http_get_request(const string &domain, const string &path) {
62+
return "GET " + path +
63+
" HTTP/1.1\r\n"
64+
"Host: " +
65+
domain +
66+
"\r\n"
67+
"Connection: close\r\n"
68+
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
69+
"Chrome/51.0.2704.106 Safari/537.36"
70+
"\r\n\r\n";
71+
}
72+
6173
bool is_github_ci() {
6274
return getenv("GITHUB_ACTIONS") != nullptr;
6375
}
@@ -95,5 +107,16 @@ int get_random_port() {
95107
return TEST_PORT + swoole_system_random(1, 10000);
96108
}
97109

110+
pid_t child_proc(const std::function<void(void)> &fn) {
111+
pid_t child_pid = fork();
112+
if (child_pid == -1) {
113+
throw std::system_error();
114+
} else if (child_pid == 0) {
115+
fn();
116+
exit(0);
117+
}
118+
return child_pid;
119+
}
120+
98121
} // namespace test
99122
} // namespace swoole

core-tests/src/network/client.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ using swoole::Pipe;
1010
using swoole::Socks5Proxy;
1111
using swoole::String;
1212
using swoole::network::AsyncClient;
13-
using swoole::network::SyncClient;
1413
using swoole::network::Client;
14+
using swoole::network::SyncClient;
1515
using swoole::test::create_http_proxy;
1616
using swoole::test::create_socks5_proxy;
1717
using swoole::test::Process;
@@ -159,6 +159,34 @@ TEST(client, async_tcp_dns) {
159159
test_async_client_tcp("localhost", swoole::test::get_random_port());
160160
}
161161

162+
TEST(client, sleep) {
163+
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
164+
165+
String buf(65536);
166+
167+
auto domain = "httpbin.org";
168+
169+
Client client(SW_SOCK_TCP, true);
170+
client.onConnect = [&domain](Client *cli) {
171+
cli->sleep();
172+
swoole_timer_after(200, [cli, &domain](auto _1, auto _2) {
173+
auto req = swoole::test::http_get_request(domain, "/");
174+
cli->send(cli, req.c_str(), req.length(), 0);
175+
cli->wakeup();
176+
});
177+
};
178+
179+
client.onError = [](Client *cli) {};
180+
client.onClose = [](Client *cli) {};
181+
client.onReceive = [&buf](Client *cli, const char *data, size_t length) { buf.append(data, length); };
182+
183+
ASSERT_EQ(client.connect(&client, domain, 80, -1, 0), 0);
184+
185+
swoole_event_wait();
186+
187+
ASSERT_TRUE(buf.contains("HTTP/1.1 200 OK"));
188+
}
189+
162190
TEST(client, connect_refuse) {
163191
int ret;
164192
Client cli(SW_SOCK_TCP, false);

core-tests/src/network/socket.cpp

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -341,42 +341,43 @@ TEST(socket, check_liveness) {
341341
#define CRLF "\r\n"
342342

343343
TEST(socket, sync) {
344-
auto sock = make_socket(SW_SOCK_TCP, SW_FD_STREAM, 0);
345-
346-
swoole::network::Address addr;
347-
ASSERT_TRUE(addr.assign("tcp://httpbin.org:80"));
348-
349-
ASSERT_EQ(sock->connect(addr), 0);
350-
351-
const char *req = "GET / HTTP/1.1" CRLF \
352-
"Host: httpbin.org" CRLF \
353-
"User-Agent: curl/7.81.0" CRLF \
354-
"Accept: */*" CRLF \
355-
"Connection: close" CRLF \
356-
CRLF CRLF;
357-
ssize_t n = strlen(req);
358-
ASSERT_EQ(sock->write_sync(req, n), n);
359-
360-
string resp;
361-
SW_LOOP {
362-
char buf[1024];
363-
n = sock->read_sync(buf, sizeof(buf));
364-
if (n == 0) {
365-
break;
366-
}
367-
ASSERT_GT(n, 0);
368-
resp.append(buf, n);
369-
}
370-
371-
ASSERT_GT(resp.length(), 4096);
372-
sock->free();
344+
auto sock = make_socket(SW_SOCK_TCP, SW_FD_STREAM, 0);
345+
346+
swoole::network::Address addr;
347+
ASSERT_TRUE(addr.assign("tcp://httpbin.org:80"));
348+
349+
ASSERT_EQ(sock->connect(addr), 0);
350+
351+
const char *req = "GET / HTTP/1.1" CRLF "Host: httpbin.org" CRLF "User-Agent: curl/7.81.0" CRLF "Accept: */*" CRLF
352+
"Connection: close" CRLF CRLF CRLF;
353+
ssize_t n = strlen(req);
354+
ASSERT_EQ(sock->write_sync(req, n), n);
355+
ASSERT_TRUE(sock->check_liveness());
356+
357+
string resp;
358+
SW_LOOP {
359+
char buf[1024];
360+
n = sock->read_sync(buf, sizeof(buf));
361+
if (n == 0) {
362+
break;
363+
}
364+
ASSERT_GT(n, 0);
365+
resp.append(buf, n);
366+
}
367+
368+
ASSERT_GT(resp.length(), 4096);
369+
370+
usleep(50000);
371+
ASSERT_FALSE(sock->check_liveness());
372+
373+
sock->free();
373374
}
374375

375376
TEST(socket, ipv6_addr) {
376-
auto sock = make_socket(SW_SOCK_TCP6, SW_FD_STREAM, 0);
377-
swoole::network::Address addr;
378-
ASSERT_TRUE(addr.assign("tcp://[::1]:12345"));
379-
ASSERT_EQ(sock->connect(addr), SW_ERR);
380-
ASSERT_EQ(errno, ECONNREFUSED);
381-
sock->free();
377+
auto sock = make_socket(SW_SOCK_TCP6, SW_FD_STREAM, 0);
378+
swoole::network::Address addr;
379+
ASSERT_TRUE(addr.assign("tcp://[::1]:12345"));
380+
ASSERT_EQ(sock->connect(addr), SW_ERR);
381+
ASSERT_EQ(errno, ECONNREFUSED);
382+
sock->free();
382383
}

core-tests/src/os/os.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
| @link https://www.swoole.com/ |
14+
| @contact [email protected] |
15+
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
16+
| @Author Tianfeng Han <[email protected]> |
17+
+----------------------------------------------------------------------+
18+
*/
19+
20+
#include "test_core.h"
21+
22+
#include "swoole_file.h"
23+
#include "swoole_thread.h"
24+
25+
using namespace swoole;
26+
27+
TEST(os, daemon) {
28+
auto sid = getsid(getpid());
29+
int status;
30+
swoole_waitpid(test::child_proc([sid](){
31+
ASSERT_EQ(sid, getsid(getpid()));
32+
ASSERT_TRUE(isatty(STDIN_FILENO));
33+
34+
ASSERT_EQ(swoole_daemon(0, 0), 0);
35+
ASSERT_NE(sid, getsid(getpid()));
36+
37+
ASSERT_FALSE(isatty(STDIN_FILENO));
38+
}), &status, 0);
39+
}
40+
41+
TEST(os, cpu_affinity) {
42+
cpu_set_t ori_affinity, affinity;
43+
ASSERT_EQ(swoole_get_cpu_affinity(&affinity), 0);
44+
ori_affinity = affinity;
45+
46+
CPU_ZERO(&affinity);
47+
CPU_SET(1, &affinity);
48+
49+
ASSERT_EQ(swoole_set_cpu_affinity(&affinity), 0);
50+
ASSERT_EQ(swoole_get_cpu_affinity(&affinity), 0);
51+
52+
auto cpu_n = SW_CPU_NUM;
53+
SW_LOOP_N(cpu_n) {
54+
if (i == 1) {
55+
ASSERT_TRUE(CPU_ISSET(i, &affinity));
56+
} else {
57+
ASSERT_FALSE(CPU_ISSET(i, &affinity));
58+
}
59+
}
60+
61+
ASSERT_EQ(swoole_set_cpu_affinity(&ori_affinity), 0);
62+
}
63+
64+
TEST(os, thread_name) {
65+
std::thread t([](){
66+
char new_name[512];
67+
auto thread_name = "sw-core-tests";
68+
ASSERT_TRUE(swoole_thread_set_name(thread_name));
69+
ASSERT_TRUE(swoole_thread_get_name(new_name, sizeof(new_name)));
70+
71+
ASSERT_STREQ(thread_name, new_name);
72+
73+
ASSERT_FALSE(swoole_thread_set_name("swoole-core-tests-max-size-is-16"));
74+
ASSERT_EQ(swoole_get_last_error(), ERANGE);
75+
});
76+
t.join();
77+
}

core-tests/src/protocol/http2.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,43 @@ TEST(http2, default_settings) {
6161
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE), _settings.max_frame_size);
6262
ASSERT_EQ(http2::get_default_setting(SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE), _settings.max_header_list_size);
6363
}
64+
65+
TEST(http2, pack_setting_frame) {
66+
char frame[SW_HTTP2_SETTING_FRAME_SIZE];
67+
http2::Settings settings_1{};
68+
http2::init_settings(&settings_1);
69+
size_t n = http2::pack_setting_frame(frame, settings_1, false);
70+
71+
ASSERT_GT(n, 16);
72+
73+
http2::Settings settings_2{};
74+
http2::unpack_setting_data(
75+
frame + SW_HTTP2_FRAME_HEADER_SIZE, n, [&settings_2](uint16_t id, uint32_t value) -> ReturnCode {
76+
swoole_http2_frame_trace_log("id=%d, value=%d", id, value);
77+
switch (id) {
78+
case SW_HTTP2_SETTING_HEADER_TABLE_SIZE:
79+
settings_2.header_table_size = value;
80+
break;
81+
case SW_HTTP2_SETTINGS_ENABLE_PUSH:
82+
settings_2.enable_push = value;
83+
break;
84+
case SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
85+
settings_2.max_concurrent_streams = value;
86+
break;
87+
case SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE:
88+
settings_2.init_window_size = value;
89+
break;
90+
case SW_HTTP2_SETTINGS_MAX_FRAME_SIZE:
91+
settings_2.max_frame_size = value;
92+
break;
93+
case SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
94+
settings_2.max_header_list_size = value;
95+
break;
96+
default:
97+
return SW_ERROR;
98+
}
99+
return SW_SUCCESS;
100+
});
101+
102+
ASSERT_MEMEQ(&settings_1, &settings_2, sizeof(settings_2));
103+
}

core-tests/src/server/message_bus.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ TEST(message_bus, read_with_buffer) {
188188
MB_ASSERT(2);
189189
MB_ASSERT(3);
190190

191+
ASSERT_GT(tmb.mb.get_memory_size(), 1024 * 1024 * 2);
192+
191193
auto r4 = tmb.q.at(3);
192194
ASSERT_EQ(r4.fd, 4);
193195
ASSERT_STREQ(r4.data.c_str(), "");
@@ -196,3 +198,6 @@ TEST(message_bus, read_with_buffer) {
196198
ASSERT_EQ(r5.fd, 5);
197199
ASSERT_STREQ(r5.data.c_str(), "");
198200
}
201+
202+
203+

ext-src/swoole_http2_client_coro.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
512512
last_stream_id = stream_id;
513513
}
514514

515-
uint16_t id = 0;
516515
uint32_t value = 0;
517516

518517
switch (type) {
@@ -522,9 +521,7 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
522521
return SW_CONTINUE;
523522
}
524523

525-
while (length > 0) {
526-
id = ntohs(*(uint16_t *) (buf));
527-
value = ntohl(*(uint32_t *) (buf + sizeof(uint16_t)));
524+
auto rc = Http2::unpack_setting_data(buf, length, [this](uint16_t id, uint32_t value) -> ReturnCode {
528525
swoole_http2_frame_trace_log("id=%d, value=%d", id, value);
529526
switch (id) {
530527
case SW_HTTP2_SETTING_HEADER_TABLE_SIZE:
@@ -568,8 +565,11 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
568565
// swoole_warning("unknown option[%d]: %d", id, value);
569566
break;
570567
}
571-
buf += sizeof(id) + sizeof(value);
572-
length -= sizeof(id) + sizeof(value);
568+
return SW_SUCCESS;
569+
});
570+
571+
if (rc != SW_SUCCESS) {
572+
return rc;
573573
}
574574

575575
Http2::set_frame_header(frame, SW_HTTP2_TYPE_SETTINGS, 0, SW_HTTP2_FLAG_ACK, stream_id);

include/swoole.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ typedef unsigned long ulong_t;
148148
#define SW_LOOP_N(n) for (decltype(n) i = 0; i < n; i++)
149149
#define SW_LOOP for (;;)
150150

151+
#ifndef MAYBE_UNUSED
152+
#ifdef __GNUC__
153+
#define MAYBE_UNUSED __attribute__((used))
154+
#else
155+
#define MAYBE_UNUSED
156+
#endif
157+
#endif
158+
151159
#ifndef MAX
152160
#define MAX(A, B) SW_MAX(A, B)
153161
#endif

0 commit comments

Comments
 (0)