Commit c680c691 authored by Sebastien Robin's avatar Sebastien Robin Committed by Pere Cortes

use threading.Timer instead of a sleeping thread, clean opened threads

parent 9da63815
...@@ -58,14 +58,15 @@ class ERP5TestNode(TestCase): ...@@ -58,14 +58,15 @@ class ERP5TestNode(TestCase):
os.mkdir(self.remote_repository0) os.mkdir(self.remote_repository0)
os.mkdir(self.remote_repository1) os.mkdir(self.remote_repository1)
os.mkdir(self.remote_repository2) os.mkdir(self.remote_repository2)
def log(*args,**kw):
for arg in args:
print "TESTNODE LOG : %r" % (arg,)
self.log = log
def tearDown(self): def tearDown(self):
shutil.rmtree(self._temp_dir, True) shutil.rmtree(self._temp_dir, True)
def getTestNode(self): def getTestNode(self):
def log(*args,**kw):
for arg in args:
print "TESTNODE LOG : %r" % (arg,)
# XXX how to get property the git path ? # XXX how to get property the git path ?
config = {} config = {}
config["git_binary"] = "/srv/slapgrid/slappart80/srv/runner/software/ba1e09f3364989dc92da955b64e72f8d/parts/git/bin/git" config["git_binary"] = "/srv/slapgrid/slappart80/srv/runner/software/ba1e09f3364989dc92da955b64e72f8d/parts/git/bin/git"
...@@ -77,7 +78,7 @@ class ERP5TestNode(TestCase): ...@@ -77,7 +78,7 @@ class ERP5TestNode(TestCase):
config["log_file"] = self.log_file config["log_file"] = self.log_file
config["test_suite_master_url"] = None config["test_suite_master_url"] = None
config["test_node_title"] = "Foo-Test-Node" config["test_node_title"] = "Foo-Test-Node"
return TestNode(log, config) return TestNode(self.log, config)
def getTestSuiteData(self, add_third_repository=False, reference="foo"): def getTestSuiteData(self, add_third_repository=False, reference="foo"):
data = [{ data = [{
...@@ -466,16 +467,9 @@ branch = foo ...@@ -466,16 +467,9 @@ branch = foo
time.sleep = original_sleep time.sleep = original_sleep
def test_12_spawn(self): def test_12_spawn(self):
def _log(*args,**kw):
for arg in args:
print "TESTNODE LOG : %r" % (arg,)
def _checkCorrectStatus(expected_status,*args): def _checkCorrectStatus(expected_status,*args):
result = process_manager.spawn(*args) result = process_manager.spawn(*args)
self.assertEqual(result['status_code'], expected_status) self.assertEqual(result['status_code'], expected_status)
def patch_sleep(n): process_manager = ProcessManager(log=self.log, max_timeout=1)
subprocess.check_call(["sleep", n/10000]) _checkCorrectStatus(0, *['sleep','0'])
original_sleep = time.sleep _checkCorrectStatus(-15, *['sleep','2'])
process_manager = ProcessManager(log=_log)
_checkCorrectStatus(0, *['sleep','3'])
_checkCorrectStatus(-15, *['sleep','8'])
time.sleep = original_sleep
...@@ -32,7 +32,7 @@ import signal ...@@ -32,7 +32,7 @@ import signal
import sys import sys
import time import time
MAX_TIMEOUT = 5 MAX_TIMEOUT = 36000
class SubprocessError(EnvironmentError): class SubprocessError(EnvironmentError):
def __init__(self, status_dict): def __init__(self, status_dict):
...@@ -109,13 +109,13 @@ class ProcessManager(object): ...@@ -109,13 +109,13 @@ class ProcessManager(object):
self.under_cancellation = False self.under_cancellation = False
self.p = None self.p = None
self.result = None self.result = None
self.max_timeout = kw.get("max_timeout") or MAX_TIMEOUT
def spawn(self, *args, **kw): def spawn(self, *args, **kw):
def timeoutExpired(p): def timeoutExpired(p, log):
time.sleep(MAX_TIMEOUT)
if p.poll() is None: if p.poll() is None:
log('PROCESS TOO LONG OR DEAD, GOING TO BE TERMINATED')
p.terminate() p.terminate()
raise TimeoutError
if self.under_cancellation: if self.under_cancellation:
raise CancellationError("Test Result was cancelled") raise CancellationError("Test Result was cancelled")
...@@ -137,10 +137,11 @@ class ProcessManager(object): ...@@ -137,10 +137,11 @@ class ProcessManager(object):
p = subprocess.Popen(args, stdin=self.stdin, stdout=subprocess.PIPE, p = subprocess.Popen(args, stdin=self.stdin, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env, **subprocess_kw) stderr=subprocess.PIPE, env=env, **subprocess_kw)
self.process_pid_set.add(p.pid) self.process_pid_set.add(p.pid)
thread = threading.Thread(target=timeoutExpired, args=(p,)) timer = threading.Timer(self.max_timeout, timeoutExpired, args=(p, self.log))
thread.start() timer.start()
stdout, stderr = subprocess_capture(p, self.log, log_prefix, stdout, stderr = subprocess_capture(p, self.log, log_prefix,
get_output=get_output) get_output=get_output)
timer.cancel()
result = dict(status_code=p.returncode, command=command, result = dict(status_code=p.returncode, command=command,
stdout=stdout, stderr=stderr) stdout=stdout, stderr=stderr)
self.process_pid_set.discard(p.pid) self.process_pid_set.discard(p.pid)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment