| 1 |
import threading |
| 2 |
from concurrent.futures import ThreadPoolExecutor |
| 3 |
from psycopg2 import OperationalError |
| 4 |
|
| 5 |
from odoo import SUPERUSER_ID, api |
| 6 |
from odoo.modules.registry import Registry |
| 7 |
from odoo.tests import tagged |
| 8 |
from odoo.tests.common import BaseCase, get_db_name |
| 9 |
from odoo.tools import mute_logger |
| 10 |
|
| 11 |
|
| 12 |
@tagged('-standard', '-at_install', 'post_install', 'database_breaking') |
| 13 |
class TestConcurrencyPromoCode(BaseCase): |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
super().setUpClass() |
| 18 |
|
| 19 |
cls.registry = Registry(get_db_name()) |
| 20 |
|
| 21 |
with cls.registry.cursor() as cr: |
| 22 |
env = api.Environment(cr, SUPERUSER_ID, {}) |
| 23 |
|
| 24 |
cls.promo_code = "AZERTY123456" |
| 25 |
cls.promo_code_program = env['loyalty.program'].create({ |
| 26 |
'name': 'FREE FOR ONE', |
| 27 |
'program_type': 'promo_code', |
| 28 |
'limit_usage': True, |
| 29 |
'max_usage': 1, |
| 30 |
'rule_ids': [(0, 0, { |
| 31 |
'minimum_qty': 0, |
| 32 |
'code': cls.promo_code, |
| 33 |
})], |
| 34 |
'reward_ids': [(0, 0, { |
| 35 |
'reward_type': 'discount', |
| 36 |
'discount_mode': 'percent', |
| 37 |
'discount_applicability': 'order', |
| 38 |
'discount': 100.0, |
| 39 |
})] |
| 40 |
}) |
| 41 |
|
| 42 |
cls.partner_1 = env['res.partner'].create([{ |
| 43 |
'name': 'Mitchel Notadmin', |
| 44 |
'email': '[email protected]', |
| 45 |
}]) |
| 46 |
cls.partner_2 = env['res.partner'].create({ |
| 47 |
'name': 'John Smith', |
| 48 |
'email': '[email protected]', |
| 49 |
}) |
| 50 |
|
| 51 |
cls.product = env['product.product'].create({ |
| 52 |
'name': "TEST PRODUCT", |
| 53 |
'standard_price': 100, |
| 54 |
}) |
| 55 |
|
| 56 |
cls.order_partner_1 = env['sale.order'].create({'partner_id': cls.partner_1.id}) |
| 57 |
cls.order_partner_2 = env['sale.order'].create({'partner_id': cls.partner_2.id}) |
| 58 |
|
| 59 |
cls.order_lines = env['sale.order.line'].create([{ |
| 60 |
'order_id': cls.order_partner_1.id, |
| 61 |
'product_id': cls.product.id, |
| 62 |
'product_uom_qty': 1, |
| 63 |
}, { |
| 64 |
'order_id': cls.order_partner_2.id, |
| 65 |
'product_id': cls.product.id, |
| 66 |
'product_uom_qty': 3, |
| 67 |
}, |
| 68 |
]) |
| 69 |
|
| 70 |
cls._released_signal = threading.Event() |
| 71 |
|
| 72 |
cls.envs = [ |
| 73 |
api.Environment(cls.registry.cursor(), SUPERUSER_ID, {}), |
| 74 |
api.Environment(cls.registry.cursor(), SUPERUSER_ID, {}), |
| 75 |
] |
| 76 |
cr.commit() |
| 77 |
|
| 78 |
def reset(): |
| 79 |
for env in cls.envs: |
| 80 |
env.cr.close() |
| 81 |
|
| 82 |
with cls.registry.cursor() as cr: |
| 83 |
cr.execute(""" |
| 84 |
DELETE FROM loyalty_card WHERE program_id = %(program_id)s; |
| 85 |
DELETE FROM loyalty_rule WHERE program_id = %(program_id)s; |
| 86 |
DELETE FROM loyalty_reward WHERE program_id = %(program_id)s; |
| 87 |
DELETE FROM loyalty_program WHERE id = %(program_id)s; |
| 88 |
DELETE FROM sale_order_line WHERE id IN %(sol_ids)s; |
| 89 |
DELETE FROM sale_order WHERE id IN %(so_ids)s; |
| 90 |
DELETE FROM res_partner WHERE id IN %(partner_ids)s; |
| 91 |
DELETE FROM product_product WHERE id = %(product_id)s; |
| 92 |
""", { |
| 93 |
'program_id': cls.promo_code_program.id, |
| 94 |
'sol_ids': tuple(cls.order_lines.ids), |
| 95 |
'so_ids': (cls.order_partner_1.id, cls.order_partner_2.id), |
| 96 |
'partner_ids': (cls.partner_1.id, cls.partner_2.id), |
| 97 |
'product_id': cls.product.id, |
| 98 |
}) |
| 99 |
cls.addClassCleanup(reset) |
| 100 |
|
| 101 |
@mute_logger('odoo.sql_db') |
| 102 |
def test_lock_concurrent_promo_code(self): |
| 103 |
""" Test that two cursors cannot lock the same row simultaneously """ |
| 104 |
|
| 105 |
|
| 106 |
start_barrier = threading.Barrier(2) |
| 107 |
|
| 108 |
def run(env, order_id): |
| 109 |
env.cr.execute("SELECT id FROM loyalty_rule WHERE code = %s", (self.promo_code,)) |
| 110 |
self.assertTrue(env.cr.fetchone()) |
| 111 |
order = env['sale.order'].browse(order_id) |
| 112 |
|
| 113 |
|
| 114 |
start_barrier.wait() |
| 115 |
|
| 116 |
try: |
| 117 |
order._try_apply_code(self.promo_code) |
| 118 |
self._released_signal.wait(timeout=20) |
| 119 |
return True |
| 120 |
|
| 121 |
except OperationalError: |
| 122 |
self._released_signal.set() |
| 123 |
return False |
| 124 |
|
| 125 |
with ThreadPoolExecutor(max_workers=2) as executor: |
| 126 |
future_1 = executor.submit(run, self.envs[0], self.order_partner_1.id) |
| 127 |
future_2 = executor.submit(run, self.envs[1], self.order_partner_2.id) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
res_1 = future_1.result(timeout=3) |
| 132 |
res_2 = future_2.result(timeout=3) |
| 133 |
self.assertNotEqual(res_1, res_2) |