diff mbox series

[1/2] iotests: Replace time.clock() with Timeout

Message ID 20181120172252.17800-2-kwolf@redhat.com
State New
Headers show
Series iotests: More Python 3 fixes | expand

Commit Message

Kevin Wolf Nov. 20, 2018, 5:22 p.m. UTC
time.clock() is deprecated since Python 3.3. Current Python versions
warn that the function will be removed in Python 3.8, and those warnings
make the test case 118 fail.

Replace it with the Timeout mechanism that is compatible with both
Python 2 and 3, and makes the code even a little nicer.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 tests/qemu-iotests/118 | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

Comments

Philippe Mathieu-Daudé Nov. 20, 2018, 8:16 p.m. UTC | #1
On 20/11/18 18:22, Kevin Wolf wrote:
> time.clock() is deprecated since Python 3.3. Current Python versions
> warn that the function will be removed in Python 3.8, and those warnings
> make the test case 118 fail.
> 
> Replace it with the Timeout mechanism that is compatible with both
> Python 2 and 3, and makes the code even a little nicer.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> ---
>   tests/qemu-iotests/118 | 16 ++++++----------
>   1 file changed, 6 insertions(+), 10 deletions(-)
> 
> diff --git a/tests/qemu-iotests/118 b/tests/qemu-iotests/118
> index ff3b2ae3e7..c4f4c213ca 100755
> --- a/tests/qemu-iotests/118
> +++ b/tests/qemu-iotests/118
> @@ -53,21 +53,17 @@ class ChangeBaseClass(iotests.QMPTestCase):
>           if not self.has_real_tray:
>               return
>   
> -        timeout = time.clock() + 3
> -        while not self.has_opened and time.clock() < timeout:
> -            self.process_events()
> -        if not self.has_opened:
> -            self.fail('Timeout while waiting for the tray to open')
> +        with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
> +            while not self.has_opened:
> +                self.process_events()
>   
>       def wait_for_close(self):
>           if not self.has_real_tray:
>               return
>   
> -        timeout = time.clock() + 3
> -        while not self.has_closed and time.clock() < timeout:
> -            self.process_events()
> -        if not self.has_opened:
> -            self.fail('Timeout while waiting for the tray to close')
> +        with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
> +            while not self.has_closed:
> +                self.process_events()
>   
>   class GeneralChangeTestsBaseClass(ChangeBaseClass):
>   
>
John Snow Nov. 20, 2018, 9:36 p.m. UTC | #2
On 11/20/18 12:22 PM, Kevin Wolf wrote:
> time.clock() is deprecated since Python 3.3. Current Python versions
> warn that the function will be removed in Python 3.8, and those warnings
> make the test case 118 fail.
> 
> Replace it with the Timeout mechanism that is compatible with both
> Python 2 and 3, and makes the code even a little nicer.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  tests/qemu-iotests/118 | 16 ++++++----------
>  1 file changed, 6 insertions(+), 10 deletions(-)
> 
> diff --git a/tests/qemu-iotests/118 b/tests/qemu-iotests/118
> index ff3b2ae3e7..c4f4c213ca 100755
> --- a/tests/qemu-iotests/118
> +++ b/tests/qemu-iotests/118
> @@ -53,21 +53,17 @@ class ChangeBaseClass(iotests.QMPTestCase):
>          if not self.has_real_tray:
>              return
>  
> -        timeout = time.clock() + 3
> -        while not self.has_opened and time.clock() < timeout:
> -            self.process_events()
> -        if not self.has_opened:
> -            self.fail('Timeout while waiting for the tray to open')
> +        with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
> +            while not self.has_opened:
> +                self.process_events()
>  
>      def wait_for_close(self):
>          if not self.has_real_tray:
>              return
>  
> -        timeout = time.clock() + 3
> -        while not self.has_closed and time.clock() < timeout:
> -            self.process_events()
> -        if not self.has_opened:
> -            self.fail('Timeout while waiting for the tray to close')
> +        with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
> +            while not self.has_closed:
> +                self.process_events()
>  
>  class GeneralChangeTestsBaseClass(ChangeBaseClass):
>  
> 

I love the way that reads. Very cool!

Reviewed-by: John Snow <jsnow@redhat.com>
diff mbox series

Patch

diff --git a/tests/qemu-iotests/118 b/tests/qemu-iotests/118
index ff3b2ae3e7..c4f4c213ca 100755
--- a/tests/qemu-iotests/118
+++ b/tests/qemu-iotests/118
@@ -53,21 +53,17 @@  class ChangeBaseClass(iotests.QMPTestCase):
         if not self.has_real_tray:
             return
 
-        timeout = time.clock() + 3
-        while not self.has_opened and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to open')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
+            while not self.has_opened:
+                self.process_events()
 
     def wait_for_close(self):
         if not self.has_real_tray:
             return
 
-        timeout = time.clock() + 3
-        while not self.has_closed and time.clock() < timeout:
-            self.process_events()
-        if not self.has_opened:
-            self.fail('Timeout while waiting for the tray to close')
+        with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
+            while not self.has_closed:
+                self.process_events()
 
 class GeneralChangeTestsBaseClass(ChangeBaseClass):