[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 03/20] QEMUMachine: add events_wait method
From: |
Max Reitz |
Subject: |
[Qemu-devel] [PULL 03/20] QEMUMachine: add events_wait method |
Date: |
Fri, 14 Jun 2019 15:40:04 +0200 |
From: John Snow <address@hidden>
Instead of event_wait which looks for a single event, add an events_wait
which can look for any number of events simultaneously. However, it
will still only return one at a time, whichever happens first.
Signed-off-by: John Snow <address@hidden>
Message-id: address@hidden
Reviewed-by: Max Reitz <address@hidden>
Signed-off-by: Max Reitz <address@hidden>
---
python/qemu/__init__.py | 69 +++++++++++++++++++++++++++++------------
1 file changed, 49 insertions(+), 20 deletions(-)
diff --git a/python/qemu/__init__.py b/python/qemu/__init__.py
index 81d9657ec0..98ed8a2e28 100644
--- a/python/qemu/__init__.py
+++ b/python/qemu/__init__.py
@@ -402,42 +402,71 @@ class QEMUMachine(object):
self._qmp.clear_events()
return events
- def event_wait(self, name, timeout=60.0, match=None):
+ @staticmethod
+ def event_match(event, match=None):
"""
- Wait for specified timeout on named event in QMP; optionally filter
- results by match.
+ Check if an event matches optional match criteria.
- The 'match' is checked to be a recursive subset of the 'event'; skips
- branch processing on match's value None
- {"foo": {"bar": 1}} matches {"foo": None}
- {"foo": {"bar": 1}} does not matches {"foo": {"baz": None}}
+ The match criteria takes the form of a matching subdict. The event is
+ checked to be a superset of the subdict, recursively, with matching
+ values whenever those values are not None.
+
+ Examples, with the subdict queries on the left:
+ - None matches any object.
+ - {"foo": None} matches {"foo": {"bar": 1}}
+ - {"foo": {"baz": None}} does not match {"foo": {"bar": 1}}
+ - {"foo": {"baz": 2}} matches {"foo": {"bar": 1, "baz": 2}}
"""
- def event_match(event, match=None):
- if match is None:
- return True
+ if match is None:
+ return True
- for key in match:
- if key in event:
- if isinstance(event[key], dict):
- if not event_match(event[key], match[key]):
- return False
- elif event[key] != match[key]:
+ for key in match:
+ if key in event:
+ if isinstance(event[key], dict):
+ if not QEMUMachine.event_match(event[key], match[key]):
return False
- else:
+ elif event[key] != match[key]:
return False
+ else:
+ return False
+ return True
- return True
+ def event_wait(self, name, timeout=60.0, match=None):
+ """
+ event_wait waits for and returns a named event from QMP with a timeout.
+
+ name: The event to wait for.
+ timeout: QEMUMonitorProtocol.pull_event timeout parameter.
+ match: Optional match criteria. See event_match for details.
+ """
+ return self.events_wait([(name, match)], timeout)
+
+ def events_wait(self, events, timeout=60.0):
+ """
+ events_wait waits for and returns a named event from QMP with a
timeout.
+
+ events: a sequence of (name, match_criteria) tuples.
+ The match criteria are optional and may be None.
+ See event_match for details.
+ timeout: QEMUMonitorProtocol.pull_event timeout parameter.
+ """
+ def _match(event):
+ for name, match in events:
+ if (event['event'] == name and
+ self.event_match(event, match)):
+ return True
+ return False
# Search cached events
for event in self._events:
- if (event['event'] == name) and event_match(event, match):
+ if _match(event):
self._events.remove(event)
return event
# Poll for new events
while True:
event = self._qmp.pull_event(wait=timeout)
- if (event['event'] == name) and event_match(event, match):
+ if _match(event):
return event
self._events.append(event)
--
2.21.0
- [Qemu-devel] [PULL 00/20] Block patches, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 01/20] blockdev-backup: don't check aio_context too early, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 04/20] iotests.py: rewrite run_job to be pickier, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 02/20] iotests.py: do not use infinite waits, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 06/20] event_match: always match on None value, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 03/20] QEMUMachine: add events_wait method,
Max Reitz <=
- [Qemu-devel] [PULL 09/20] hw/block/fdc: floppy command FIFO memory initialization, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 08/20] iotests: Fix intermittent failure in 219, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 05/20] iotests: add iotest 256 for testing blockdev-backup across iothread contexts, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 07/20] iotests: Filter 175's allocation information, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 10/20] iotests: restrict 254 to support only qcow2, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 11/20] qemu-img: Fix options leakage in img_rebase(), Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 12/20] qapi/block-core: Overlays are not snapshots, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 13/20] blockdev: Overlays are not snapshots, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 14/20] qemu-img: Move quiet into ImgConvertState, Max Reitz, 2019/06/14
- [Qemu-devel] [PULL 15/20] qemu-img: Add salvaging mode to convert, Max Reitz, 2019/06/14