From b029a92a17177c7e379d04109edee9e1ec1d63ab Mon Sep 17 00:00:00 2001 From: Konstantin Shulgin <konstantin.shulgin@gmail.com> Date: Thu, 18 Aug 2011 16:42:34 +0400 Subject: [PATCH] feature 'master-valgrind': * process handle routines in test enviroment was updated. --- test/lib/server.py | 87 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/test/lib/server.py b/test/lib/server.py index b8adb7e928..b2633f1869 100644 --- a/test/lib/server.py +++ b/test/lib/server.py @@ -11,19 +11,6 @@ import daemon import glob import ConfigParser -def wait_until_connected(port): - """Wait until the server is started and accepting connections""" - - is_connected = False - while not is_connected: - try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect(("localhost", port)) - is_connected = True - sock.close() - except socket.error as e: - time.sleep(0.001) - def check_port(port): """Check if the port we're connecting to is available""" @@ -196,20 +183,18 @@ class Server(object): args = prepare_gdb(args) elif self.valgrind: args = prepare_valgrind(args, self.valgrind_log, - os.path.abspath(os.path.join(self.vardir, - self.default_suppression_name))) + os.path.abspath(os.path.join(self.vardir, + self.default_suppression_name))) if self.start_and_exit: self._start_and_exit(args) - else: - self.process = pexpect.spawn(args[0], args[1:], cwd = self.vardir) + return - # wait until the server is connectedk - wait_until_connected(self.port) + self.process = pexpect.spawn(args[0], args[1:], cwd = self.vardir) + # wait until the server is connected + self.wait_until_started() # Set is_started flag, to nicely support cleanup during an exception. self.is_started = True - with open(self.pidfile) as f: - self.pid = int(f.read()) def stop(self, silent=True): """Stop server instance. Do nothing if the server is not started, @@ -225,20 +210,18 @@ class Server(object): if self.process == None: self.kill_old_server() - else: - self.kill_server() - - if self.gdb: - self.process.expect(pexpect.EOF, timeout = 1 << 30) - else: - self.process.expect(pexpect.EOF) + return + # kill process + self.process.kill(signal.SIGTERM) + self.process.wait() + # clean-up processs flags self.is_started = False - self.pid = None + self.process = None def deploy(self, config=None, binary=None, vardir=None, mem=None, start_and_exit=None, gdb=None, valgrind=None, valgrind_sup=None, - silent=True, need_init=True): + silent=True, need_init=True): if config != None: self.config = config if binary != None: self.binary = binary if vardir != None: self.vardir = vardir @@ -267,23 +250,12 @@ class Server(object): stderr = subprocess.STDOUT).stdout.read() print output - def kill_server(self): - """Kill a server which was started correctly""" - try: - os.kill(self.pid, signal.SIGTERM) - except OSError as e: - print e - pass - def kill_old_server(self, silent=True): """Kill old server instance if it exists.""" - if os.access(self.pidfile, os.F_OK) == False: + pid = self.read_pidfile() + if pid == -1: return # Nothing to do - pid = 0 - with open(self.pidfile) as f: - pid = int(f.read()) - if not silent: print " Found old server, pid {0}, killing...".format(pid) @@ -294,3 +266,32 @@ class Server(object): except OSError: pass + def read_pidfile(self): + if os.access(self.pidfile, os.F_OK) == False: + # file is inaccessible (not exist or permission denied) + return -1 + + pid = -1 + try: + with open(self.pidfile) as f: + pid = int(f.read()) + except: + pass + return pid + + def wait_until_started(self): + """Wait until the server is started and accepting connections""" + + while (self.read_pidfile() != self.process.pid): + time.sleep(0.001) + + is_connected = False + while not is_connected: + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(("localhost", self.port)) + is_connected = True + sock.close() + except socket.error as e: + time.sleep(0.001) + -- GitLab