From patchwork Tue Jan 11 13:13:33 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 471621 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 p0BDEHT8008472 for ; Tue, 11 Jan 2011 13:14:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756038Ab1AKNNo (ORCPT ); Tue, 11 Jan 2011 08:13:44 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44954 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756029Ab1AKNNl (ORCPT ); Tue, 11 Jan 2011 08:13:41 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p0BDDea6009280 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 11 Jan 2011 08:13:40 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p0BDDd0s022873; Tue, 11 Jan 2011 08:13:39 -0500 Received: from qu0061.eng.lab.tlv.redhat.com ([10.35.16.61]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p0BDD9nh025030; Tue, 11 Jan 2011 08:13:38 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH 21/26] KVM test: rss_file_transfer.py: timeout fixes Date: Tue, 11 Jan 2011 15:13:33 +0200 Message-Id: <1294751618-21631-21-git-send-email-mgoldish@redhat.com> In-Reply-To: <1294751618-21631-1-git-send-email-mgoldish@redhat.com> References: <1294751618-21631-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 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.6 (demeter1.kernel.org [140.211.167.41]); Tue, 11 Jan 2011 13:14:24 +0000 (UTC) diff --git a/client/tests/kvm/rss_file_transfer.py b/client/tests/kvm/rss_file_transfer.py index 404b72b..d907678 100755 --- a/client/tests/kvm/rss_file_transfer.py +++ b/client/tests/kvm/rss_file_transfer.py @@ -80,7 +80,7 @@ class FileTransferClient(object): Connect to a RSS (remote shell server) and transfer files. """ - def __init__(self, address, port, timeout=10): + def __init__(self, address, port, timeout=20): """ Connect to a server. @@ -116,86 +116,94 @@ class FileTransferClient(object): self._socket.close() - def _send(self, str): + def _send(self, str, timeout=60): try: + if timeout <= 0: + raise socket.timeout + self._socket.settimeout(timeout) self._socket.sendall(str) + except socket.timeout: + raise FileTransferTimeoutError("Timeout expired while sending " + "data to server") except socket.error, e: raise FileTransferSocketError("Could not send data to server", e) - def _receive(self, size, timeout=10): + def _receive(self, size, timeout=60): strs = [] end_time = time.time() + timeout - while size > 0: - try: - self._socket.settimeout(max(0.0001, end_time - time.time())) + try: + while size > 0: + timeout = end_time - time.time() + if timeout <= 0: + raise socket.timeout + self._socket.settimeout(timeout) data = self._socket.recv(size) - except socket.timeout: - raise FileTransferTimeoutError("Timeout expired while " - "receiving data from server") - except socket.error, e: - raise FileTransferSocketError("Error receiving data from " - "server", e) - if not data: - raise FileTransferProtocolError("Connection closed " - "unexpectedly") - strs.append(data) - size -= len(data) + if not data: + raise FileTransferProtocolError("Connection closed " + "unexpectedly while " + "receiving data from " + "server") + strs.append(data) + size -= len(data) + except socket.timeout: + raise FileTransferTimeoutError("Timeout expired while receiving " + "data from server") + except socket.error, e: + raise FileTransferSocketError("Error receiving data from server", + e) return "".join(strs) - def _send_packet(self, str): + def _send_packet(self, str, timeout=60): self._send(struct.pack("=I", len(str))) - self._send(str) + self._send(str, timeout) - def _receive_packet(self, timeout=10): + def _receive_packet(self, timeout=60): size = struct.unpack("=I", self._receive(4))[0] return self._receive(size, timeout) - def _send_file_chunks(self, filename, timeout=30): + def _send_file_chunks(self, filename, timeout=60): f = open(filename, "rb") try: - end_time = time.time() + timeout - while time.time() < end_time: - data = f.read(CHUNKSIZE) - try: - self._send_packet(data) - except FileTransferError, e: - e.filename = filename - raise - if len(data) < CHUNKSIZE: - break - else: - raise FileTransferTimeoutError("Timeout expired while sending " - "file %s" % filename) + try: + end_time = time.time() + timeout + while True: + data = f.read(CHUNKSIZE) + self._send_packet(data, end_time - time.time()) + if len(data) < CHUNKSIZE: + break + except FileTransferError, e: + e.filename = filename + raise finally: f.close() - def _receive_file_chunks(self, filename, timeout=30): + def _receive_file_chunks(self, filename, timeout=60): f = open(filename, "wb") try: - end_time = time.time() + timeout - while True: - try: + try: + end_time = time.time() + timeout + while True: data = self._receive_packet(end_time - time.time()) - except FileTransferError, e: - e.filename = filename - raise - f.write(data) - if len(data) < CHUNKSIZE: - break + f.write(data) + if len(data) < CHUNKSIZE: + break + except FileTransferError, e: + e.filename = filename + raise finally: f.close() - def _send_msg(self, msg, timeout=10): + def _send_msg(self, msg, timeout=60): self._send(struct.pack("=I", msg)) - def _receive_msg(self, timeout=10): + def _receive_msg(self, timeout=60): s = self._receive(4, timeout) return struct.unpack("=I", s)[0] @@ -220,7 +228,7 @@ class FileUploadClient(FileTransferClient): Connect to a RSS (remote shell server) and upload files or directory trees. """ - def __init__(self, address, port, timeout=10): + def __init__(self, address, port, timeout=20): """ Connect to a server. @@ -241,7 +249,7 @@ class FileUploadClient(FileTransferClient): if os.path.isfile(path): self._send_msg(RSS_CREATE_FILE) self._send_packet(os.path.basename(path)) - self._send_file_chunks(path, max(0, end_time - time.time())) + self._send_file_chunks(path, end_time - time.time()) elif os.path.isdir(path): self._send_msg(RSS_CREATE_DIR) self._send_packet(os.path.basename(path)) @@ -297,7 +305,7 @@ class FileUploadClient(FileTransferClient): "directories" % src_pattern) # Look for RSS_OK or RSS_ERROR - msg = self._receive_msg(max(0, end_time - time.time())) + msg = self._receive_msg(end_time - time.time()) if msg == RSS_OK: return elif msg == RSS_ERROR: @@ -317,7 +325,7 @@ class FileDownloadClient(FileTransferClient): Connect to a RSS (remote shell server) and download files or directory trees. """ - def __init__(self, address, port, timeout=10): + def __init__(self, address, port, timeout=20): """ Connect to a server. @@ -378,8 +386,7 @@ class FileDownloadClient(FileTransferClient): filename = self._receive_packet() if os.path.isdir(dst_path): dst_path = os.path.join(dst_path, filename) - self._receive_file_chunks( - dst_path, max(0, end_time - time.time())) + self._receive_file_chunks(dst_path, end_time - time.time()) dst_path = os.path.dirname(dst_path) file_count += 1 elif msg == RSS_CREATE_DIR: @@ -416,7 +423,7 @@ class FileDownloadClient(FileTransferClient): def upload(address, port, src_pattern, dst_path, timeout=60, - connect_timeout=10): + connect_timeout=20): """ Connect to server and upload files. @@ -428,7 +435,7 @@ def upload(address, port, src_pattern, dst_path, timeout=60, def download(address, port, src_pattern, dst_path, timeout=60, - connect_timeout=10): + connect_timeout=20): """ Connect to server and upload files.