Skip to content

Commit

Permalink
Merge pull request #78 from acsone/master-fix-distributed-filestore
Browse files Browse the repository at this point in the history
Ensure filestore is shared when ran in a distributed way
  • Loading branch information
yvaucher authored Oct 8, 2024
2 parents 6e925f3 + f9bd2d8 commit f602202
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions pytest_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import ast
import os
import signal
import subprocess
import threading
from contextlib import contextmanager
from unittest import mock
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -110,6 +112,21 @@ def load_http(request):
odoo.service.server.start(stop=True)
signal.signal(signal.SIGINT, signal.default_int_handler)

@contextmanager
def _shared_filestore(original_db_name, db_name):
# This method ensure that if tests are ran in a distributed way
# we share the filestore between the original database and the
# copy of the database. This is useful to avoid copying the
# filestore for each worker.
# This is done by patching the filestore method of the odoo
# configuration to point to the original filestore.
if original_db_name == db_name:
yield
return
with mock.patch.object(odoo.tools.config, "filestore") as filestore:
fs_path = os.path.join(odoo.tools.config['data_dir'], 'filestore', original_db_name)
filestore.return_value = fs_path
yield

@contextmanager
def _worker_db_name():
Expand All @@ -122,15 +139,18 @@ def _worker_db_name():
try:
if xdist_worker:
db_name = f"{original_db_name}-{xdist_worker}"
os.system(f"dropdb {db_name} --if-exists")
os.system(f"createdb -T {original_db_name} {db_name}")
subprocess.run(["dropdb", db_name, "--if-exists"], check=True)
subprocess.run(["createdb", "-T", original_db_name, db_name], check=True)
odoo.tools.config["db_name"] = db_name
yield db_name
odoo.tools.config["dbfilter"] = f"^{db_name}$"
with _shared_filestore(original_db_name, db_name):
yield db_name
finally:
if db_name != original_db_name:
odoo.sql_db.close_db(db_name)
os.system(f"dropdb {db_name}")
subprocess.run(["dropdb", db_name, "--if-exists"], check=True)
odoo.tools.config["db_name"] = original_db_name
odoo.tools.config["dbfilter"] = f"^{original_db_name}$"


@pytest.fixture(scope='session', autouse=True)
Expand Down

0 comments on commit f602202

Please sign in to comment.