From: Laurent Vivier <lvivier@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Laurent Vivier" <lvivier@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Michael Roth" <michael.roth@amd.com>,
"Juan Quintela" <quintela@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Ani Sinha" <ani@anisinha.ca>,
"Igor Mammedov" <imammedo@redhat.com>,
"Jens Freimann" <jfreimann@redhat.com>
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 [thread overview]
Message-ID: <20211118133225.324937-5-lvivier@redhat.com> (raw)
In-Reply-To: <20211118133225.324937-1-lvivier@redhat.com>
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
next prev parent reply other threads:[~2021-11-18 14:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-18 13:32 [PATCH v3 0/5] tests/qtest: add some tests for virtio-net failover Laurent Vivier
2021-11-18 13:32 ` [PATCH v3 1/5] qtest/libqos: add a function to initialize secondary PCI buses Laurent Vivier
2021-11-18 13:32 ` [PATCH v3 2/5] tests/qtest: add some tests for virtio-net failover Laurent Vivier
2021-11-18 13:32 ` [PATCH v3 3/5] failover: fix unplug pending detection Laurent Vivier
2021-11-18 13:32 ` Laurent Vivier [this message]
2021-11-18 13:32 ` [PATCH v3 5/5] tests/libqtest: update virtio-net failover test Laurent Vivier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211118133225.324937-5-lvivier@redhat.com \
--to=lvivier@redhat.com \
--cc=ani@anisinha.ca \
--cc=berrange@redhat.com \
--cc=imammedo@redhat.com \
--cc=jfreimann@redhat.com \
--cc=kraxel@redhat.com \
--cc=michael.roth@amd.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).