From patchwork Sat Sep 25 09:36:40 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 208822 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o8P9WKw6013366 for ; Sat, 25 Sep 2010 09:32:20 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754448Ab0IYJcJ (ORCPT ); Sat, 25 Sep 2010 05:32:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:11411 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754072Ab0IYJcI (ORCPT ); Sat, 25 Sep 2010 05:32:08 -0400 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o8P9W7Yk032341 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 25 Sep 2010 05:32:07 -0400 Received: from dhcp-91-158.nay.redhat.com (dhcp-91-158.nay.redhat.com [10.66.91.158]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o8P9W5jZ000687; Sat, 25 Sep 2010 05:32:06 -0400 Subject: [PATCH 1/3] KVM Test: Introduce a helper class to run a test in the background To: lmr@redhat.com, autotest@test.kernel.org From: Jason Wang Cc: kvm@vger.kernel.org, mst@redhat.com Date: Sat, 25 Sep 2010 17:36:40 +0800 Message-ID: <20100925093640.28158.82786.stgit@dhcp-91-158.nay.redhat.com> In-Reply-To: <20100925092836.28158.64788.stgit@dhcp-91-158.nay.redhat.com> References: <20100925092836.28158.64788.stgit@dhcp-91-158.nay.redhat.com> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Sat, 25 Sep 2010 09:32:20 +0000 (UTC) diff --git a/client/tests/kvm/kvm_test_utils.py b/client/tests/kvm/kvm_test_utils.py index 5412aac..9f508b9 100644 --- a/client/tests/kvm/kvm_test_utils.py +++ b/client/tests/kvm/kvm_test_utils.py @@ -21,7 +21,7 @@ More specifically: @copyright: 2008-2009 Red Hat Inc. """ -import time, os, logging, re, commands +import time, os, logging, re, commands, threading from autotest_lib.client.common_lib import error from autotest_lib.client.bin import utils import kvm_utils, kvm_vm, kvm_subprocess, scan_results @@ -505,3 +505,45 @@ def run_autotest(vm, session, control_path, timeout, outputdir): e_msg = ("Tests %s failed during control file execution" % " ".join(bad_results)) raise error.TestFail(e_msg) + +class BackgroundTest: + """ + This class would run a test in background through a dedicated thread. + """ + + def __init__(self, func, params): + """ + Initialize the object and set a few attributes. + """ + self.thread = threading.Thread(target=self.launch, + args=(func, params)) + self.exception = None + + def launch(self, func, params): + """ + Catch and record the exception. + """ + try: + func(*params) + except Exception, e: + self.exception = e + + def start(self): + """ + Run func(params) in a dedicated thread + """ + self.thread.start() + + def join(self): + """ + Wait for the join of thread and raise its exception if any. + """ + self.thread.join() + if self.exception: + raise self.exception + + def is_alive(self): + """ + Check whether the test is still alive. + """ + return self.thread.is_alive()