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: 1221d8887c
commatwo_master
Dean Lee 3 years ago committed by GitHub
parent c8b79d56f5
commit 3e286dd194
  1. 20
      selfdrive/common/tests/test_swaglog.cc

@ -12,10 +12,12 @@
const char *SWAGLOG_ADDR = "ipc:///tmp/logmessage"; const char *SWAGLOG_ADDR = "ipc:///tmp/logmessage";
std::string daemon_name = "testy"; std::string daemon_name = "testy";
std::string dongle_id = "test_dongle_id"; 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) { for (int i = 0; i < msg_cnt; ++i) {
LOGD("%d", msg); LOGD("%d", thread_id);
LINE_NO = __LINE__ - 1;
usleep(1); usleep(1);
} }
} }
@ -23,7 +25,7 @@ void log_thread(int msg, int msg_cnt) {
void send_stop_msg(void *zctx) { void send_stop_msg(void *zctx) {
void *sock = zmq_socket(zctx, ZMQ_PUSH); void *sock = zmq_socket(zctx, ZMQ_PUSH);
zmq_connect(sock, SWAGLOG_ADDR); zmq_connect(sock, SWAGLOG_ADDR);
zmq_send(sock, "", 0, ZMQ_NOBLOCK); zmq_send(sock, "stop", 4, ZMQ_NOBLOCK);
zmq_close(sock); zmq_close(sock);
} }
@ -34,7 +36,11 @@ void recv_log(void *zctx, int thread_cnt, int thread_msg_cnt) {
while (true) { while (true) {
char buf[4096] = {}; 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); REQUIRE(buf[0] == CLOUDLOG_DEBUG);
std::string err; 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(msg["levelnum"].int_value() == CLOUDLOG_DEBUG);
REQUIRE_THAT(msg["filename"].string_value(), Catch::Contains("test_swaglog.cc")); REQUIRE_THAT(msg["filename"].string_value(), Catch::Contains("test_swaglog.cc"));
REQUIRE(msg["funcname"].string_value() == "log_thread"); 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"]; auto ctx = msg["ctx"];
REQUIRE(ctx["daemon"].string_value() == daemon_name); 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]++; thread_msgs[thread_id]++;
} }
for (int i = 0; i < thread_cnt; ++i) { for (int i = 0; i < thread_cnt; ++i) {
INFO("thread :" << i);
REQUIRE(thread_msgs[i] == thread_msg_cnt); REQUIRE(thread_msgs[i] == thread_msg_cnt);
} }
zmq_close(sock); zmq_close(sock);
@ -78,12 +85,13 @@ TEST_CASE("swaglog") {
void *zctx = zmq_ctx_new(); void *zctx = zmq_ctx_new();
send_stop_msg(zctx); send_stop_msg(zctx);
std::vector<std::thread> log_threads; std::vector<std::thread> log_threads;
for (int i = 0; i < thread_cnt; ++i) { for (int i = 0; i < thread_cnt; ++i) {
log_threads.push_back(std::thread(log_thread, i, thread_msg_cnt)); log_threads.push_back(std::thread(log_thread, i, thread_msg_cnt));
} }
for (auto &t : log_threads) t.join(); for (auto &t : log_threads) t.join();
recv_log(zctx, thread_cnt, thread_msg_cnt); recv_log(zctx, thread_cnt, thread_msg_cnt);
zmq_ctx_destroy(zctx); zmq_ctx_destroy(zctx);
} }

Loading…
Cancel
Save