Skip to content

Commit 1769811

Browse files
authored
Increase the unit test coverage, optimize the code in the reactor/string module, and remove the select reactor. (#5757)
* Added more core tests for Reactor, optimize code, add comments. --filter=[core] * optimize core tests --filter=[core] * fix tests [2] --filter=[core] * fix tests [3] --filter=[core] * Added sw_printf/swoole_set_stdout_stream/swoole_get_stdout_stream API. Added more core tests for coroutine. * code format * Remove select reactor. Optimize reactor/coroutine core tests. --filter=[core] * Remove useless code in Reactor module. Optimiz tests * Move the functions with a larger number of lines from the header file to the .cc source file. * Optimize tests [4] --filter=[core] * Optimize tests [5] --filter=[core] * Optimize tests [6] --filter=[core] * Optimize tests [7] --filter=[core]
1 parent d0b9d54 commit 1769811

File tree

28 files changed

+840
-590
lines changed

28 files changed

+840
-590
lines changed

core-tests/include/test_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535

3636
#define TEST_DOMAIN_BAIDU "www.baidu.com"
3737

38+
#define TEST_HTTP_DOMAIN "www.gov.cn"
39+
#define TEST_HTTP_EXPECT "Location: https://www.gov.cn/"
40+
41+
#define TEST_STR "hello world, hello swoole\n"
42+
#define TEST_STR2 "I am Rango\n"
43+
44+
#define TEST_LOG_FILE "/tmp/swoole.log"
45+
3846
#define TEST_REQUEST_BAIDU \
3947
"GET / HTTP/1.1\r\n" \
4048
"Host: www.baidu.com\r\n" \

core-tests/src/core/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ TEST(base, only_dump) {
307307
}
308308

309309
TEST(base, redirect_stdout) {
310-
auto file = "/tmp/swoole.log";
310+
auto file = TEST_LOG_FILE;
311311
auto out_1 = "hello world, hello swoole!\n";
312312
auto out_2 = "write to /dev/null\n";
313313
auto status = test::spawn_exec_and_wait([&]() {

core-tests/src/core/log.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ TEST(log, redirect_1) {
143143

144144
TEST(log, redirect_2) {
145145
auto status = test::spawn_exec_and_wait([]() {
146-
auto file = "/tmp/swoole.log";
146+
auto file = TEST_LOG_FILE;
147147
auto str = "hello world, hello swoole\n";
148148

149149
sw_logger()->reset();

core-tests/src/core/string.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,98 @@ TEST(string, rtrim) {
4545
ASSERT_EQ(strcmp("", buf), 0);
4646
}
4747

48+
TEST(string, move_and_copy) {
49+
String s1(TEST_STR);
50+
ASSERT_MEMEQ(s1.str, TEST_STR, s1.length);
51+
52+
String s2(s1);
53+
ASSERT_MEMEQ(s2.str, TEST_STR, s2.length);
54+
ASSERT_NE(s1.str, nullptr);
55+
56+
String s3(std::move(s1));
57+
ASSERT_MEMEQ(s3.str, TEST_STR, s3.length);
58+
ASSERT_EQ(s1.str, nullptr);
59+
60+
String s4;
61+
s4 = s3;
62+
ASSERT_MEMEQ(s4.str, TEST_STR, s4.length);
63+
ASSERT_NE(s3.str, nullptr);
64+
65+
String s5;
66+
s5 = std::move(s3);
67+
ASSERT_MEMEQ(s5.str, TEST_STR, s5.length);
68+
ASSERT_EQ(s3.str, nullptr);
69+
70+
String s6(SW_STRL(TEST_STR));
71+
ASSERT_MEMEQ(s6.str, TEST_STR, s6.length);
72+
}
73+
74+
TEST(string, append) {
75+
String s1(TEST_STR);
76+
s1.append(12345678);
77+
78+
String s2(TEST_STR2);
79+
s1.append(s2);
80+
81+
ASSERT_MEMEQ(s1.str, TEST_STR "12345678" TEST_STR2, s1.length);
82+
}
83+
84+
TEST(string, write) {
85+
String s1;
86+
s1.reserve(32);
87+
88+
String s2(TEST_STR);
89+
s1.repeat(" ", 1, 30);
90+
s1.write(30, s2);
91+
92+
auto s3 = s1.substr(30, s2.length);
93+
ASSERT_MEMEQ(s3.str, TEST_STR, s3.length);
94+
}
95+
96+
TEST(string, repeat) {
97+
auto end_str = "[end]";
98+
String s1;
99+
s1.repeat(SW_STRL("hello\r\n"), 5);
100+
s1.append(end_str);
101+
102+
int count = 0;
103+
auto offset = s1.split(SW_STRL("\r\n"), [&](const char *data, size_t length) -> bool {
104+
count++;
105+
EXPECT_MEMEQ(data, "hello\r\n", 7);
106+
return true;
107+
});
108+
109+
ASSERT_EQ(offset, s1.length - strlen(end_str));
110+
ASSERT_MEMEQ(s1.str + offset, end_str, strlen(end_str));
111+
112+
ASSERT_EQ(count, 5);
113+
}
114+
115+
TEST(string, release) {
116+
String s1(TEST_STR);
117+
ASSERT_MEMEQ(s1.str, TEST_STR, s1.length);
118+
119+
auto s2 = s1.release();
120+
ASSERT_EQ(s1.str, nullptr);
121+
ASSERT_EQ(s1.length, 0);
122+
123+
ASSERT_MEMEQ(s2, TEST_STR, strlen(TEST_STR));
124+
sw_free(s2);
125+
}
126+
127+
TEST(string, ub) {
128+
String s1(TEST_STR);
129+
String s2(TEST_STR2);
130+
131+
s1 = s2;
132+
s1 = s1;
133+
134+
auto rs = s1.substr(s1.length, 10);
135+
ASSERT_EQ(rs.str, nullptr);
136+
137+
ASSERT_FALSE(s1.repeat("\r\n", 2, 0));
138+
}
139+
48140
TEST(string, strnpos) {
49141
{
50142
string haystack = "hello world";

core-tests/src/coroutine/base.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,44 @@ TEST(coroutine_base, cancel) {
210210
});
211211
}
212212

213+
TEST(coroutine_base, noncancelable) {
214+
std::unordered_map<std::string, bool> flags;
215+
coroutine::run([&flags](void *arg) {
216+
auto cid = Coroutine::create([&flags](void *_arg) {
217+
Coroutine *current = Coroutine::get_current();
218+
flags["yield_1"] = true;
219+
current->yield();
220+
ASSERT_FALSE(current->is_canceled());
221+
222+
flags["yield_2"] = true;
223+
current->yield_ex(-1);
224+
ASSERT_TRUE(current->is_canceled());
225+
});
226+
227+
auto co = Coroutine::get_by_cid(cid);
228+
229+
flags["cancel_1"] = true;
230+
ASSERT_FALSE(co->cancel());
231+
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_CO_CANNOT_CANCEL);
232+
flags["resume_1"] = true;
233+
co->resume();
234+
235+
flags["cancel_2"] = true;
236+
ASSERT_TRUE(co->cancel());
237+
flags["resume_2"] = true;
238+
239+
flags["done"] = true;
240+
});
241+
242+
ASSERT_TRUE(flags["yield_1"]);
243+
ASSERT_TRUE(flags["yield_2"]);
244+
ASSERT_TRUE(flags["cancel_1"]);
245+
ASSERT_TRUE(flags["resume_1"]);
246+
ASSERT_TRUE(flags["cancel_2"]);
247+
ASSERT_TRUE(flags["resume_2"]);
248+
ASSERT_TRUE(flags["done"]);
249+
}
250+
213251
TEST(coroutine_base, timeout) {
214252
coroutine::run([](void *arg) {
215253
auto co = Coroutine::get_current_safe();
@@ -224,6 +262,7 @@ TEST(coroutine_base, gdb) {
224262
long cid = current->get_cid();
225263
ASSERT_EQ(swoole_coroutine_count(), 1);
226264
ASSERT_EQ(swoole_coroutine_get(cid), current);
265+
ASSERT_EQ(swoole_coroutine_get(999999), nullptr);
227266

228267
swoole_coroutine_iterator_reset();
229268
ASSERT_EQ(swoole_coroutine_iterator_each(), current);
@@ -234,3 +273,54 @@ TEST(coroutine_base, gdb) {
234273
Coroutine::print_list();
235274
});
236275
}
276+
277+
TEST(coroutine_base, bailout) {
278+
int status;
279+
280+
status = test::spawn_exec_and_wait([]() {
281+
std::unordered_map<std::string, bool> flags;
282+
coroutine::run([&flags](void *arg) {
283+
Coroutine::create([&flags](void *_arg) {
284+
Coroutine *current = Coroutine::get_current();
285+
current->bailout([&flags]() { flags["exit"] = true; });
286+
flags["end"] = true;
287+
});
288+
});
289+
290+
ASSERT_TRUE(flags["exit"]);
291+
ASSERT_FALSE(flags["end"]);
292+
});
293+
ASSERT_EQ(status, 0);
294+
295+
status = test::spawn_exec_and_wait([]() {
296+
std::unordered_map<std::string, bool> flags;
297+
coroutine::run([&flags](void *arg) {
298+
Coroutine *current = Coroutine::get_current();
299+
current->bailout(nullptr);
300+
flags["end"] = true;
301+
});
302+
303+
ASSERT_TRUE(flags["exit"]);
304+
ASSERT_FALSE(flags["end"]);
305+
});
306+
ASSERT_EQ(WEXITSTATUS(status), 1);
307+
308+
status = test::spawn_exec_and_wait([]() {
309+
std::unordered_map<std::string, bool> flags;
310+
coroutine::run([&flags](void *arg) {
311+
Coroutine *current = Coroutine::get_current();
312+
swoole_event_defer(
313+
[current, &flags](void *args) {
314+
flags["bailout"] = true;
315+
current->bailout(nullptr);
316+
flags["end"] = true;
317+
},
318+
nullptr);
319+
flags["exit"] = true;
320+
});
321+
322+
ASSERT_TRUE(flags["exit"]);
323+
ASSERT_TRUE(flags["end"]);
324+
});
325+
ASSERT_EQ(WEXITSTATUS(status), 0);
326+
}

core-tests/src/network/client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ TEST(client, sleep) {
184184

185185
String buf(65536);
186186

187-
auto domain = "httpbin.org";
187+
auto domain = TEST_HTTP_DOMAIN;
188188

189189
Client client(SW_SOCK_TCP, true);
190190
client.onConnect = [&domain](Client *cli) {
@@ -204,7 +204,7 @@ TEST(client, sleep) {
204204

205205
swoole_event_wait();
206206

207-
ASSERT_TRUE(buf.contains("HTTP/1.1 200 OK"));
207+
ASSERT_TRUE(buf.contains(TEST_HTTP_EXPECT));
208208
}
209209

210210
TEST(client, connect_refuse) {

core-tests/src/network/socket.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ TEST(socket, check_liveness) {
343343
static void test_socket_sync(network::Socket *sock, bool connect = true) {
344344
if (connect) {
345345
network::Address addr;
346-
ASSERT_TRUE(addr.assign("tcp://httpbin.org:80"));
346+
ASSERT_TRUE(addr.assign("tcp://" TEST_HTTP_DOMAIN ":80"));
347347
ASSERT_EQ(sock->connect(addr), 0);
348348
}
349349

350-
auto req = test::http_get_request("httpbin.org", "/get");
350+
auto req = test::http_get_request(TEST_HTTP_DOMAIN, "/get");
351351
ASSERT_EQ(sock->write_sync(req.c_str(), req.length()), req.length());
352352
ASSERT_TRUE(sock->check_liveness());
353353

@@ -362,7 +362,7 @@ static void test_socket_sync(network::Socket *sock, bool connect = true) {
362362
resp.append(buf, n);
363363
}
364364

365-
ASSERT_TRUE(resp.find("HTTP/1.1 200 OK") != resp.npos);
365+
ASSERT_TRUE(resp.find(TEST_HTTP_EXPECT) != resp.npos);
366366

367367
usleep(50000);
368368
ASSERT_FALSE(sock->check_liveness());
@@ -378,7 +378,7 @@ TEST(socket, sync) {
378378
TEST(socket, dup) {
379379
auto sock = make_socket(SW_SOCK_TCP, SW_FD_STREAM, 0);
380380
network::Address addr;
381-
ASSERT_TRUE(addr.assign("tcp://httpbin.org:80"));
381+
ASSERT_TRUE(addr.assign("tcp://" TEST_HTTP_DOMAIN ":80"));
382382
ASSERT_EQ(sock->connect(addr), 0);
383383

384384
auto sock_2 = sock->dup();
@@ -389,9 +389,27 @@ TEST(socket, dup) {
389389

390390
TEST(socket, ipv6_addr) {
391391
auto sock = make_socket(SW_SOCK_TCP6, SW_FD_STREAM, 0);
392-
swoole::network::Address addr;
392+
network::Address addr;
393393
ASSERT_TRUE(addr.assign("tcp://[::1]:12345"));
394394
ASSERT_EQ(sock->connect(addr), SW_ERR);
395395
ASSERT_EQ(errno, ECONNREFUSED);
396396
sock->free();
397397
}
398+
399+
TEST(socket, loopback_addr) {
400+
network::Address addr1;
401+
addr1.assign(SW_SOCK_TCP, "127.0.0.1", 0);
402+
ASSERT_TRUE(addr1.is_loopback_addr());
403+
404+
network::Address addr2;
405+
addr2.assign(SW_SOCK_TCP6, "::1", 0);
406+
ASSERT_TRUE(addr1.is_loopback_addr());
407+
408+
network::Address addr3;
409+
addr3.assign(SW_SOCK_TCP, "192.168.1.2", 0);
410+
ASSERT_FALSE(addr3.is_loopback_addr());
411+
412+
network::Address addr4;
413+
addr4.assign(SW_SOCK_TCP6, "192::66::88", 0);
414+
ASSERT_FALSE(addr4.is_loopback_addr());
415+
}

0 commit comments

Comments
 (0)