|
| 1 | +# pylint: disable=broad-except |
| 2 | +import threading |
| 3 | +import time |
| 4 | +import traceback |
| 5 | +from typing import List |
1 | 6 | import pytest |
2 | 7 | from allocation import model |
3 | 8 | from allocation import unit_of_work |
| 9 | +from ..random_refs import random_sku, random_batchref, random_orderid |
4 | 10 |
|
5 | | -def insert_batch(session, ref, sku, qty, eta): |
| 11 | +def insert_batch(session, ref, sku, qty, eta, product_version=1): |
6 | 12 | session.execute( |
7 | | - 'INSERT INTO products (sku) VALUES (:sku)', |
8 | | - dict(sku=sku), |
| 13 | + 'INSERT INTO products (sku, version_number) VALUES (:sku, :version)', |
| 14 | + dict(sku=sku, version=product_version), |
9 | 15 | ) |
10 | 16 | session.execute( |
11 | 17 | 'INSERT INTO batches (reference, sku, _purchased_quantity, eta)' |
@@ -65,3 +71,53 @@ class MyException(Exception): |
65 | 71 | new_session = session_factory() |
66 | 72 | rows = list(new_session.execute('SELECT * FROM "batches"')) |
67 | 73 | assert rows == [] |
| 74 | + |
| 75 | + |
| 76 | +def try_to_allocate(orderid, sku, exceptions): |
| 77 | + line = model.OrderLine(orderid, sku, 10) |
| 78 | + try: |
| 79 | + with unit_of_work.SqlAlchemyUnitOfWork() as uow: |
| 80 | + product = uow.products.get(sku=sku) |
| 81 | + product.allocate(line) |
| 82 | + time.sleep(0.2) |
| 83 | + uow.commit() |
| 84 | + except Exception as e: |
| 85 | + print(traceback.format_exc()) |
| 86 | + exceptions.append(e) |
| 87 | + |
| 88 | + |
| 89 | +def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory): |
| 90 | + sku, batch = random_sku(), random_batchref() |
| 91 | + session = postgres_session_factory() |
| 92 | + insert_batch(session, batch, sku, 100, eta=None, product_version=1) |
| 93 | + session.commit() |
| 94 | + |
| 95 | + order1, order2 = random_orderid(1), random_orderid(2) |
| 96 | + exceptions = [] # type: List[Exception] |
| 97 | + try_to_allocate_order1 = lambda: try_to_allocate(order1, sku, exceptions) |
| 98 | + try_to_allocate_order2 = lambda: try_to_allocate(order2, sku, exceptions) |
| 99 | + thread1 = threading.Thread(target=try_to_allocate_order1) |
| 100 | + thread2 = threading.Thread(target=try_to_allocate_order2) |
| 101 | + thread1.start() |
| 102 | + thread2.start() |
| 103 | + thread1.join() |
| 104 | + thread2.join() |
| 105 | + |
| 106 | + [[version]] = session.execute( |
| 107 | + "SELECT version_number FROM products WHERE sku=:sku", |
| 108 | + dict(sku=sku), |
| 109 | + ) |
| 110 | + assert version == 2 |
| 111 | + exception = [exceptions] |
| 112 | + assert 'could not serialize access due to concurrent update' in str(exception) |
| 113 | + |
| 114 | + orders = list(session.execute( |
| 115 | + "SELECT orderid FROM allocations" |
| 116 | + " JOIN batches ON allocations.batch_id = batches.id" |
| 117 | + " JOIN order_lines ON allocations.orderline_id = order_lines.id" |
| 118 | + " WHERE order_lines.sku=:sku", |
| 119 | + dict(sku=sku), |
| 120 | + )) |
| 121 | + assert len(orders) == 1 |
| 122 | + with unit_of_work.SqlAlchemyUnitOfWork() as uow: |
| 123 | + uow.session.execute('select 1') |
0 commit comments