From 3e286dd1941fd800362aa34423e5d62391458cc2 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Mon, 17 Jan 2022 01:36:52 +0800 Subject: [PATCH] swaglog: Fix random test failure (#23546) * print info * retry zmq_recv on errors * get line no from __LINE__ * cleanup * renmae msg to thread_id old-commit-hash: 1221d8887c47ab4d4478984d9d79ade0070d98ab --- selfdrive/common/tests/test_swaglog.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/selfdrive/common/tests/test_swaglog.cc b/selfdrive/common/tests/test_swaglog.cc index 025ee51dbf..08a5ed6eae 100644 --- a/selfdrive/common/tests/test_swaglog.cc +++ b/selfdrive/common/tests/test_swaglog.cc @@ -12,10 +12,12 @@ const char *SWAGLOG_ADDR = "ipc:///tmp/logmessage"; std::string daemon_name = "testy"; std::string dongle_id = "test_dongle_id"; +int LINE_NO = 0; -void log_thread(int msg, int msg_cnt) { +void log_thread(int thread_id, int msg_cnt) { for (int i = 0; i < msg_cnt; ++i) { - LOGD("%d", msg); + LOGD("%d", thread_id); + LINE_NO = __LINE__ - 1; usleep(1); } } @@ -23,7 +25,7 @@ void log_thread(int msg, int msg_cnt) { void send_stop_msg(void *zctx) { void *sock = zmq_socket(zctx, ZMQ_PUSH); zmq_connect(sock, SWAGLOG_ADDR); - zmq_send(sock, "", 0, ZMQ_NOBLOCK); + zmq_send(sock, "stop", 4, ZMQ_NOBLOCK); zmq_close(sock); } @@ -34,7 +36,11 @@ void recv_log(void *zctx, int thread_cnt, int thread_msg_cnt) { while (true) { char buf[4096] = {}; - if (zmq_recv(sock, buf, sizeof(buf), 0) == 0) break; + if (zmq_recv(sock, buf, sizeof(buf), 0) <= 0) { + if (errno == EAGAIN || errno == EINTR || errno == EFSM) continue; + break; + } + if (strcmp(buf, "stop") == 0) break; REQUIRE(buf[0] == CLOUDLOG_DEBUG); std::string err; @@ -44,7 +50,7 @@ void recv_log(void *zctx, int thread_cnt, int thread_msg_cnt) { REQUIRE(msg["levelnum"].int_value() == CLOUDLOG_DEBUG); REQUIRE_THAT(msg["filename"].string_value(), Catch::Contains("test_swaglog.cc")); REQUIRE(msg["funcname"].string_value() == "log_thread"); - REQUIRE(msg["lineno"].int_value() == 18); // TODO: do this automatically + REQUIRE(msg["lineno"].int_value() == LINE_NO); auto ctx = msg["ctx"]; REQUIRE(ctx["daemon"].string_value() == daemon_name); @@ -64,6 +70,7 @@ void recv_log(void *zctx, int thread_cnt, int thread_msg_cnt) { thread_msgs[thread_id]++; } for (int i = 0; i < thread_cnt; ++i) { + INFO("thread :" << i); REQUIRE(thread_msgs[i] == thread_msg_cnt); } zmq_close(sock); @@ -78,12 +85,13 @@ TEST_CASE("swaglog") { void *zctx = zmq_ctx_new(); send_stop_msg(zctx); + std::vector log_threads; for (int i = 0; i < thread_cnt; ++i) { log_threads.push_back(std::thread(log_thread, i, thread_msg_cnt)); } - for (auto &t : log_threads) t.join(); + recv_log(zctx, thread_cnt, thread_msg_cnt); zmq_ctx_destroy(zctx); }