Move the class virt.utils.Thread to base_utils.InterruptedThread thus it can be 
used in general utils.

Signed-off-by: Jiří Župka <jzu...@redhat.com>
---
 client/common_lib/base_utils.py                    |   65 ++++++++++++++++++
 .../kvm/tests/migration_with_file_transfer.py      |    6 +-
 client/tests/kvm/tests/migration_with_reboot.py    |    4 +-
 client/tests/kvm/tests/nic_bonding.py              |    9 ++-
 client/tests/kvm/tests/vmstop.py                   |    6 +-
 client/virt/tests/nic_promisc.py                   |    5 +-
 client/virt/tests/nicdriver_unload.py              |    4 +-
 client/virt/tests/ntttcp.py                        |    2 +-
 client/virt/virt_test_utils.py                     |    2 +-
 client/virt/virt_utils.py                          |   69 +-------------------
 10 files changed, 87 insertions(+), 85 deletions(-)

diff --git a/client/common_lib/base_utils.py b/client/common_lib/base_utils.py
index 972d18a..c40e5dc 100644
--- a/client/common_lib/base_utils.py
+++ b/client/common_lib/base_utils.py
@@ -817,6 +817,71 @@ def run_parallel(commands, timeout=None, 
ignore_status=False,
     return [bg_job.result for bg_job in bg_jobs]
 
 
+class InterruptedThread(Thread):
+    """
+    Run a function in a background thread.
+    """
+    def __init__(self, target, args=(), kwargs={}):
+        """
+        Initialize the instance.
+
+        @param target: Function to run in the thread.
+        @param args: Arguments to pass to target.
+        @param kwargs: Keyword arguments to pass to target.
+        """
+        Thread.__init__(self)
+        self._target = target
+        self._args = args
+        self._kwargs = kwargs
+
+
+    def run(self):
+        """
+        Run target (passed to the constructor).  No point in calling this
+        function directly.  Call start() to make this function run in a new
+        thread.
+        """
+        self._e = None
+        self._retval = None
+        try:
+            try:
+                self._retval = self._target(*self._args, **self._kwargs)
+            except Exception:
+                self._e = sys.exc_info()
+                raise
+        finally:
+            # Avoid circular references (start() may be called only once so
+            # it's OK to delete these)
+            del self._target, self._args, self._kwargs
+
+
+    def join(self, timeout=None, suppress_exception=False):
+        """
+        Join the thread.  If target raised an exception, re-raise it.
+        Otherwise, return the value returned by target.
+
+        @param timeout: Timeout value to pass to threading.Thread.join().
+        @param suppress_exception: If True, don't re-raise the exception.
+        """
+        Thread.join(self, timeout)
+        try:
+            if self._e:
+                if not suppress_exception:
+                    # Because the exception was raised in another thread, we
+                    # need to explicitly insert the current context into it
+                    s = error.exception_context(self._e[1])
+                    s = error.join_contexts(error.get_context(), s)
+                    error.set_exception_context(self._e[1], s)
+                    raise self._e[0], self._e[1], self._e[2]
+            else:
+                return self._retval
+        finally:
+            # Avoid circular references (join() may be called multiple times
+            # so we can't delete these)
+            self._e = None
+            self._retval = None
+
+
 @deprecated
 def run_bg(command):
     """Function deprecated. Please use BgJob class instead."""
diff --git a/client/tests/kvm/tests/migration_with_file_transfer.py 
b/client/tests/kvm/tests/migration_with_file_transfer.py
index 075148d..073b87e 100644
--- a/client/tests/kvm/tests/migration_with_file_transfer.py
+++ b/client/tests/kvm/tests/migration_with_file_transfer.py
@@ -56,13 +56,13 @@ def run_migration_with_file_transfer(test, params, env):
 
         error.context("transferring file to guest while migrating",
                       logging.info)
-        bg = virt_utils.Thread(vm.copy_files_to, (host_path, guest_path),
-                              dict(verbose=True, timeout=transfer_timeout))
+        bg = utils.InterruptedThread(vm.copy_files_to, (host_path, guest_path),
+                                 dict(verbose=True, timeout=transfer_timeout))
         run_and_migrate(bg)
 
         error.context("transferring file back to host while migrating",
                       logging.info)
-        bg = virt_utils.Thread(vm.copy_files_from,
+        bg = utils.InterruptedThread(vm.copy_files_from,
                               (guest_path, host_path_returned),
                               dict(verbose=True, timeout=transfer_timeout))
         run_and_migrate(bg)
diff --git a/client/tests/kvm/tests/migration_with_reboot.py 
b/client/tests/kvm/tests/migration_with_reboot.py
index b291a83..6ee2da5 100644
--- a/client/tests/kvm/tests/migration_with_reboot.py
+++ b/client/tests/kvm/tests/migration_with_reboot.py
@@ -1,4 +1,4 @@
-from autotest_lib.client.virt import virt_utils
+from autotest_lib.client.common_lib import utils
 
 
 def run_migration_with_reboot(test, params, env):
@@ -27,7 +27,7 @@ def run_migration_with_reboot(test, params, env):
 
     try:
         # Reboot the VM in the background
-        bg = virt_utils.Thread(vm.reboot, (session,))
+        bg = utils.InterruptedThread(vm.reboot, (session,))
         bg.start()
         try:
             while bg.isAlive():
diff --git a/client/tests/kvm/tests/nic_bonding.py 
b/client/tests/kvm/tests/nic_bonding.py
index 891bd98..d1f1732 100644
--- a/client/tests/kvm/tests/nic_bonding.py
+++ b/client/tests/kvm/tests/nic_bonding.py
@@ -1,7 +1,7 @@
 import logging, time
 from autotest_lib.client.virt.tests import file_transfer
-from autotest_lib.client.virt import virt_test_utils, virt_utils, aexpect
-from autotest_lib.client.common_lib import error
+from autotest_lib.client.virt import virt_test_utils, aexpect
+from autotest_lib.client.common_lib import error, utils
 
 
 def run_nic_bonding(test, params, env):
@@ -54,8 +54,9 @@ def run_nic_bonding(test, params, env):
         file_transfer.run_file_transfer(test, params, env)
 
         logging.info("Failover test with file transfer")
-        transfer_thread = virt_utils.Thread(file_transfer.run_file_transfer,
-                                           (test, params, env))
+        transfer_thread = utils.InterruptedThread(
+                                               file_transfer.run_file_transfer,
+                                               (test, params, env))
         try:
             transfer_thread.start()
             while transfer_thread.isAlive():
diff --git a/client/tests/kvm/tests/vmstop.py b/client/tests/kvm/tests/vmstop.py
index c9ac72b..79d2db1 100644
--- a/client/tests/kvm/tests/vmstop.py
+++ b/client/tests/kvm/tests/vmstop.py
@@ -1,7 +1,6 @@
 import logging, time, os
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.bin import utils
-from autotest_lib.client.virt import virt_utils
 
 
 def run_vmstop(test, params, env):
@@ -35,8 +34,9 @@ def run_vmstop(test, params, env):
         utils.run("dd if=/dev/zero of=/tmp/file bs=1M count=%s" % file_size)
         # Transfer file from host to guest, we didn't expect the finish of
         # transfer, we just let it to be a kind of stress in guest.
-        bg = virt_utils.Thread(vm.copy_files_to, ("/tmp/file", guest_path),
-                               dict(verbose=True, timeout=60))
+        bg = utils.InterruptedThread(vm.copy_files_to,
+                                     ("/tmp/file", guest_path),
+                                     dict(verbose=True, timeout=60))
         logging.info("Start the background transfer")
         bg.start()
 
diff --git a/client/virt/tests/nic_promisc.py b/client/virt/tests/nic_promisc.py
index 7e9a343..6b15ca5 100644
--- a/client/virt/tests/nic_promisc.py
+++ b/client/virt/tests/nic_promisc.py
@@ -26,8 +26,9 @@ def run_nic_promisc(test, params, env):
                                               vm.get_mac_address(0))
 
     try:
-        transfer_thread = virt_utils.Thread(file_transfer.run_file_transfer,
-                                           (test, params, env))
+        transfer_thread = utils.InterruptedThread(
+                                               file_transfer.run_file_transfer,
+                                               (test, params, env))
         transfer_thread.start()
         while transfer_thread.isAlive():
             session_serial.cmd("ip link set %s promisc on" % ethname)
diff --git a/client/virt/tests/nicdriver_unload.py 
b/client/virt/tests/nicdriver_unload.py
index 20a92a7..085b30d 100644
--- a/client/virt/tests/nicdriver_unload.py
+++ b/client/virt/tests/nicdriver_unload.py
@@ -42,8 +42,8 @@ def run_nicdriver_unload(test, params, env):
     try:
         threads = []
         for t in range(int(params.get("sessions_num", "10"))):
-            thread = virt_utils.Thread(file_transfer.run_file_transfer,
-                                      (test, params, env))
+            thread = utils.InterruptedThread(file_transfer.run_file_transfer,
+                                             (test, params, env))
             thread.start()
             threads.append(thread)
 
diff --git a/client/virt/tests/ntttcp.py b/client/virt/tests/ntttcp.py
index dab2e1f..83fb3b9 100644
--- a/client/virt/tests/ntttcp.py
+++ b/client/virt/tests/ntttcp.py
@@ -155,7 +155,7 @@ def run_ntttcp(test, params, env):
         return list
 
     try:
-        bg = virt_utils.Thread(receiver, ())
+        bg = utils.InterruptedThread(receiver, ())
         bg.start()
         if bg.isAlive():
             sender()
diff --git a/client/virt/virt_test_utils.py b/client/virt/virt_test_utils.py
index f4f3a5c..313e51c 100644
--- a/client/virt/virt_test_utils.py
+++ b/client/virt/virt_test_utils.py
@@ -607,7 +607,7 @@ def run_autotest(vm, session, control_path, timeout, 
outputdir, params):
                 mig_timeout = float(params.get("mig_timeout", "3600"))
                 mig_protocol = params.get("migration_protocol", "tcp")
 
-                bg = virt_utils.Thread(session.cmd_output,
+                bg = utils.InterruptedThread(session.cmd_output,
                                       kwargs={'cmd': "bin/autotest control",
                                               'timeout': timeout,
                                               'print_func': logging.info})
diff --git a/client/virt/virt_utils.py b/client/virt/virt_utils.py
index 16125b7..4936fa4 100644
--- a/client/virt/virt_utils.py
+++ b/client/virt/virt_utils.py
@@ -1444,71 +1444,6 @@ def archive_as_tarball(source_dir, dest_dir, 
tarball_name=None,
     tarball.close()
 
 
-class Thread(threading.Thread):
-    """
-    Run a function in a background thread.
-    """
-    def __init__(self, target, args=(), kwargs={}):
-        """
-        Initialize the instance.
-
-        @param target: Function to run in the thread.
-        @param args: Arguments to pass to target.
-        @param kwargs: Keyword arguments to pass to target.
-        """
-        threading.Thread.__init__(self)
-        self._target = target
-        self._args = args
-        self._kwargs = kwargs
-
-
-    def run(self):
-        """
-        Run target (passed to the constructor).  No point in calling this
-        function directly.  Call start() to make this function run in a new
-        thread.
-        """
-        self._e = None
-        self._retval = None
-        try:
-            try:
-                self._retval = self._target(*self._args, **self._kwargs)
-            except Exception:
-                self._e = sys.exc_info()
-                raise
-        finally:
-            # Avoid circular references (start() may be called only once so
-            # it's OK to delete these)
-            del self._target, self._args, self._kwargs
-
-
-    def join(self, timeout=None, suppress_exception=False):
-        """
-        Join the thread.  If target raised an exception, re-raise it.
-        Otherwise, return the value returned by target.
-
-        @param timeout: Timeout value to pass to threading.Thread.join().
-        @param suppress_exception: If True, don't re-raise the exception.
-        """
-        threading.Thread.join(self, timeout)
-        try:
-            if self._e:
-                if not suppress_exception:
-                    # Because the exception was raised in another thread, we
-                    # need to explicitly insert the current context into it
-                    s = error.exception_context(self._e[1])
-                    s = error.join_contexts(error.get_context(), s)
-                    error.set_exception_context(self._e[1], s)
-                    raise self._e[0], self._e[1], self._e[2]
-            else:
-                return self._retval
-        finally:
-            # Avoid circular references (join() may be called multiple times
-            # so we can't delete these)
-            self._e = None
-            self._retval = None
-
-
 def parallel(targets):
     """
     Run multiple functions in parallel.
@@ -1523,9 +1458,9 @@ def parallel(targets):
     threads = []
     for target in targets:
         if isinstance(target, tuple) or isinstance(target, list):
-            t = Thread(*target)
+            t = utils.InterruptedThread(*target)
         else:
-            t = Thread(target)
+            t = utils.InterruptedThread(target)
         threads.append(t)
         t.start()
     return [t.join() for t in threads]
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to