From 832b6d362e362a8fc79c383d1f161ecffbce4e07 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Dec 2022 17:16:54 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E7=83=A6=E4=BA=BA=E7=9A=84=E7=BC=96=E8=AF=91=E5=99=A8=20warnin?= =?UTF-8?q?g:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit coost/test/tw.cc:320:21: warning: unused variable 't' [-Wunused-variable] const char* t; coost/include/co/unitest.h:20:23: warning: field 'line' will be initialized after field 'c' [-Wreorder-ctor] : file(file), line(line), c(c), msg(std::move(msg)) { --- include/co/unitest.h | 2 +- test/tw.cc | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/include/co/unitest.h b/include/co/unitest.h index 9eeb5f707..291054927 100644 --- a/include/co/unitest.h +++ b/include/co/unitest.h @@ -17,7 +17,7 @@ namespace xx { struct Failed { Failed(const char* c, const char* file, int line, fastring&& msg) - : file(file), line(line), c(c), msg(std::move(msg)) { + : c(c), file(file), line(line), msg(std::move(msg)) { } const char* c; // case name const char* file; diff --git a/test/tw.cc b/test/tw.cc index 4b3ec7f4e..58f34332e 100644 --- a/test/tw.cc +++ b/test/tw.cc @@ -317,7 +317,6 @@ const char* p; BM_group(reverse_search) { int64 v; size_t r; - const char* t; BM_add(RQS)( v = RQS(s, p); From e2b35732c65b2882c8ba84ee1675036e60d087af Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Dec 2022 22:14:23 +0800 Subject: [PATCH 2/8] "$ cmake .. && make test" hangs on some standalone tests, seperate them from other tests --- test/CMakeLists.txt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 62d6b2d6e..834dc4997 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,10 @@ file(GLOB_RECURSE ALL_TEST_FILES "*.cc") +set(NON_STANDALONE_TESTS "http_serv.cc" "rpc.cc" "udp.cc" "ssl.cc") + foreach(TEST_FILE ${ALL_TEST_FILES}) get_filename_component(TEST_FILE_NAME ${TEST_FILE} NAME) string(REPLACE ".cc" "" TEST_TARGET ${TEST_FILE_NAME}) - message(" - ${TEST_FILE} --> ${TEST_TARGET}") add_executable(${TEST_TARGET}_test ${TEST_FILE}) @@ -13,5 +14,12 @@ foreach(TEST_FILE ${ALL_TEST_FILES}) target_link_libraries(${TEST_TARGET}_test PRIVATE co) - add_test(NAME ${TEST_TARGET}_test COMMAND ${TEST_TARGET}_test) + list(FIND {NON_STANDALONE_TESTS TEST_FILE NOT_A_TEST) + + if (${TEST_FILE_NAME} IN_LIST NON_STANDALONE_TESTS) + message(" - ${TEST_FILE} --> standalone test ${TEST_TARGET}") + else() + message(" - ${TEST_FILE} --> ${TEST_TARGET}") + add_test(NAME ${TEST_TARGET}_test COMMAND ${TEST_TARGET}_test) + endif() endforeach(TEST_FILE ${ALL_TEST_FILES}) From 1b38611100ee3953c90d6c88132207c744eba9fb Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Dec 2022 22:16:39 +0800 Subject: [PATCH 3/8] make some tests runnable by "make test" or "ctest" --- test/mem.cc | 9 ++++++++- test/nco.cc | 11 ++++++++--- test/so/easy.cc | 14 ++++++++++++-- test/so/echo.cc | 29 ++++++++++++++++++++++------- test/so/tcp2.cc | 2 +- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/test/mem.cc b/test/mem.cc index 3da8826fa..84e01ec2d 100644 --- a/test/mem.cc +++ b/test/mem.cc @@ -6,6 +6,8 @@ DEF_int32(m, 200, "m"); DEF_int32(t, 1, "thread num"); DEF_bool(xfree, false, "test xfree"); +co::WaitGroup wg; + void test_fun(int id) { int N = FLG_n; co::array v(N); @@ -67,6 +69,7 @@ void test_fun(int id) { COUT << "thread " << id << ":\n" << s; v.reset(); + wg.done(); } void test_string() { @@ -228,6 +231,8 @@ void test_xfree() { co::sleep(1); } } + + wg.done(); } int main(int argc, char** argv) { @@ -240,14 +245,16 @@ int main(int argc, char** argv) { test_unordered_map(); for (int i = 0; i < FLG_t; ++i) { + wg.add(); std::thread(test_fun, i).detach(); } } else { + wg.add(); go(test_xalloc); go(test_xfree); } - while (true) sleep::sec(8); + wg.wait(); return 0; } diff --git a/test/nco.cc b/test/nco.cc index ba9240371..4d101f444 100644 --- a/test/nco.cc +++ b/test/nco.cc @@ -1,15 +1,20 @@ -#include "co/co.h" +#include "co/all.h" DEF_uint32(n, 1000000, "coroutine number"); -DEF_uint32(t, 60, "seconds to sleep in coroutines"); +DEF_uint32(t, 1, "seconds to sleep in coroutines"); + +co::wait_group wg; DEF_main(argc, argv) { + COUT << "sleep " << FLG_t << " seconds\n"; for (int i = 0; i < FLG_n; ++i) { + wg.add(); go([](){ co::sleep(FLG_t * 1000); + wg.done(); }); } - co::sleep(1000000); + wg.wait(); return 0; } diff --git a/test/so/easy.cc b/test/so/easy.cc index a5aa7d810..bff8262ea 100644 --- a/test/so/easy.cc +++ b/test/so/easy.cc @@ -1,5 +1,4 @@ -#include "co/flag.h" -#include "co/http.h" +#include "co/all.h" DEF_string(d, ".", "root dir"); DEF_string(ip, "0.0.0.0", "http server ip"); @@ -7,6 +6,17 @@ DEF_int32(port, 80, "http server port"); int main(int argc, char** argv) { flag::init(argc, argv); + go([]() { + co::sleep(1000); + http::Client c("https://github.com"); + c.get("/"); + int http_status = c.status(); + CHECK_EQ(http_status, 200); + COUT << "http client staus: " << http_status << " OK"; + exit(0); + }); + + COUT << "http server on http://" << FLG_ip << ":" << FLG_port; so::easy(FLG_d.c_str(), FLG_ip.c_str(), FLG_port); // mum never have to worry again return 0; } diff --git a/test/so/echo.cc b/test/so/echo.cc index 2c7eb7d7b..c205672aa 100644 --- a/test/so/echo.cc +++ b/test/so/echo.cc @@ -2,12 +2,13 @@ DEF_string(ip, "127.0.0.1", "ip"); DEF_int32(port, 9988, "port"); -DEF_int32(c, 0, "client num"); +DEF_int32(c, -10000, "client num: c = 0, run server only; c > 0, run client only with |c| connections; c < 0, run both server and client, with |c| client connections."); DEF_int32(l, 4096, "message length"); -DEF_int32(t, 60, "test time in seconds"); +DEF_int32(t, 10, "test time in seconds"); void conn_cb(tcp::Connection conn) { fastream buf(FLG_l); + buf.resize(FLG_l); while (true) { int r = conn.recvn(&buf[0], FLG_l); @@ -64,24 +65,38 @@ void client_fun(int i) { int main(int argc, char** argv) { flag::init(argc, argv); - if (FLG_c <= 0) { + if (FLG_c == 0) { tcp::Server().on_connection(conn_cb).start( FLG_ip.c_str(), FLG_port ); while (true) sleep::sec(102400); } else { - g_count = (Count*) co::zalloc(sizeof(Count) * FLG_c); - for (int i = 0; i < FLG_c; ++i) { + int num = FLG_c; + if (FLG_c < 0) { + tcp::Server().on_connection(conn_cb).start( + FLG_ip.c_str(), FLG_port + ); + sleep::sec(1); + num = -FLG_c; + } + + g_count = (Count*) co::zalloc(sizeof(Count) * num); + for (int i = 0; i < num; ++i) { go(client_fun, i); } - sleep::sec(FLG_t); + COUT << "stress test the server with " << num << " tcp connections for " << FLG_t << " seconds"; + for (int i = 0; i < FLG_t; ++i) { + sleep::sec(1); + std::cerr << "."; + } + std::cerr << std::endl; atomic_store(&g_stop, true); sleep::sec(3); size_t rsum = 0; size_t ssum = 0; - for (int i = 0; i < FLG_c; ++i) { + for (int i = 0; i < num; ++i) { rsum += g_count[i].r; ssum += g_count[i].s; } diff --git a/test/so/tcp2.cc b/test/so/tcp2.cc index 35377929e..22eaeaf2f 100644 --- a/test/so/tcp2.cc +++ b/test/so/tcp2.cc @@ -135,6 +135,6 @@ int main(int argc, char** argv) { atomic_store(&g_stopped, true); delete gPool; - sleep::sec(5); + sleep::sec(1); return 0; } From 313b531f918e85bfb7cd7531d1f1c93aff5660a4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Dec 2022 23:16:09 +0800 Subject: [PATCH 4/8] improve tests --- test/stack.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/stack.cc b/test/stack.cc index 9302d656c..f03ec48ad 100644 --- a/test/stack.cc +++ b/test/stack.cc @@ -6,6 +6,8 @@ DEF_bool(t, false, "if true, run test in thread"); DEF_bool(m, false, "if true, run test in main thread"); DEF_bool(check, false, "if true, run CHECK test"); +co::WaitGroup wg; + void a() { char* p = 0; if (FLG_check) { @@ -13,6 +15,7 @@ void a() { } else { *p = 'c'; } + wg.done(); } void b() { @@ -26,6 +29,7 @@ void c() { int main(int argc, char** argv) { flag::init(argc, argv); + wg.add(); if (FLG_m) { c(); } else if (FLG_t) { @@ -34,7 +38,6 @@ int main(int argc, char** argv) { go(c); } - while (1) sleep::sec(1024); - + wg.wait(); return 0; } From 80cff85b65451999eaf40e2fb3b01e5ebc5f9fd3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Jan 2023 00:42:54 +0800 Subject: [PATCH 5/8] change the default server port from 80 to 8080. ports below 1024 are kept for super-user on unix. --- test/so/easy.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/so/easy.cc b/test/so/easy.cc index bff8262ea..9a6d15328 100644 --- a/test/so/easy.cc +++ b/test/so/easy.cc @@ -2,7 +2,7 @@ DEF_string(d, ".", "root dir"); DEF_string(ip, "0.0.0.0", "http server ip"); -DEF_int32(port, 80, "http server port"); +DEF_int32(port, 8080, "http server port"); int main(int argc, char** argv) { flag::init(argc, argv); From 7b5343e3fe3e7098849f393e5c7c992974e9486d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Jan 2023 00:44:21 +0800 Subject: [PATCH 6/8] stack.cc is actually a death test. let's leave it away. --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 834dc4997..ee1ffd30e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ file(GLOB_RECURSE ALL_TEST_FILES "*.cc") -set(NON_STANDALONE_TESTS "http_serv.cc" "rpc.cc" "udp.cc" "ssl.cc") +set(NON_STANDALONE_TESTS "http_serv.cc" "rpc.cc" "udp.cc" "ssl.cc" "stack.cc") foreach(TEST_FILE ${ALL_TEST_FILES}) get_filename_component(TEST_FILE_NAME ${TEST_FILE} NAME) From c38c1b9ca2749b8b5256e510703fbe77df7cb8f3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Jan 2023 01:33:00 +0800 Subject: [PATCH 7/8] Change the default connection num to 256. The max num of file descriptors is 1024 by default on most linux dists. To avoid open file failures, change the connection num to be less than 1024. --- test/so/echo.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/so/echo.cc b/test/so/echo.cc index c205672aa..f02f12c56 100644 --- a/test/so/echo.cc +++ b/test/so/echo.cc @@ -2,7 +2,7 @@ DEF_string(ip, "127.0.0.1", "ip"); DEF_int32(port, 9988, "port"); -DEF_int32(c, -10000, "client num: c = 0, run server only; c > 0, run client only with |c| connections; c < 0, run both server and client, with |c| client connections."); +DEF_int32(c, -256, "client num: c = 0, run server only; c > 0, run client only with |c| connections; c < 0, run both server and client, with |c| client connections."); DEF_int32(l, 4096, "message length"); DEF_int32(t, 10, "test time in seconds"); @@ -89,6 +89,7 @@ int main(int argc, char** argv) { for (int i = 0; i < FLG_t; ++i) { sleep::sec(1); std::cerr << "."; + std::cerr.flush(); } std::cerr << std::endl; atomic_store(&g_stop, true); From 9d7ce654431714d9cdc513a1e091241d521134a2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Jan 2023 01:38:05 +0800 Subject: [PATCH 8/8] improve fs api a little bit. --- include/co/fs.h | 4 ++++ src/fs.cc | 7 ++++++- src/log/log.cc | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/co/fs.h b/include/co/fs.h index 88b7d8dfb..6bf6bc091 100644 --- a/include/co/fs.h +++ b/include/co/fs.h @@ -177,8 +177,12 @@ class __coapi file { return this->write(&c, 1); } + // if open() fails, the posix errno is stored and can be used later. + int open_errno() const { return _open_errno; } + private: void* _p; + int _open_errno = 0; }; // open mode: diff --git a/src/fs.cc b/src/fs.cc index d3f8f4db2..ec51140e5 100644 --- a/src/fs.cc +++ b/src/fs.cc @@ -173,7 +173,12 @@ bool file::open(const char* path, char mode) { } p->fd = xx::open(path, mode); - return p->fd != nullfd; + if (p->fd != nullfd) { + return true; + } else { + this->_open_errno = errno; + return false; + }; } void file::close() { diff --git a/src/log/log.cc b/src/log/log.cc index 1ab1d954c..af3d7539f 100644 --- a/src/log/log.cc +++ b/src/log/log.cc @@ -654,7 +654,7 @@ fs::file& LogFile::open(const char* topic, int level, LogTime* t) { if (!_file) { s.clear(); - s << "cann't open the file: " << _path << '\n'; + s << "cann't open the file: " << _path << ", " << strerror(_file.open_errno()) << "\n"; log2stderr(s.data(), s.size()); } return _file;