Skip to contentSkip to conversation

iotests: fix wait_until_completed()

PATCH Superseded Peter Xu 3 participants 6 messages Mar 26, 2018 – Apr 4, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 iotests: fix wait_until_completed()

If there are more than one events, wait_until_completed() might return the 2nd event even if the 1st event is JOB_COMPLETED, since the for loop will continue to run even if completed is set to True.

It never happened before, but it can be triggered when OOB is enabled due to the RESUME startup message. Fix that up by removing the boolean and make sure we return the correct event.

---
 tests/qemu-iotests/iotests.py | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
tests/qemu-iotests/iotests.py +8 -12
index b5d7945af8..11704e6583 100644
@@ -470,18 +470,14 @@ class QMPTestCase(unittest.TestCase):
 
     def wait_until_completed(self, drive='drive0', check_offset=True):
         '''Wait for a block job to finish, returning the event'''
-        completed = False
-        while not completed:
-            for event in self.vm.get_qmp_events(wait=True):
-                if event['event'] == 'BLOCK_JOB_COMPLETED':
-                    self.assert_qmp(event, 'data/device', drive)
-                    self.assert_qmp_absent(event, 'data/error')
-                    if check_offset:
-                        self.assert_qmp(event, 'data/offset', event['data']['len'])
-                    completed = True
-
-        self.assert_no_active_block_jobs()
-        return event
+        for event in self.vm.get_qmp_events(wait=True):
+            if event['event'] == 'BLOCK_JOB_COMPLETED':
+                self.assert_qmp(event, 'data/device', drive)
+                self.assert_qmp_absent(event, 'data/error')
+                if check_offset:
+                    self.assert_qmp(event, 'data/offset', event['data']['len'])
+                self.assert_no_active_block_jobs()
+                return event
 
     def wait_ready(self, drive='drive0'):
         '''Wait until a block job BLOCK_JOB_READY event'''
Signature · 1 line
2.14.3
#1 lore raw

Am 26.03.2018 um 08:11 hat Peter Xu geschrieben:

Quoted 34 lines

If there are more than one events, wait_until_completed() might return the 2nd event even if the 1st event is JOB_COMPLETED, since the for loop will continue to run even if completed is set to True.

It never happened before, but it can be triggered when OOB is enabled due to the RESUME startup message. Fix that up by removing the boolean and make sure we return the correct event.

---
 tests/qemu-iotests/iotests.py | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
tests/qemu-iotests/iotests.py +1 -12
index b5d7945af8..11704e6583 100644
@@ -470,18 +470,14 @@ class QMPTestCase(unittest.TestCase):
 
     def wait_until_completed(self, drive='drive0', check_offset=True):
         '''Wait for a block job to finish, returning the event'''
-        completed = False
-        while not completed:
-            for event in self.vm.get_qmp_events(wait=True):
-                if event['event'] == 'BLOCK_JOB_COMPLETED':
-                    self.assert_qmp(event, 'data/device', drive)
-                    self.assert_qmp_absent(event, 'data/error')
-                    if check_offset:
-                        self.assert_qmp(event, 'data/offset', event['data']['len'])
-                    completed = True
-
-        self.assert_no_active_block_jobs()
-        return event
+        for event in self.vm.get_qmp_events(wait=True):

tests/qemu-iotests/iotests.py class QMPTestCase(unittest.TestCase): · line 483

Quoted 10 lines
tests/qemu-iotests/iotests.py +7 -0
+            if event['event'] == 'BLOCK_JOB_COMPLETED':
+                self.assert_qmp(event, 'data/device', drive)
+                self.assert_qmp_absent(event, 'data/error')
+                if check_offset:
+                    self.assert_qmp(event, 'data/offset', event['data']['len'])
+                self.assert_no_active_block_jobs()
+                return event
 
     def wait_ready(self, drive='drive0'):
         '''Wait until a block job BLOCK_JOB_READY event'''

If an event is pending, but it's not the expected event, won't we return None now instead of waiting for the BLOCK_JOB_COMPLETED event?

Wouldn't it be much easier to just add a 'break'?

Kevin

author
#2 lore raw
Quoted 43 lines · Kevin Wolf

Am 26.03.2018 um 08:11 hat Peter Xu geschrieben:

Quoted 42 lines

If there are more than one events, wait_until_completed() might return the 2nd event even if the 1st event is JOB_COMPLETED, since the for loop will continue to run even if completed is set to True.

It never happened before, but it can be triggered when OOB is enabled due to the RESUME startup message. Fix that up by removing the boolean and make sure we return the correct event.

---
 tests/qemu-iotests/iotests.py | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
tests/qemu-iotests/iotests.py +8 -12
index b5d7945af8..11704e6583 100644
@@ -470,18 +470,14 @@ class QMPTestCase(unittest.TestCase):
 
     def wait_until_completed(self, drive='drive0', check_offset=True):
         '''Wait for a block job to finish, returning the event'''
-        completed = False
-        while not completed:
-            for event in self.vm.get_qmp_events(wait=True):
-                if event['event'] == 'BLOCK_JOB_COMPLETED':
-                    self.assert_qmp(event, 'data/device', drive)
-                    self.assert_qmp_absent(event, 'data/error')
-                    if check_offset:
-                        self.assert_qmp(event, 'data/offset', event['data']['len'])
-                    completed = True
-
-        self.assert_no_active_block_jobs()
-        return event
+        for event in self.vm.get_qmp_events(wait=True):
+            if event['event'] == 'BLOCK_JOB_COMPLETED':
+                self.assert_qmp(event, 'data/device', drive)
+                self.assert_qmp_absent(event, 'data/error')
+                if check_offset:
+                    self.assert_qmp(event, 'data/offset', event['data']['len'])
+                self.assert_no_active_block_jobs()
+                return event
 
Quoted 5 lines

tests/qemu-iotests/iotests.py class QMPTestCase(unittest.TestCase): · line 483

Quoted 2 lines
tests/qemu-iotests/iotests.py +0 -0
     def wait_ready(self, drive='drive0'):
         '''Wait until a block job BLOCK_JOB_READY event'''

If an event is pending, but it's not the expected event, won't we return None now instead of waiting for the BLOCK_JOB_COMPLETED event?

If so, we'll return none. The patch fixes the other case when there are two events: one JOB_COMPLETED plus another (e.g., RESUME) event. When that happens, logically we should return one JOB_COMPLETED event, but the old code will return the other event (e.g., RESUME).

Quoted 2 lines

Wouldn't it be much easier to just add a 'break'?

Yes, it's the same. But IMHO those logics (e.g., the completed variable) are not really needed at all. This one is simpler.

Or do you want me to post the oneliner that you prefer?

Thanks,

Signature · 1 line
Peter Xu
#3 lore raw
Quoted 44 lines · Peter Xu
Quoted 43 lines · Kevin Wolf

Am 26.03.2018 um 08:11 hat Peter Xu geschrieben:

Quoted 42 lines

If there are more than one events, wait_until_completed() might return the 2nd event even if the 1st event is JOB_COMPLETED, since the for loop will continue to run even if completed is set to True.

It never happened before, but it can be triggered when OOB is enabled due to the RESUME startup message. Fix that up by removing the boolean and make sure we return the correct event.

---
 tests/qemu-iotests/iotests.py | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
tests/qemu-iotests/iotests.py +8 -12
index b5d7945af8..11704e6583 100644
@@ -470,18 +470,14 @@ class QMPTestCase(unittest.TestCase):
 
     def wait_until_completed(self, drive='drive0', check_offset=True):
         '''Wait for a block job to finish, returning the event'''
-        completed = False
-        while not completed:
-            for event in self.vm.get_qmp_events(wait=True):
-                if event['event'] == 'BLOCK_JOB_COMPLETED':
-                    self.assert_qmp(event, 'data/device', drive)
-                    self.assert_qmp_absent(event, 'data/error')
-                    if check_offset:
-                        self.assert_qmp(event, 'data/offset', event['data']['len'])
-                    completed = True
-
-        self.assert_no_active_block_jobs()
-        return event
+        for event in self.vm.get_qmp_events(wait=True):
+            if event['event'] == 'BLOCK_JOB_COMPLETED':
+                self.assert_qmp(event, 'data/device', drive)
+                self.assert_qmp_absent(event, 'data/error')
+                if check_offset:
+                    self.assert_qmp(event, 'data/offset', event['data']['len'])
+                self.assert_no_active_block_jobs()
+                return event
 
Quoted 7 lines
Quoted 5 lines

tests/qemu-iotests/iotests.py class QMPTestCase(unittest.TestCase): · line 483

Quoted 2 lines
tests/qemu-iotests/iotests.py +0 -0
     def wait_ready(self, drive='drive0'):
         '''Wait until a block job BLOCK_JOB_READY event'''

If an event is pending, but it's not the expected event, won't we return None now instead of waiting for the BLOCK_JOB_COMPLETED event?

If so, we'll return none.

Kevin is pointing out that this patch is broken. Previously the function waited for BLOCK_JOB_COMPLETED, even when other events were pending when we entered the function. Now it returns None and does not wait for BLOCK_JOB_COMPLETED!

Quoted 10 lines

The patch fixes the other case when there
are two events: one JOB_COMPLETED plus another (e.g., RESUME) event. When that happens, logically we should return one JOB_COMPLETED event, but the old code will return the other event (e.g., RESUME).

Quoted 2 lines

Wouldn't it be much easier to just add a 'break'?

Yes, it's the same. But IMHO those logics (e.g., the completed variable) are not really needed at all. This one is simpler.

No, the outer loop is needed so that the function waits until BLOCK_JOB_COMPLETED is received. It's not possible to do it with a single for loop.

Stefan

author
#4 lore raw
Quoted 51 lines · Stefan Hajnoczi
Quoted 50 lines · Peter Xu
Quoted 48 lines · Kevin Wolf

Am 26.03.2018 um 08:11 hat Peter Xu geschrieben:

Quoted 34 lines

If there are more than one events, wait_until_completed() might return the 2nd event even if the 1st event is JOB_COMPLETED, since the for loop will continue to run even if completed is set to True.

It never happened before, but it can be triggered when OOB is enabled due to the RESUME startup message. Fix that up by removing the boolean and make sure we return the correct event.

---
 tests/qemu-iotests/iotests.py | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
tests/qemu-iotests/iotests.py +1 -12
index b5d7945af8..11704e6583 100644
@@ -470,18 +470,14 @@ class QMPTestCase(unittest.TestCase):
 
     def wait_until_completed(self, drive='drive0', check_offset=True):
         '''Wait for a block job to finish, returning the event'''
-        completed = False
-        while not completed:
-            for event in self.vm.get_qmp_events(wait=True):
-                if event['event'] == 'BLOCK_JOB_COMPLETED':
-                    self.assert_qmp(event, 'data/device', drive)
-                    self.assert_qmp_absent(event, 'data/error')
-                    if check_offset:
-                        self.assert_qmp(event, 'data/offset', event['data']['len'])
-                    completed = True
-
-        self.assert_no_active_block_jobs()
-        return event
+        for event in self.vm.get_qmp_events(wait=True):

tests/qemu-iotests/iotests.py class QMPTestCase(unittest.TestCase): · line 483

Quoted 10 lines
tests/qemu-iotests/iotests.py +7 -0
+            if event['event'] == 'BLOCK_JOB_COMPLETED':
+                self.assert_qmp(event, 'data/device', drive)
+                self.assert_qmp_absent(event, 'data/error')
+                if check_offset:
+                    self.assert_qmp(event, 'data/offset', event['data']['len'])
+                self.assert_no_active_block_jobs()
+                return event
 
     def wait_ready(self, drive='drive0'):
         '''Wait until a block job BLOCK_JOB_READY event'''

If an event is pending, but it's not the expected event, won't we return None now instead of waiting for the BLOCK_JOB_COMPLETED event?

Quoted 6 lines
Quoted 1 line

If so, we'll return none.

Kevin is pointing out that this patch is broken. Previously the function waited for BLOCK_JOB_COMPLETED, even when other events were pending when we entered the function. Now it returns None and does not wait for BLOCK_JOB_COMPLETED!

Aha! Surely I missed that. :)

Quoted 7 lines
Quoted 7 lines

The patch fixes the other case when there
are two events: one JOB_COMPLETED plus another (e.g., RESUME) event. When that happens, logically we should return one JOB_COMPLETED event, but the old code will return the other event (e.g., RESUME).

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Wouldn't it be much easier to just add a 'break'?

Yes, it's the same. But IMHO those logics (e.g., the completed variable) are not really needed at all. This one is simpler.

No, the outer loop is needed so that the function waits until BLOCK_JOB_COMPLETED is received. It's not possible to do it with a single for loop.

Indeed. But then I would still slightly prefer removing the "completed" var:

def wait_until_completed(self, drive='drive0', check_offset=True):
'''Wait for a block job to finish, returning the event''' while True:
for event in self.vm.get_qmp_events(wait=True):
if event['event'] == 'BLOCK_JOB_COMPLETED':
self.assert_qmp(event, 'data/device', drive) self.assert_qmp_absent(event, 'data/error')
if check_offset:
self.assert_qmp(event, 'data/offset', event['data']['len'])
self.assert_no_active_block_jobs()
return event

Or a single break would work too. Do either of you have any preference? I can repost in either way. Thanks,

Signature · 1 line
Peter Xu
#5 lore raw
Quoted 34 lines · Peter Xu
Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines · Peter Xu
Quoted 1 line · Kevin Wolf

Am 26.03.2018 um 08:11 hat Peter Xu geschrieben:

The patch fixes the other case when there
are two events: one JOB_COMPLETED plus another (e.g., RESUME) event. When that happens, logically we should return one JOB_COMPLETED event, but the old code will return the other event (e.g., RESUME).

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Wouldn't it be much easier to just add a 'break'?

Yes, it's the same. But IMHO those logics (e.g., the completed variable) are not really needed at all. This one is simpler.

No, the outer loop is needed so that the function waits until BLOCK_JOB_COMPLETED is received. It's not possible to do it with a single for loop.

Indeed. But then I would still slightly prefer removing the "completed" var:

def wait_until_completed(self, drive='drive0', check_offset=True):
'''Wait for a block job to finish, returning the event''' while True:
for event in self.vm.get_qmp_events(wait=True):
if event['event'] == 'BLOCK_JOB_COMPLETED':
self.assert_qmp(event, 'data/device', drive) self.assert_qmp_absent(event, 'data/error')
if check_offset:
self.assert_qmp(event, 'data/offset', event['data']['len'])
self.assert_no_active_block_jobs()
return event

Quoted 2 lines

Or a single break would work too. Do either of you have any preference? I can repost in either way. Thanks,

Looks good to me!

Stefan