qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 4/5] libqtest: add a function to use a timeout when waiting fo


From: Laurent Vivier
Subject: [PATCH v3 4/5] libqtest: add a function to use a timeout when waiting for an event
Date: Thu, 18 Nov 2021 14:32:24 +0100

To be able to check we _don't_ receive a given event, we need to
be able to stop to wait for it after a given amount of time.

To do that, introduce a timeout value in qtest_qmp_eventwait().
The new version of the function is qtest_qmp_eventwait_timeout(),
that uses the new function qtest_qmp_receive_dict_timeout().

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 tests/qtest/libqos/libqtest.h | 26 +++++++++++++++++++++++-
 tests/qtest/libqtest.c        | 37 ++++++++++++++++++++++++++++++-----
 tests/unit/test-qga.c         |  2 +-
 3 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/tests/qtest/libqos/libqtest.h b/tests/qtest/libqos/libqtest.h
index 59e927119563..3f96afa5f431 100644
--- a/tests/qtest/libqos/libqtest.h
+++ b/tests/qtest/libqos/libqtest.h
@@ -209,6 +209,17 @@ void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t 
fds_num,
 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
     GCC_FMT_ATTR(2, 0);
 
+/**
+ * qtest_qmp_receive_dict_timeout:
+ * @s: #QTestState instance to operate on.
+ * @timeout: time to wait before aborting read
+ *
+ * Reads a QMP message from QEMU and returns the response.
+ * If a timeout is provided, return NULL if message is
+ * not received during the given amount of time
+ */
+QDict *qtest_qmp_receive_dict_timeout(QTestState *s, struct timeval *timeout);
+
 /**
  * qtest_qmp_receive_dict:
  * @s: #QTestState instance to operate on.
@@ -236,6 +247,19 @@ QDict *qtest_qmp_receive(QTestState *s);
  */
 void qtest_qmp_eventwait(QTestState *s, const char *event);
 
+/**
+ * qtest_qmp_eventwait_timeout:
+ * @s: #QTestState instance to operate on.
+ * @timeout: time to wait before aborting wait
+ * @event: event to wait for.
+ *
+ * Continuously polls for QMP responses until it receives the desired event
+ * or the timeout exausts.
+ * Returns a copy of the event for further investigation or NULL on timeout
+ */
+QDict *qtest_qmp_eventwait_timeout(QTestState *s, struct timeval *timeout,
+                                   const char *event);
+
 /**
  * qtest_qmp_eventwait_ref:
  * @s: #QTestState instance to operate on.
@@ -690,7 +714,7 @@ void qtest_remove_abrt_handler(void *data);
 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
-QDict *qmp_fd_receive(int fd);
+QDict *qmp_fd_receive(int fd, struct timeval *timeout);
 void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
                       const char *fmt, va_list ap) GCC_FMT_ATTR(4, 0);
 void qmp_fd_vsend(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 25aeea385bfa..10e05d0c7aa8 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -599,7 +599,7 @@ static void qmp_response(void *opaque, QObject *obj, Error 
*err)
     g_assert(qmp->response);
 }
 
-QDict *qmp_fd_receive(int fd)
+QDict *qmp_fd_receive(int fd, struct timeval *timeout)
 {
     QMPResponseParser qmp;
     bool log = getenv("QTEST_LOG") != NULL;
@@ -610,6 +610,18 @@ QDict *qmp_fd_receive(int fd)
         ssize_t len;
         char c;
 
+        if (timeout) {
+            fd_set set;
+            int ret;
+
+            FD_ZERO(&set);
+            FD_SET(fd, &set);
+            ret = select(fd + 1, &set, NULL, NULL, timeout);
+            if (ret == 0) {
+                /* timeout */
+                return NULL;
+            }
+        }
         len = read(fd, &c, 1);
         if (len == -1 && errno == EINTR) {
             continue;
@@ -643,9 +655,14 @@ QDict *qtest_qmp_receive(QTestState *s)
     }
 }
 
+QDict *qtest_qmp_receive_dict_timeout(QTestState *s, struct timeval *timeout)
+{
+    return qmp_fd_receive(s->qmp_fd, timeout);
+}
+
 QDict *qtest_qmp_receive_dict(QTestState *s)
 {
-    return qmp_fd_receive(s->qmp_fd);
+    return qtest_qmp_receive_dict_timeout(s, NULL);
 }
 
 int qtest_socket_server(const char *socket_path)
@@ -729,7 +746,7 @@ QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
 {
     qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
 
-    return qmp_fd_receive(fd);
+    return qmp_fd_receive(fd, NULL);
 }
 
 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
@@ -848,7 +865,8 @@ QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
     return NULL;
 }
 
-QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
+QDict *qtest_qmp_eventwait_timeout(QTestState *s, struct timeval *timeout,
+                                   const char *event)
 {
     QDict *response = qtest_qmp_event_ref(s, event);
 
@@ -857,7 +875,11 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char 
*event)
     }
 
     for (;;) {
-        response = qtest_qmp_receive_dict(s);
+        response = qtest_qmp_receive_dict_timeout(s, timeout);
+        if (timeout != NULL && response == NULL) {
+            /* exit on timeout */
+            return NULL;
+        }
         if ((qdict_haskey(response, "event")) &&
             (strcmp(qdict_get_str(response, "event"), event) == 0)) {
             return response;
@@ -866,6 +888,11 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char 
*event)
     }
 }
 
+QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
+{
+    return qtest_qmp_eventwait_timeout(s, NULL, event);
+}
+
 void qtest_qmp_eventwait(QTestState *s, const char *event)
 {
     QDict *response;
diff --git a/tests/unit/test-qga.c b/tests/unit/test-qga.c
index 5cb140d1b53d..22aa6405aaec 100644
--- a/tests/unit/test-qga.c
+++ b/tests/unit/test-qga.c
@@ -174,7 +174,7 @@ static void test_qga_sync_delimited(gconstpointer fix)
         g_assert_cmpint(v, ==, 1);
     } while (c != 0xff);
 
-    ret = qmp_fd_receive(fixture->fd);
+    ret = qmp_fd_receive(fixture->fd, NULL);
     g_assert_nonnull(ret);
     qmp_assert_no_error(ret);
 
-- 
2.33.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]