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/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/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; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 62d6b2d6e..ee1ffd30e 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" "stack.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}) 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..9a6d15328 100644 --- a/test/so/easy.cc +++ b/test/so/easy.cc @@ -1,12 +1,22 @@ -#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"); -DEF_int32(port, 80, "http server port"); +DEF_int32(port, 8080, "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..f02f12c56 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, -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, 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,39 @@ 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.flush(); + } + 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; } 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; } 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);