Skip to content

Commit

Permalink
swaglog: Fix random test failure (commaai#23546)
Browse files Browse the repository at this point in the history
* print info

* retry zmq_recv on errors

* get line no from __LINE__

* cleanup

* renmae msg to thread_id
  • Loading branch information
deanlee authored Jan 16, 2022
1 parent a6e8d31 commit 1221d88
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions selfdrive/common/tests/test_swaglog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
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);
}
}

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);
}

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -78,12 +85,13 @@ TEST_CASE("swaglog") {

void *zctx = zmq_ctx_new();
send_stop_msg(zctx);

std::vector<std::thread> 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);
}

0 comments on commit 1221d88

Please sign in to comment.