From patchwork Fri Nov 26 23:01:12 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 360082 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 oAQN1Nkv004693 for ; Fri, 26 Nov 2010 23:01:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752801Ab0KZXBU (ORCPT ); Fri, 26 Nov 2010 18:01:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:30540 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752031Ab0KZXBU (ORCPT ); Fri, 26 Nov 2010 18:01:20 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oAQN1Jdr030298 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 26 Nov 2010 18:01:19 -0500 Received: from freedom.redhat.com (vpn-11-239.rdu.redhat.com [10.11.11.239]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oAQN1Gsk005815; Fri, 26 Nov 2010 18:01:17 -0500 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues , Jason Wang Subject: [PATCH 1/3] KVM Test: Introduce a helper class to run a test in the background Date: Fri, 26 Nov 2010 21:01:12 -0200 Message-Id: <1290812474-21377-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 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]); Fri, 26 Nov 2010 23:01:24 +0000 (UTC) diff --git a/client/tests/kvm/kvm_test_utils.py b/client/tests/kvm/kvm_test_utils.py index e7c8d33..218bf31 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, signal +import time, os, logging, re, commands, signal, 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 @@ -564,6 +564,53 @@ def run_autotest(vm, session, control_path, timeout, outputdir): raise error.TestFail(e_msg) +class BackgroundTest(object): + """ + 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() + + def get_loss_ratio(output): """ Get the packet loss ratio from the output of ping