Skip to content

Commit 71bd87f

Browse files
committed
[IMP] core: remove current_thread().testing
The flag may not be set anymore on the current thread. The way to check if we are testing is to check the `current_test` variable in modules. odoo/odoo#186884 task-4270485 closes #164 Related: odoo/enterprise#73634 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 15a098b commit 71bd87f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/base/tests/test_util.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import unittest
66
import uuid
77
from ast import literal_eval
8+
from contextlib import contextmanager
89

910
from lxml import etree
1011

@@ -13,6 +14,7 @@
1314
except ImportError:
1415
import mock
1516

17+
from odoo import modules
1618
from odoo.tools import mute_logger
1719
from odoo.tools.safe_eval import safe_eval
1820

@@ -31,6 +33,19 @@
3133
NOTNOT = () if USE_ORM_DOMAIN else ("!", "!")
3234

3335

36+
@contextmanager
37+
def without_testing():
38+
thread = threading.current_thread()
39+
testing = getattr(modules.module, "current_test", False) or getattr(thread, "testing", False)
40+
try:
41+
modules.module.current_test = False
42+
thread.testing = False
43+
yield
44+
finally:
45+
thread.testing = testing
46+
modules.module.current_test = testing
47+
48+
3449
class TestAdaptOneDomain(UnitTestCase):
3550
def setUp(self):
3651
super(TestAdaptOneDomain, self).setUp()
@@ -754,9 +769,8 @@ def test_parallel_rowcount(self):
754769
self.assertEqual(rowcount, expected)
755770

756771
def test_parallel_rowcount_threaded(self):
757-
threading.current_thread().testing = False
758-
self.test_parallel_rowcount()
759-
threading.current_thread().testing = True
772+
with without_testing():
773+
self.test_parallel_rowcount()
760774

761775
def test_parallel_execute_retry_on_serialization_failure(self):
762776
TEST_TABLE_NAME = "_upgrade_serialization_failure_test_table"
@@ -786,13 +800,11 @@ def test_parallel_execute_retry_on_serialization_failure(self):
786800
)
787801
)
788802

789-
threading.current_thread().testing = False
790803
# exploded queries will generate a SerializationFailed error, causing some of the queries to be retried
791-
with mute_logger(util.pg._logger.name, "odoo.sql_db"):
804+
with without_testing(), mute_logger(util.pg._logger.name, "odoo.sql_db"):
792805
util.explode_execute(
793806
cr, util.format_query(cr, "DELETE FROM {}", TEST_TABLE_NAME), TEST_TABLE_NAME, bucket_size=1
794807
)
795-
threading.current_thread().testing = True
796808

797809
if hasattr(self, "_savepoint_id"):
798810
# `explode_execute` causes the cursor to be committed, losing the automatic checkpoint

src/util/pg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
from psycopg2 import errorcodes, sql
3434

3535
try:
36+
from odoo.modules import module as odoo_module
3637
from odoo.sql_db import db_connect
3738
except ImportError:
3839
from openerp.sql_db import db_connect
3940

41+
odoo_module = None
42+
4043
from .exceptions import MigrationError, SleepyDeveloperError
4144
from .helpers import _validate_table, model_of_table
4245
from .misc import log_progress
@@ -178,6 +181,7 @@ def parallel_execute(cr, queries, logger=_logger):
178181
parallel_execute_impl = (
179182
_parallel_execute_serial
180183
if getattr(threading.current_thread(), "testing", False)
184+
or (odoo_module is not None and getattr(odoo_module, "current_test", False))
181185
else _parallel_execute_threaded
182186
)
183187
return parallel_execute_impl(cr, queries, logger=_logger)

0 commit comments

Comments
 (0)