[PATCH 5/6] qmp.py: change event_wait to use a dict

John Snow posted 6 patches 5 years, 8 months ago
Maintainers: John Snow <jsnow@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Markus Armbruster <armbru@redhat.com>, Eduardo Habkost <ehabkost@redhat.com>, Eric Blake <eblake@redhat.com>, Cleber Rosa <crosa@redhat.com>, Max Reitz <mreitz@redhat.com>
There is a newer version of this series
[PATCH 5/6] qmp.py: change event_wait to use a dict
Posted by John Snow 5 years, 8 months ago
It's easier to work with than a list of tuples, because we can check the
keys for membership.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/qemu/machine.py        | 10 +++++-----
 tests/qemu-iotests/040        | 12 ++++++------
 tests/qemu-iotests/260        |  5 +++--
 tests/qemu-iotests/iotests.py | 16 ++++++++--------
 4 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/python/qemu/machine.py b/python/qemu/machine.py
index 183d8f3d38..748de5f322 100644
--- a/python/qemu/machine.py
+++ b/python/qemu/machine.py
@@ -476,21 +476,21 @@ def event_wait(self, name, timeout=60.0, match=None):
         timeout: QEMUMonitorProtocol.pull_event timeout parameter.
         match: Optional match criteria. See event_match for details.
         """
-        return self.events_wait([(name, match)], timeout)
+        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.
+        events: a mapping containing {name: match_criteria}.
                 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
+            name = event['event']
+            if name in events:
+                return self.event_match(event, events[name])
             return False
 
         # Search cached events
diff --git a/tests/qemu-iotests/040 b/tests/qemu-iotests/040
index 32c82b4ec6..90b59081ff 100755
--- a/tests/qemu-iotests/040
+++ b/tests/qemu-iotests/040
@@ -485,12 +485,12 @@ class TestErrorHandling(iotests.QMPTestCase):
 
     def run_job(self, expected_events, error_pauses_job=False):
         match_device = {'data': {'device': 'job0'}}
-        events = [
-            ('BLOCK_JOB_COMPLETED', match_device),
-            ('BLOCK_JOB_CANCELLED', match_device),
-            ('BLOCK_JOB_ERROR', match_device),
-            ('BLOCK_JOB_READY', match_device),
-        ]
+        events = {
+            'BLOCK_JOB_COMPLETED': match_device,
+            'BLOCK_JOB_CANCELLED': match_device,
+            'BLOCK_JOB_ERROR': match_device,
+            'BLOCK_JOB_READY': match_device,
+        }
 
         completed = False
         log = []
diff --git a/tests/qemu-iotests/260 b/tests/qemu-iotests/260
index 30c0de380d..b2fb045ddd 100755
--- a/tests/qemu-iotests/260
+++ b/tests/qemu-iotests/260
@@ -65,8 +65,9 @@ def test(persistent, restart):
 
     vm.qmp_log('block-commit', device='drive0', top=top,
                filters=[iotests.filter_qmp_testfiles])
-    ev = vm.events_wait((('BLOCK_JOB_READY', None),
-                         ('BLOCK_JOB_COMPLETED', None)))
+    ev = vm.events_wait({
+        'BLOCK_JOB_READY': None,
+        'BLOCK_JOB_COMPLETED': None })
     log(filter_qmp_event(ev))
     if (ev['event'] == 'BLOCK_JOB_COMPLETED'):
         vm.shutdown()
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index 5d2990a0e4..3390fab021 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -604,14 +604,14 @@ def run_job(self, job, auto_finalize=True, auto_dismiss=False,
         """
         match_device = {'data': {'device': job}}
         match_id = {'data': {'id': job}}
-        events = [
-            ('BLOCK_JOB_COMPLETED', match_device),
-            ('BLOCK_JOB_CANCELLED', match_device),
-            ('BLOCK_JOB_ERROR', match_device),
-            ('BLOCK_JOB_READY', match_device),
-            ('BLOCK_JOB_PENDING', match_id),
-            ('JOB_STATUS_CHANGE', match_id)
-        ]
+        events = {
+            'BLOCK_JOB_COMPLETED': match_device,
+            'BLOCK_JOB_CANCELLED': match_device,
+            'BLOCK_JOB_ERROR': match_device,
+            'BLOCK_JOB_READY': match_device,
+            'BLOCK_JOB_PENDING': match_id,
+            'JOB_STATUS_CHANGE': match_id,
+        }
         error = None
         while True:
             ev = filter_qmp_event(self.events_wait(events, timeout=wait))
-- 
2.21.1


Re: [PATCH 5/6] qmp.py: change event_wait to use a dict
Posted by Vladimir Sementsov-Ogievskiy 5 years, 8 months ago
25.02.2020 3:56, John Snow wrote:
> It's easier to work with than a list of tuples, because we can check the
> keys for membership.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>   python/qemu/machine.py        | 10 +++++-----
>   tests/qemu-iotests/040        | 12 ++++++------
>   tests/qemu-iotests/260        |  5 +++--
>   tests/qemu-iotests/iotests.py | 16 ++++++++--------
>   4 files changed, 22 insertions(+), 21 deletions(-)
> 
> diff --git a/python/qemu/machine.py b/python/qemu/machine.py
> index 183d8f3d38..748de5f322 100644
> --- a/python/qemu/machine.py
> +++ b/python/qemu/machine.py
> @@ -476,21 +476,21 @@ def event_wait(self, name, timeout=60.0, match=None):
>           timeout: QEMUMonitorProtocol.pull_event timeout parameter.
>           match: Optional match criteria. See event_match for details.
>           """
> -        return self.events_wait([(name, match)], timeout)
> +        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.
> +        events: a mapping containing {name: match_criteria}.
>                   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
> +            name = event['event']
> +            if name in events:
> +                return self.event_match(event, events[name])
>               return False
>   
>           # Search cached events
> diff --git a/tests/qemu-iotests/040 b/tests/qemu-iotests/040
> index 32c82b4ec6..90b59081ff 100755
> --- a/tests/qemu-iotests/040
> +++ b/tests/qemu-iotests/040
> @@ -485,12 +485,12 @@ class TestErrorHandling(iotests.QMPTestCase):
>   
>       def run_job(self, expected_events, error_pauses_job=False):
>           match_device = {'data': {'device': 'job0'}}
> -        events = [
> -            ('BLOCK_JOB_COMPLETED', match_device),
> -            ('BLOCK_JOB_CANCELLED', match_device),
> -            ('BLOCK_JOB_ERROR', match_device),
> -            ('BLOCK_JOB_READY', match_device),
> -        ]
> +        events = {
> +            'BLOCK_JOB_COMPLETED': match_device,
> +            'BLOCK_JOB_CANCELLED': match_device,
> +            'BLOCK_JOB_ERROR': match_device,
> +            'BLOCK_JOB_READY': match_device,
> +        }
>   
>           completed = False
>           log = []
> diff --git a/tests/qemu-iotests/260 b/tests/qemu-iotests/260
> index 30c0de380d..b2fb045ddd 100755
> --- a/tests/qemu-iotests/260
> +++ b/tests/qemu-iotests/260
> @@ -65,8 +65,9 @@ def test(persistent, restart):
>   
>       vm.qmp_log('block-commit', device='drive0', top=top,
>                  filters=[iotests.filter_qmp_testfiles])
> -    ev = vm.events_wait((('BLOCK_JOB_READY', None),
> -                         ('BLOCK_JOB_COMPLETED', None)))
> +    ev = vm.events_wait({
> +        'BLOCK_JOB_READY': None,
> +        'BLOCK_JOB_COMPLETED': None })

may be better to keep it 2-lines. Or, otherwise, put closing "})" on a separate line too.

>       log(filter_qmp_event(ev))
>       if (ev['event'] == 'BLOCK_JOB_COMPLETED'):
>           vm.shutdown()
> diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
> index 5d2990a0e4..3390fab021 100644
> --- a/tests/qemu-iotests/iotests.py
> +++ b/tests/qemu-iotests/iotests.py
> @@ -604,14 +604,14 @@ def run_job(self, job, auto_finalize=True, auto_dismiss=False,
>           """
>           match_device = {'data': {'device': job}}
>           match_id = {'data': {'id': job}}
> -        events = [
> -            ('BLOCK_JOB_COMPLETED', match_device),
> -            ('BLOCK_JOB_CANCELLED', match_device),
> -            ('BLOCK_JOB_ERROR', match_device),
> -            ('BLOCK_JOB_READY', match_device),
> -            ('BLOCK_JOB_PENDING', match_id),
> -            ('JOB_STATUS_CHANGE', match_id)
> -        ]
> +        events = {
> +            'BLOCK_JOB_COMPLETED': match_device,
> +            'BLOCK_JOB_CANCELLED': match_device,
> +            'BLOCK_JOB_ERROR': match_device,
> +            'BLOCK_JOB_READY': match_device,
> +            'BLOCK_JOB_PENDING': match_id,
> +            'JOB_STATUS_CHANGE': match_id,
> +        }
>           error = None
>           while True:
>               ev = filter_qmp_event(self.events_wait(events, timeout=wait))
> 


Not sure that I like new interface more (neither that it is faster), but it works too:
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

-- 
Best regards,
Vladimir

Re: [PATCH 5/6] qmp.py: change event_wait to use a dict
Posted by John Snow 5 years, 8 months ago

On 2/27/20 6:25 AM, Vladimir Sementsov-Ogievskiy wrote:
> 25.02.2020 3:56, John Snow wrote:
>> It's easier to work with than a list of tuples, because we can check the
>> keys for membership.
>>
>> Signed-off-by: John Snow <jsnow@redhat.com>
>> ---
>>   python/qemu/machine.py        | 10 +++++-----
>>   tests/qemu-iotests/040        | 12 ++++++------
>>   tests/qemu-iotests/260        |  5 +++--
>>   tests/qemu-iotests/iotests.py | 16 ++++++++--------
>>   4 files changed, 22 insertions(+), 21 deletions(-)
>>
>> diff --git a/python/qemu/machine.py b/python/qemu/machine.py
>> index 183d8f3d38..748de5f322 100644
>> --- a/python/qemu/machine.py
>> +++ b/python/qemu/machine.py
>> @@ -476,21 +476,21 @@ def event_wait(self, name, timeout=60.0,
>> match=None):
>>           timeout: QEMUMonitorProtocol.pull_event timeout parameter.
>>           match: Optional match criteria. See event_match for details.
>>           """
>> -        return self.events_wait([(name, match)], timeout)
>> +        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.
>> +        events: a mapping containing {name: match_criteria}.
>>                   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
>> +            name = event['event']
>> +            if name in events:
>> +                return self.event_match(event, events[name])
>>               return False
>>             # Search cached events
>> diff --git a/tests/qemu-iotests/040 b/tests/qemu-iotests/040
>> index 32c82b4ec6..90b59081ff 100755
>> --- a/tests/qemu-iotests/040
>> +++ b/tests/qemu-iotests/040
>> @@ -485,12 +485,12 @@ class TestErrorHandling(iotests.QMPTestCase):
>>         def run_job(self, expected_events, error_pauses_job=False):
>>           match_device = {'data': {'device': 'job0'}}
>> -        events = [
>> -            ('BLOCK_JOB_COMPLETED', match_device),
>> -            ('BLOCK_JOB_CANCELLED', match_device),
>> -            ('BLOCK_JOB_ERROR', match_device),
>> -            ('BLOCK_JOB_READY', match_device),
>> -        ]
>> +        events = {
>> +            'BLOCK_JOB_COMPLETED': match_device,
>> +            'BLOCK_JOB_CANCELLED': match_device,
>> +            'BLOCK_JOB_ERROR': match_device,
>> +            'BLOCK_JOB_READY': match_device,
>> +        }
>>             completed = False
>>           log = []
>> diff --git a/tests/qemu-iotests/260 b/tests/qemu-iotests/260
>> index 30c0de380d..b2fb045ddd 100755
>> --- a/tests/qemu-iotests/260
>> +++ b/tests/qemu-iotests/260
>> @@ -65,8 +65,9 @@ def test(persistent, restart):
>>         vm.qmp_log('block-commit', device='drive0', top=top,
>>                  filters=[iotests.filter_qmp_testfiles])
>> -    ev = vm.events_wait((('BLOCK_JOB_READY', None),
>> -                         ('BLOCK_JOB_COMPLETED', None)))
>> +    ev = vm.events_wait({
>> +        'BLOCK_JOB_READY': None,
>> +        'BLOCK_JOB_COMPLETED': None })
> 
> may be better to keep it 2-lines. Or, otherwise, put closing "})" on a
> separate line too.
> 

Sure.

>>       log(filter_qmp_event(ev))
>>       if (ev['event'] == 'BLOCK_JOB_COMPLETED'):
>>           vm.shutdown()
>> diff --git a/tests/qemu-iotests/iotests.py
>> b/tests/qemu-iotests/iotests.py
>> index 5d2990a0e4..3390fab021 100644
>> --- a/tests/qemu-iotests/iotests.py
>> +++ b/tests/qemu-iotests/iotests.py
>> @@ -604,14 +604,14 @@ def run_job(self, job, auto_finalize=True,
>> auto_dismiss=False,
>>           """
>>           match_device = {'data': {'device': job}}
>>           match_id = {'data': {'id': job}}
>> -        events = [
>> -            ('BLOCK_JOB_COMPLETED', match_device),
>> -            ('BLOCK_JOB_CANCELLED', match_device),
>> -            ('BLOCK_JOB_ERROR', match_device),
>> -            ('BLOCK_JOB_READY', match_device),
>> -            ('BLOCK_JOB_PENDING', match_id),
>> -            ('JOB_STATUS_CHANGE', match_id)
>> -        ]
>> +        events = {
>> +            'BLOCK_JOB_COMPLETED': match_device,
>> +            'BLOCK_JOB_CANCELLED': match_device,
>> +            'BLOCK_JOB_ERROR': match_device,
>> +            'BLOCK_JOB_READY': match_device,
>> +            'BLOCK_JOB_PENDING': match_id,
>> +            'JOB_STATUS_CHANGE': match_id,
>> +        }
>>           error = None
>>           while True:
>>               ev = filter_qmp_event(self.events_wait(events,
>> timeout=wait))
>>
> 
> 
> Not sure that I like new interface more (neither that it is faster), but
> it works too:
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> 

It's a little bit of cross-contamination from the JobRunner series I
posted. This patch really belongs with that series instead.

It's not likely to be faster on the order of ~6 events, but logistically
it's just a little cleaner to read.

Thanks for the reviews!