Skip to content

Commit

Permalink
[IMP] core: remove current_thread().testing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kmagusiak committed Jan 31, 2025
1 parent 946deba commit 71297e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/base/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import uuid
from ast import literal_eval
from contextlib import contextmanager

from lxml import etree

Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/util/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 71297e4

Please sign in to comment.