Skip to content

Commit 73780bd

Browse files
aj-fuentesKangOl
andcommitted
[IMP] {util/pg,base/tests}: pick parallel_execute impl at runtime
If we can force a serial implementation for `parallel_execute` we can avoid unexpected `cr.commit` calls during tests. In effect, committing breaks the savepoint mechanic in tests and thus causes hard to understand test failures. Another benefit of choosing the implementation at runtime is that we can now test both implementations for correctness. closes #33 Signed-off-by: Christophe Simonis (chs) <[email protected]> Co-authored-by: "Christophe Simonis" <[email protected]>
1 parent aad0d58 commit 73780bd

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/base/tests/test_util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import operator
22
import re
3+
import threading
34
import unittest
45
import uuid
56
from ast import literal_eval
@@ -446,6 +447,11 @@ def test_parallel_rowcount(self):
446447
rowcount = util.explode_execute(cr, query, table="res_lang", bucket_size=10)
447448
self.assertEqual(rowcount, expected)
448449

450+
def test_parallel_rowcount_threaded(self):
451+
threading.current_thread().testing = False
452+
self.test_parallel_rowcount()
453+
threading.current_thread().testing = True
454+
449455
def test_create_column_with_fk(self):
450456
cr = self.env.cr
451457
self.assertFalse(util.column_exists(cr, "res_partner", "_test_lang_id"))

src/util/pg.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import re
6+
import threading
67
import time
78
import uuid
89
import warnings
@@ -61,18 +62,17 @@ def savepoint(cr):
6162
yield
6263

6364

64-
if ThreadPoolExecutor is None:
65+
def _parallel_execute_serial(cr, queries, logger=_logger):
66+
cnt = 0
67+
for query in log_progress(queries, logger, qualifier="queries", size=len(queries)):
68+
cr.execute(query)
69+
cnt += cr.rowcount
70+
return cnt
6571

66-
def parallel_execute(cr, queries, logger=_logger):
67-
cnt = 0
68-
for query in log_progress(queries, logger, qualifier="queries", size=len(queries)):
69-
cr.execute(query)
70-
cnt += cr.rowcount
71-
return cnt
7272

73-
else:
73+
if ThreadPoolExecutor is not None:
7474

75-
def parallel_execute(cr, queries, logger=_logger):
75+
def _parallel_execute_threaded(cr, queries, logger=_logger):
7676
"""
7777
Execute queries in parallel
7878
Use a maximum of 8 workers (but not more than the number of CPUs)
@@ -118,6 +118,18 @@ def execute(query):
118118
)
119119
)
120120

121+
else:
122+
_parallel_execute_threaded = _parallel_execute_serial
123+
124+
125+
def parallel_execute(cr, queries, logger=_logger):
126+
parallel_execute_impl = (
127+
_parallel_execute_serial
128+
if getattr(threading.current_thread(), "testing", False)
129+
else _parallel_execute_threaded
130+
)
131+
return parallel_execute_impl(cr, queries, logger=_logger)
132+
121133

122134
def format_query(cr, query, *args, **kwargs):
123135
"""

0 commit comments

Comments
 (0)