@@ -75,10 +75,10 @@ class MyException(Exception):
7575import traceback
7676import time
7777
78- def try_to_allocate (orderid , sku , session_factory , exceptions ):
78+ def try_to_allocate (orderid , sku , exceptions ):
7979 line = model .OrderLine (orderid , sku , 10 )
8080 try :
81- with unit_of_work .start ( session_factory ) as uow :
81+ with unit_of_work .SqlAlchemyUnitOfWork ( ) as uow :
8282 product = uow .products .get (sku = sku )
8383 product .allocate (line )
8484 time .sleep (0.2 )
@@ -88,16 +88,16 @@ def try_to_allocate(orderid, sku, session_factory, exceptions):
8888 exceptions .append (e )
8989
9090
91- def test_integrity_of_concurrent_updates_to_product_version (postgres_session_factory ):
91+ def test_concurrent_updates_to_version_are_not_allowed (postgres_session_factory ):
9292 sku , batch = random_ref ('s' ), random_ref ('b' )
9393 session = postgres_session_factory ()
9494 insert_batch (session , batch , sku , 100 , eta = None , product_version = 3 )
9595 session .commit ()
9696
9797 exceptions = []
9898 o1 , o2 = random_ref ('o1' ), random_ref ('o2' )
99- target1 = lambda : try_to_allocate (o1 , sku , postgres_session_factory , exceptions )
100- target2 = lambda : try_to_allocate (o2 , sku , postgres_session_factory , exceptions )
99+ target1 = lambda : try_to_allocate (o1 , sku , exceptions )
100+ target2 = lambda : try_to_allocate (o2 , sku , exceptions )
101101 t1 = threading .Thread (target = target1 )
102102 t2 = threading .Thread (target = target2 )
103103 t1 .start ()
@@ -109,15 +109,18 @@ def test_integrity_of_concurrent_updates_to_product_version(postgres_session_fac
109109 "SELECT version_number FROM products WHERE sku=:sku" ,
110110 dict (sku = sku ),
111111 )
112- assert version == 5
113- assert exceptions == []
112+ assert version == 4
113+ exception = [exceptions ]
114+ assert 'could not serialize access due to concurrent update' in str (exception )
115+
114116 orders = list (session .execute (
115117 "SELECT orderid FROM allocations"
116118 " JOIN batches ON allocations.batch_id = batches.id"
117119 " JOIN order_lines ON allocations.orderline_id = order_lines.id"
118120 " WHERE order_lines.sku=:sku" ,
119121 dict (sku = sku ),
120122 ))
121- orders = set ([orders [0 ][0 ], orders [1 ][0 ]])
122- assert orders == {o1 , o2 }
123+ assert len (orders ) == 1
124+ with unit_of_work .SqlAlchemyUnitOfWork () as uow :
125+ uow .session .execute ('select 1' )
123126
0 commit comments