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