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