From 3db0858fb0d86fe23335d75284558992651a4009 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
Date: Mon, 25 Jan 2021 21:29:20 +0100
Subject: [PATCH] tests/virtual-device: Add a class function to wait for a
 timeout

---
 tests/virtual-device.py | 40 ++++++++++++++--------------------------
 1 file changed, 14 insertions(+), 26 deletions(-)

diff --git a/tests/virtual-device.py b/tests/virtual-device.py
index 30d9705..1f9d76c 100644
--- a/tests/virtual-device.py
+++ b/tests/virtual-device.py
@@ -94,6 +94,16 @@ class VirtualDevice(unittest.TestCase):
         self.assertFalse(self.dev.is_open())
         super().tearDown()
 
+    def wait_timeout(self, interval):
+        timeout_reached = False
+        def on_timeout():
+            nonlocal timeout_reached
+            timeout_reached = True
+
+        GLib.timeout_add(interval, on_timeout)
+        while not timeout_reached:
+            ctx.iteration(False)
+
     def send_command(self, command, *args):
         self.assertIn(command, ['INSERT', 'REMOVE', 'SCAN', 'ERROR', 'RETRY',
             'FINGER', 'UNPLUG', 'SLEEP', 'SET_ENROLL_STAGES', 'SET_SCAN_TYPE',
@@ -458,55 +468,33 @@ class VirtualDevice(unittest.TestCase):
             self.dev.close_sync()
 
     def test_device_sleep(self):
-        timeout_reached = False
-        def on_timeout():
-            nonlocal timeout_reached
-            timeout_reached = True
-
         self.send_command('SLEEP', 1500)
-        GLib.timeout_add(300, on_timeout)
 
         self.start_verify(FPrint.Print.new(self.dev),
             identify=self.dev.supports_identify())
-        while not timeout_reached:
-            ctx.iteration(False)
 
+        self.wait_timeout(300)
         self.assertFalse(self._verify_completed)
 
-        timeout_reached = False
         self._cancellable.cancel()
-        GLib.timeout_add(200, on_timeout)
-
-        while not timeout_reached:
-            ctx.iteration(False)
+        self.wait_timeout(200)
 
         self.assertTrue(self._verify_completed)
         self.cancel_verify()
 
     def test_device_sleep_on_cancellation(self):
-        timeout_reached = False
-        def on_timeout():
-            nonlocal timeout_reached
-            timeout_reached = True
-
         self.send_command('SET_CANCELLATION_ENABLED', int(False))
         self.send_command('SLEEP', 1500)
         self.send_command('SCAN', 'foo-print')
-        GLib.timeout_add(300, on_timeout)
 
         self.start_verify(FPrint.Print.new(self.dev),
             identify=self.dev.supports_identify())
-        while not timeout_reached:
-            ctx.iteration(False)
+        self.wait_timeout(300)
 
         self.assertFalse(self._verify_completed)
 
-        timeout_reached = False
         self._cancellable.cancel()
-        GLib.timeout_add(300, on_timeout)
-
-        while not timeout_reached:
-            ctx.iteration(False)
+        self.wait_timeout(300)
 
         self.assertFalse(self._verify_completed)
         self.cancel_verify()