File tree Expand file tree Collapse file tree
cli/tests/adapters/http_grader Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3030STOCK = 3
3131CHECKOUTS = 10
3232ROUND_TRIP_S = 0.05
33- """Between reading the stock and writing it back: what a query to a database costs, and far more
34- than the spread of a burst's arrivals, so a shop that does not lock oversells every time."""
33+ """Between reading the stock and writing it back: what a query to a database costs."""
3534UNUSED_CONTROL_PORT = 9
3635EXACT = ComparatorSpec (ComparatorType .EXACT )
3736
@@ -67,11 +66,16 @@ def reservations_document() -> dict[str, Any]:
6766
6867@dataclass
6968class Shop :
70- """One item's stock, read and written in two steps with a round trip between them."""
69+ """One item's stock, read and written in two steps with a round trip between them. A shop
70+ that does not lock lets every checkout of a burst read the stock before any writes it back,
71+ so it oversells every time, however the burst's arrivals spread."""
7172
7273 locking : bool
7374 stock : int = STOCK
7475 _lock : threading .Lock = field (default_factory = threading .Lock )
76+ _all_read : threading .Barrier = field (
77+ default_factory = lambda : threading .Barrier (CHECKOUTS , timeout = GENEROUS_WAIT_S )
78+ )
7579
7680 def restock (self , _request : AppRequest ) -> AppAnswer :
7781 self .stock = STOCK
@@ -88,6 +92,8 @@ def stock_left(self, _request: AppRequest) -> AppAnswer:
8892
8993 def _check_then_update (self ) -> AppAnswer :
9094 seen = self .stock
95+ if not self .locking :
96+ self ._all_read .wait ()
9197 time .sleep (ROUND_TRIP_S )
9298 if seen <= 0 :
9399 return AppAnswer (409 , {"error" : "out of stock" })
You can’t perform that action at this time.
0 commit comments