Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_promotion_redemption_race.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_promotion_redemption_race.py · 3.9 KB · 96 lines
1"""A promotion's caps are enforced inside the write lock, not before it.
2
3Found by simulation: 20 concurrent guest checkouts against a
4max_redemptions=1 code all got the discount. The gate in checkout is a
5read; between it and the redemption write any number of shoppers pass it.
6The cap and the per-customer limit are now a schema `locked_checks` entry
7(hook_promotion_redemptions.LOCKED_CHECK), so every writer meets them.
8"""
9
10import threading
11
12import pytest
13
14import object_promotions
15import object_records
16from test_promotions import (cart_items_for, checkout_as, product,
17 promotion, rows, setup_env, stock_in)
18
19
20def _baskets(data_dir, count, *, emails):
21 product(data_dir, "p1", "Enamel Mug", 10000)
22 stock_in(data_dir, "p1", 100)
23 for i in range(count):
24 token = f"race-session-token-{i:02d}-abcdefghij"
25 cart_items_for(data_dir, f"rc{i}", token, f"ri{i}")
26 return [(f"race-session-token-{i:02d}-abcdefghij", emails(i))
27 for i in range(count)]
28
29
30def _stampede(objects_root, baskets):
31 start = threading.Barrier(len(baskets))
32 answers = [None] * len(baskets)
33
34 def go(i, token, email):
35 start.wait()
36 answers[i] = checkout_as(objects_root, token, email,
37 promo_code="SPRING20")
38
39 threads = [threading.Thread(target=go, args=(i, t, e))
40 for i, (t, e) in enumerate(baskets)]
41 for t in threads:
42 t.start()
43 for t in threads:
44 t.join()
45 return answers
46
47
48def test_concurrent_checkouts_cannot_overspend_a_capped_code(tmp_path, monkeypatch):
49 data_dir, objects_root = setup_env(tmp_path, monkeypatch)
50 promotion(data_dir, max_redemptions=1)
51 baskets = _baskets(data_dir, 12, emails=lambda i: f"shopper{i}@example.com")
52 answers = _stampede(objects_root, baskets)
53 discounted = [a for a in answers if a.get("ok") and a.get("discount_cents")]
54 assert len(discounted) == 1, answers
55 assert len(rows(data_dir, "promotion_redemptions")) == 1
56 refused = [a for a in answers if not a.get("ok")]
57 assert len(refused) == 11 and all(a["status"] == 409 for a in refused)
58 # a refused shopper is not left with a half-made order or a claimed basket
59 assert len(rows(data_dir, "orders")) == 1
60
61
62def test_concurrent_checkouts_cannot_beat_the_per_customer_limit(tmp_path, monkeypatch):
63 data_dir, objects_root = setup_env(tmp_path, monkeypatch)
64 promotion(data_dir, per_customer_limit=1)
65 baskets = _baskets(data_dir, 8, emails=lambda i: "Same.Person@Example.com")
66 answers = _stampede(objects_root, baskets)
67 assert len([a for a in answers if a.get("ok")]) == 1, answers
68 assert len(rows(data_dir, "promotion_redemptions")) == 1
69
70
71def test_the_locked_check_refuses_a_direct_write_too(tmp_path, monkeypatch):
72 data_dir, _ = setup_env(tmp_path, monkeypatch)
73 promotion(data_dir, max_redemptions=1)
74 row = {"promotion_id": "pr1", "code_used": "SPRING20", "customer_email": "a@x.com",
75 "discount_cents": "1", "owner_id": "shop"}
76 object_records.create_collection_record("promotion_redemptions", dict(row), base_dir=data_dir)
77 with pytest.raises(object_records.CheckRefusedError):
78 object_records.create_collection_record("promotion_redemptions", dict(row),
79 base_dir=data_dir)
80
81
82@pytest.mark.parametrize("a,b", [
83 ("Ada@Example.com", "ada@example.com"),
84 ("ada+spring@example.com", "ada@example.com"),
85 (" ada+x+y@EXAMPLE.com ", "ada+z@example.com"),
86])
87def test_one_person_has_one_key(a, b):
88 assert object_promotions.customer_key(a) == object_promotions.customer_key(b)
89
90
91def test_other_addresses_stay_distinct():
92 assert object_promotions.customer_key("ada@example.com") != \
93 object_promotions.customer_key("ada@example.org")
94 assert object_promotions.customer_key("a.da@example.com") != \
95 object_promotions.customer_key("ada@example.com")
96 assert object_promotions.customer_key("") == ""