lcr/0010-add-call-timeout-check-macro.patch

98 lines
2.9 KiB
Diff
Raw Normal View History

From edb2ec1336f59ad8ca093f8cf2b2e092ff62575e Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Wed, 8 Mar 2023 10:33:40 +0800
Subject: [PATCH 10/15] add call timeout check macro
1. add macro for check call function timeout, and output a log;
2. add testcase for call timeout check macro;
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
tests/log_ut.cpp | 40 ++++++++++++++++++++++++++++++++++++++++
third_party/log.h | 11 +++++++++++
2 files changed, 51 insertions(+)
diff --git a/tests/log_ut.cpp b/tests/log_ut.cpp
index 8cf6599..2638e85 100644
--- a/tests/log_ut.cpp
+++ b/tests/log_ut.cpp
@@ -166,6 +166,8 @@ TEST(log_testcases, test_isula_libutils_log_enable)
DEBUG("debug log");
check_log(fd, false, false, "debug log");
isula_libutils_log_disable();
+
+ unlink(fname);
}
TEST(log_testcases, test_isula_libutils_log_prefix)
@@ -202,5 +204,43 @@ TEST(log_testcases, test_isula_libutils_log_prefix)
isula_libutils_set_log_prefix("");
INFO("fake log");
check_log(fd, true, true, default_prefix);
+
+ isula_libutils_log_disable();
+ unlink(fname);
+}
+
+static int do_sleep(int time)
+{
+ sleep(time);
+ return 1;
}
+TEST(log_testcases, test_call_check_timeout)
+{
+ struct isula_libutils_log_config tconf = {0};
+ const char *default_prefix = "iSula";
+ const char *prio = "INFO";
+ const char *fname = "/tmp/fake.fifo";
+ int fd = -1;
+ int nret;
+
+ tconf.driver = ISULA_LOG_DRIVER_FIFO;
+ tconf.priority = prio;
+ tconf.file = fname;
+ isula_libutils_log_enable(&tconf);
+
+ fd = isula_libutils_get_log_fd();
+ ASSERT_GE(fd, 0);
+
+ // not timeout, will no log
+ CALL_CHECK_TIMEOUT(2, do_sleep(1));
+ check_log(fd, false, false, default_prefix);
+
+ // timeout, should have log
+ CALL_CHECK_TIMEOUT(1, nret = do_sleep(2));
+ check_log(fd, true, true, "use 1 sec, timeout!!");
+ ASSERT_EQ(nret, 1);
+
+ isula_libutils_log_disable();
+ unlink(fname);
+}
\ No newline at end of file
diff --git a/third_party/log.h b/third_party/log.h
index 2db0d98..fad04a1 100644
--- a/third_party/log.h
+++ b/third_party/log.h
@@ -452,6 +452,17 @@ void isula_libutils_log_disable();
int isula_libutils_get_log_fd(void);
+#define CALL_CHECK_TIMEOUT(timeout, caller) \
+ do { \
+ struct timespec tbeg, tend; \
+ (void)clock_gettime(CLOCK_REALTIME, &tbeg); \
+ caller; \
+ (void)clock_gettime(CLOCK_REALTIME, &tend); \
+ if (tend.tv_sec - tbeg.tv_sec >= (timeout)) { \
+ ERROR("Call: "#caller" use %d sec, timeout!!", timeout); \
+ } \
+ } while (0)
+
#ifdef __cplusplus
}
#endif
--
2.25.1