Skip to content

Commit 2dbfe7d

Browse files
committed
test: make every checkout of the unlocked shop read the stock before any writes it
1 parent 8cef668 commit 2dbfe7d

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎cli/tests/adapters/http_grader/test_parallel_end_to_end.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
STOCK = 3
3131
CHECKOUTS = 10
3232
ROUND_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."""
3534
UNUSED_CONTROL_PORT = 9
3635
EXACT = ComparatorSpec(ComparatorType.EXACT)
3736

@@ -67,11 +66,16 @@ def reservations_document() -> dict[str, Any]:
6766

6867
@dataclass
6968
class 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"})

0 commit comments

Comments
 (0)