diff --git a/src/base/tests/test_util.py b/src/base/tests/test_util.py index 65f2bb67..945c0aac 100644 --- a/src/base/tests/test_util.py +++ b/src/base/tests/test_util.py @@ -5,6 +5,7 @@ import unittest import uuid from ast import literal_eval +from contextlib import contextmanager from lxml import etree @@ -13,6 +14,7 @@ except ImportError: import mock +from odoo import modules from odoo.tools import mute_logger from odoo.tools.safe_eval import safe_eval @@ -31,6 +33,19 @@ NOTNOT = () if USE_ORM_DOMAIN else ("!", "!") +@contextmanager +def without_testing(): + thread = threading.current_thread() + testing = getattr(modules.module, "current_test", False) or getattr(thread, "testing", False) + try: + modules.module.current_test = False + thread.testing = False + yield + finally: + thread.testing = testing + modules.module.current_test = testing + + class TestAdaptOneDomain(UnitTestCase): def setUp(self): super(TestAdaptOneDomain, self).setUp() @@ -754,9 +769,8 @@ def test_parallel_rowcount(self): self.assertEqual(rowcount, expected) def test_parallel_rowcount_threaded(self): - threading.current_thread().testing = False - self.test_parallel_rowcount() - threading.current_thread().testing = True + with without_testing(): + self.test_parallel_rowcount() def test_parallel_execute_retry_on_serialization_failure(self): TEST_TABLE_NAME = "_upgrade_serialization_failure_test_table" @@ -786,13 +800,11 @@ def test_parallel_execute_retry_on_serialization_failure(self): ) ) - threading.current_thread().testing = False # exploded queries will generate a SerializationFailed error, causing some of the queries to be retried - with mute_logger(util.pg._logger.name, "odoo.sql_db"): + with without_testing(), mute_logger(util.pg._logger.name, "odoo.sql_db"): util.explode_execute( cr, util.format_query(cr, "DELETE FROM {}", TEST_TABLE_NAME), TEST_TABLE_NAME, bucket_size=1 ) - threading.current_thread().testing = True if hasattr(self, "_savepoint_id"): # `explode_execute` causes the cursor to be committed, losing the automatic checkpoint diff --git a/src/util/pg.py b/src/util/pg.py index dc50dce2..dc3b6783 100644 --- a/src/util/pg.py +++ b/src/util/pg.py @@ -33,10 +33,13 @@ from psycopg2 import errorcodes, sql try: + from odoo.modules import module as odoo_module from odoo.sql_db import db_connect except ImportError: from openerp.sql_db import db_connect + odoo_module = None + from .exceptions import MigrationError, SleepyDeveloperError from .helpers import _validate_table, model_of_table from .misc import log_progress @@ -178,6 +181,7 @@ def parallel_execute(cr, queries, logger=_logger): parallel_execute_impl = ( _parallel_execute_serial if getattr(threading.current_thread(), "testing", False) + or (odoo_module is not None and getattr(odoo_module, "current_test", False)) else _parallel_execute_threaded ) return parallel_execute_impl(cr, queries, logger=_logger)