Skip to content

若干小改进 #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/co/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion include/co/unitest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/log/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

Expand All @@ -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})
9 changes: 8 additions & 1 deletion test/mem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*> v(N);
Expand Down Expand Up @@ -67,6 +69,7 @@ void test_fun(int id) {

COUT << "thread " << id << ":\n" << s;
v.reset();
wg.done();
}

void test_string() {
Expand Down Expand Up @@ -228,6 +231,8 @@ void test_xfree() {
co::sleep(1);
}
}

wg.done();
}

int main(int argc, char** argv) {
Expand All @@ -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;
}
11 changes: 8 additions & 3 deletions test/nco.cc
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 13 additions & 3 deletions test/so/easy.cc
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 23 additions & 7 deletions test/so/echo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/so/tcp2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions test/stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ 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) {
CHECK_EQ(1 + 1, 3);
} else {
*p = 'c';
}
wg.done();
}

void b() {
Expand All @@ -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) {
Expand All @@ -34,7 +38,6 @@ int main(int argc, char** argv) {
go(c);
}

while (1) sleep::sec(1024);

wg.wait();
return 0;
}
1 change: 0 additions & 1 deletion test/tw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down