You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					93 lines
				
				2.7 KiB
			
		
		
			
		
	
	
					93 lines
				
				2.7 KiB
			| 
											4 years ago
										 | #include <zmq.h>
 | ||
|  | #include <iostream>
 | ||
|  | #define CATCH_CONFIG_MAIN
 | ||
|  | #include "catch2/catch.hpp"
 | ||
|  | 
 | ||
|  | #include "json11.hpp"
 | ||
| 
											4 years ago
										 | #include "common/swaglog.h"
 | ||
|  | #include "common/util.h"
 | ||
|  | #include "common/version.h"
 | ||
| 
											4 years ago
										 | #include "selfdrive/hardware/hw.h"
 | ||
|  | 
 | ||
|  | const char *SWAGLOG_ADDR = "ipc:///tmp/logmessage";
 | ||
| 
											4 years ago
										 | std::string daemon_name = "testy";
 | ||
| 
											4 years ago
										 | std::string dongle_id = "test_dongle_id";
 | ||
| 
											4 years ago
										 | int LINE_NO = 0;
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											4 years ago
										 | void log_thread(int thread_id, int msg_cnt) {
 | ||
| 
											4 years ago
										 |   for (int i = 0; i < msg_cnt; ++i) {
 | ||
| 
											4 years ago
										 |     LOGD("%d", thread_id);
 | ||
|  |     LINE_NO = __LINE__ - 1;
 | ||
| 
											4 years ago
										 |     usleep(1);
 | ||
|  |   }
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | void recv_log(int thread_cnt, int thread_msg_cnt) {
 | ||
|  |   void *zctx = zmq_ctx_new();
 | ||
| 
											4 years ago
										 |   void *sock = zmq_socket(zctx, ZMQ_PULL);
 | ||
|  |   zmq_bind(sock, SWAGLOG_ADDR);
 | ||
|  |   std::vector<int> thread_msgs(thread_cnt);
 | ||
| 
											4 years ago
										 |   int total_count = 0;
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											4 years ago
										 |   for (auto start = std::chrono::steady_clock::now(), now = start;
 | ||
|  |        now < start + std::chrono::seconds{1} && total_count < (thread_cnt * thread_msg_cnt);
 | ||
|  |        now = std::chrono::steady_clock::now()) {
 | ||
| 
											4 years ago
										 |     char buf[4096] = {};
 | ||
| 
											4 years ago
										 |     if (zmq_recv(sock, buf, sizeof(buf), ZMQ_DONTWAIT) <= 0) {
 | ||
| 
											4 years ago
										 |       if (errno == EAGAIN || errno == EINTR || errno == EFSM) continue;
 | ||
|  |       break;
 | ||
|  |     }
 | ||
| 
											4 years ago
										 | 
 | ||
|  |     REQUIRE(buf[0] == CLOUDLOG_DEBUG);
 | ||
|  |     std::string err;
 | ||
|  |     auto msg = json11::Json::parse(buf + 1, err);
 | ||
|  |     REQUIRE(!msg.is_null());
 | ||
|  | 
 | ||
|  |     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");
 | ||
| 
											4 years ago
										 |     REQUIRE(msg["lineno"].int_value() == LINE_NO);
 | ||
| 
											4 years ago
										 | 
 | ||
|  |     auto ctx = msg["ctx"];
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											4 years ago
										 |     REQUIRE(ctx["daemon"].string_value() == daemon_name);
 | ||
| 
											4 years ago
										 |     REQUIRE(ctx["dongle_id"].string_value() == dongle_id);
 | ||
|  |     REQUIRE(ctx["dirty"].bool_value() == true);
 | ||
| 
											4 years ago
										 | 
 | ||
|  |     REQUIRE(ctx["version"].string_value() == COMMA_VERSION);
 | ||
|  | 
 | ||
| 
											4 years ago
										 |     std::string device = "pc";
 | ||
| 
											4 years ago
										 |     if (Hardware::TICI()) {
 | ||
| 
											4 years ago
										 |       device = "tici";
 | ||
|  |     }
 | ||
|  |     REQUIRE(ctx["device"].string_value() == device);
 | ||
|  | 
 | ||
|  |     int thread_id = atoi(msg["msg"].string_value().c_str());
 | ||
|  |     REQUIRE((thread_id >= 0 && thread_id < thread_cnt));
 | ||
|  |     thread_msgs[thread_id]++;
 | ||
| 
											4 years ago
										 |     total_count++;
 | ||
| 
											4 years ago
										 |   }
 | ||
|  |   for (int i = 0; i < thread_cnt; ++i) {
 | ||
| 
											4 years ago
										 |     INFO("thread :" << i);
 | ||
| 
											4 years ago
										 |     REQUIRE(thread_msgs[i] == thread_msg_cnt);
 | ||
|  |   }
 | ||
|  |   zmq_close(sock);
 | ||
| 
											4 years ago
										 |   zmq_ctx_destroy(zctx);
 | ||
| 
											4 years ago
										 | }
 | ||
|  | 
 | ||
|  | TEST_CASE("swaglog") {
 | ||
| 
											4 years ago
										 |   setenv("MANAGER_DAEMON", daemon_name.c_str(), 1);
 | ||
| 
											4 years ago
										 |   setenv("DONGLE_ID", dongle_id.c_str(), 1);
 | ||
|  |   setenv("dirty", "1", 1);
 | ||
|  |   const int thread_cnt = 5;
 | ||
|  |   const int thread_msg_cnt = 100;
 | ||
|  | 
 | ||
|  |   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();
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											4 years ago
										 |   recv_log(thread_cnt, thread_msg_cnt);
 | ||
| 
											4 years ago
										 | }
 |