tests/qtest: add support for callback to receive QMP events

Currently code must call one of the qtest_qmp_event* functions to
fetch events. These are only usable if the immediate caller knows
the particular event they want to capture, and are only interested
in one specific event type. Adding ability to register an event
callback lets the caller capture a range of events over any period
of time.

Reviewed-by: Juan Quintela <quintela@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20230601161347.1803440-3-berrange@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2023-06-01 17:13:39 +01:00 committed by Juan Quintela
parent 28760edcd9
commit 0150e75d01
2 changed files with 57 additions and 4 deletions

View file

@ -82,6 +82,8 @@ struct QTestState
GString *rx;
QTestTransportOps ops;
GList *pending_events;
QTestQMPEventCallback eventCB;
void *eventData;
};
static GHookList abrt_hooks;
@ -703,8 +705,13 @@ QDict *qtest_qmp_receive(QTestState *s)
if (!qdict_get_try_str(response, "event")) {
return response;
}
/* Stash the event for a later consumption */
s->pending_events = g_list_append(s->pending_events, response);
if (!s->eventCB ||
!s->eventCB(s, qdict_get_str(response, "event"),
response, s->eventData)) {
/* Stash the event for a later consumption */
s->pending_events = g_list_append(s->pending_events, response);
}
}
}
@ -808,6 +815,13 @@ void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
va_end(ap);
}
void qtest_qmp_set_event_callback(QTestState *s,
QTestQMPEventCallback cb, void *opaque)
{
s->eventCB = cb;
s->eventData = opaque;
}
QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
{
while (s->pending_events) {