public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [KVM-AUTOTEST PATCH 01/28] KVM test: introduce a helper class to run a function in the background
@ 2010-12-27 16:01 Michael Goldish
  2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 02/28] KVM test: kvm_utils.py: add a convenience function to run functions in parallel Michael Goldish
                   ` (26 more replies)
  0 siblings, 27 replies; 37+ messages in thread
From: Michael Goldish @ 2010-12-27 16:01 UTC (permalink / raw)
  To: autotest, kvm

This class allows running a function in a background thread.  If the function
raises an exception, the exception is recorded and re-raised when join() is
called.  Otherwise, join() returns the value returned by the function.

This is useful for performing multiple simple actions in parallel (such as
rebooting several VMs at once to save time).  It may also be useful for running
tests on a VM while migrating it (but then it wouldn't be safe to pass the VM
object as a parameter to this class).

Differences from Jason Wang's original patch:
- Inherit from threading.Thread instead of wrapping it.
- Record the returned value as well as the exception raised.
- When re-raising the exception, use its original traceback so the user will
  know where the original exception was raised.
- Destroy all potentially circular references (self._args and the traceback in
  self._e can refer to the thread object itself).
- Rename BackgroundTest to Thread.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Michael Goldish <mgoldish@redhat.com>
---
 client/tests/kvm/kvm_utils.py |   60 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 59 insertions(+), 1 deletions(-)

diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index 5496e06..5d79112 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -5,7 +5,7 @@ KVM test utility functions.
 """
 
 import time, string, random, socket, os, signal, re, logging, commands, cPickle
-import fcntl, shelve, ConfigParser, rss_file_transfer
+import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys
 from autotest_lib.client.bin import utils, os_dep
 from autotest_lib.client.common_lib import error, logging_config
 import kvm_subprocess
@@ -1044,6 +1044,64 @@ def get_vendor_from_pci_id(pci_id):
     return re.sub(":", " ", commands.getoutput(cmd))
 
 
+class Thread(threading.Thread):
+    """
+    Runs a test in the 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:
+                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):
+        """
+        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().
+        """
+        threading.Thread.join(self, timeout)
+        try:
+            if self._e:
+                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
+
+
 class KvmLoggingConfig(logging_config.LoggingConfig):
     """
     Used with the sole purpose of providing convenient logging setup
-- 
1.7.3.3

^ permalink raw reply related	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2010-12-28 14:23 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-27 16:01 [KVM-AUTOTEST PATCH 01/28] KVM test: introduce a helper class to run a function in the background Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 02/28] KVM test: kvm_utils.py: add a convenience function to run functions in parallel Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 03/28] KVM test: corrections to migration_with_reboot Michael Goldish
2010-12-28 12:25   ` [Autotest] " Jason Wang
2010-12-28 12:42     ` Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 04/28] KVM test: migration_with_reboot: use kvm_utils.Thread Michael Goldish
2010-12-28 12:27   ` [Autotest] " Jason Wang
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 05/28] KVM test: vmstop: " Michael Goldish
2010-12-28 12:29   ` [Autotest] " Jason Wang
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 06/28] KVM test: migration_with_file_transfer: " Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 07/28] KVM test: migration_with_file_transfer: use unique host filename Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 08/28] KVM test: migration_with_file_transfer: verify transfer correctness Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 09/28] KVM test: remove kvm_test_utils.BackgroundTest Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 10/28] KVM test: avoid printing address cache messages too often Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 11/28] KVM test: make_qemu_command(): catch IndexError when accessing self.netdev_id Michael Goldish
2010-12-28 13:23   ` [Autotest] " Jason Wang
2010-12-28 14:23     ` Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 12/28] KVM test: use VM.clone() in make_qemu_command() Michael Goldish
2010-12-28 13:13   ` Jason Wang
2010-12-28 14:17     ` Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 13/28] KVM test: don't print the contents of env before and after tests Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 14/28] KVM test: fix md5sum verification of ISO files Michael Goldish
2010-12-28 13:25   ` Jason Wang
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 15/28] KVM test: kvm_subprocess.py: increase default timeout from 30 to 60 secs Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 16/28] KVM test: whql_submission: run a user specified shell command before the test Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 17/28] KVM test: whql_submission: make the HDD tests run on a non-system drive Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 18/28] KVM test: whql: rename the whql_submission variant to whql.submission Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 19/28] KVM test: whql.submission: don't run jobs that require manual intervention Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 20/28] KVM test: whql.submission: add unclassified USB tablet tests Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 21/28] KVM test: refactor whql_submission_15.cs and whql_submission.py Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 22/28] KVM test: whql: add a network submission Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 23/28] KVM test: whql.submission: use a different submission name for each submission category Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 24/28] KVM test: whql.client_install: setup auto logon for DTMLLUAdminUser Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 25/28] KVM test: whql.submission: don't use any cdroms Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 26/28] KVM test: whql.submission: support VirtIO network tests Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 27/28] KVM test: whql.submission: reorder DeviceData Michael Goldish
2010-12-27 16:01 ` [KVM-AUTOTEST PATCH 28/28] KVM test: whql_submission.py: log in using all NICs before running tests Michael Goldish

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox