From 3ca788f558bbdd68eaec7f57266bfbd17c2370e5 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Fri, 16 Aug 2019 09:43:45 +0000 Subject: [PATCH] [IMP] tests: refactor _wait_remaining_requests After a browser_js, http requests threads are joined. If one of them doesn't finish gracefully, a dumpstacks occurs. As the dumpstacks call is in a loop, it can quickly polute logs. Even more, a the dumptack may include threads that were not yet processed and that will be joined in a future loop. With this commit, dumpstacks will be called once and for all at the end of the method, if at least one thread is remaining. Furthermore, before this commit a sleep of 0.5 sec occured at most ten times for each thread before considering it as lost. It means that each thread is benefiting of the cumumulated time of the previous ones. That's not fair. With this commit, a default timeout of 10 sec is used for all threads. The original warning is kept for each remaining request. --- odoo/tests/common.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 6485092c6bb..3611fec68b2 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -929,26 +929,26 @@ class HttpCase(TransactionCase): return self.opener.post(url, data=data, files=files, timeout=timeout, headers=headers) return self.opener.get(url, timeout=timeout, headers=headers) - def _wait_remaining_requests(self): - t0 = int(time.time()) - for thread in threading.enumerate(): - if thread.name.startswith('odoo.service.http.request.'): - join_retry_count = 10 - while thread.isAlive(): - # Need a busyloop here as thread.join() masks signals - # and would prevent the forced shutdown. - thread.join(0.05) - join_retry_count -= 1 - if join_retry_count < 0: - self._logger.warning("Stop waiting for thread %s handling request for url %s", - thread.name, getattr(thread, 'url', '')) - break - time.sleep(0.5) - t1 = int(time.time()) - if t0 != t1: - self._logger.info('remaining requests') - odoo.tools.misc.dumpstacks() - t0 = t1 + def _wait_remaining_requests(self, timeout=10): + + def get_http_request_threads(): + return [t for t in threading.enumerate() if t.name.startswith('odoo.service.http.request.')] + + start_time = time.time() + request_threads = get_http_request_threads() + self._logger.info('waiting for threads: %s', request_threads) + + for thread in request_threads: + thread.join(timeout - (time.time() - start_time)) + + request_threads = get_http_request_threads() + for thread in request_threads: + self._logger.warning("Stop waiting for thread %s handling request for url %s", + thread.name, getattr(thread, 'url', '')) + + if request_threads: + self._logger.info('remaining requests') + odoo.tools.misc.dumpstacks() def authenticate(self, user, password): # stay non-authenticated