Skip to main content

Stock locks: test plan

For: testers, with a developer for the held-lock checks, on the QA server before release v2.7.0. What to check so that two people changing the same stock at the same moment never lose or double a movement. The rules are in Stock locks: the rules.

Two ways to test at the same moment

A. Two people, one count. Two testers on two PCs (or two browsers signed in as different users) fill in their screens, then one says "3, 2, 1" and both press Save together. Repeat each check 3 times: timing by hand does not always overlap.

B. A held lock (reliable). A developer holds the stock row's lock in the database, the tester presses Save, and the developer releases it. The save must wait, then go through on the right numbers.

-- Developer, in psql on the QA shop's schema
BEGIN;
SELECT stock_id, stock_quantity FROM product.stock
 WHERE product_price_id = <P1 id> AND location_id = <branch A id> FOR UPDATE;
-- Tester presses Save now: the screen keeps loading.
-- Wait 10 seconds, then:
COMMIT;   -- the tester's save finishes

Never do B on a customer server. Do not hold the lock longer than about 30 seconds: the screen may time out.

Set up once

You need Why
Branch A and B, two users who hold A, one who holds both Two people on the same stock
Product P1 at A with exactly 1 on hand The "last unit" case
Product P2 at A with 100 on hand, average ₹100 Quantity and cost checks
Product P3 at A, a different product To check unrelated products do not wait
A sale of 5 × P2, and a purchase of 10 × P2 For the return checks
A developer with psql access to the QA database For method B

Sales and purchases

# Check Steps Pass when
1 Last unit, setting on rejectNegativeStockOnWac on. Two users sell 1 × P1 together (A) One sale saves. The other: "Not enough stock: 0.0 on hand, 1.0 requested for …". P1 = 0
2 Last unit, setting off Same, setting off Both save. P1 = −1. Stock history has two sell rows, quantities 1 → 0 → −1 (none lost)
3 Two sales, many units Two users sell 10 × P2 together Both save. P2 = 80. Stock history: 100 → 90 → 80
4 Sale and purchase together One sells 10 × P2, the other purchases 10 × P2 at ₹130 P2 = 100. Average is right for the order the history shows (sale first: 90 at 100 + 10 at 130 = ₹103; purchase first: 100 at 100 + 10 at 130 = ₹102.727273, then the sale leaves at that)
5 Held lock, sale Method B on P2 at A › tester saves a sale of 1 × P2 Screen waits until COMMIT, then saves. P2 falls by 1
6 Unrelated product Method B on P2 › tester sells P3 Saves at once. No wait
7 Other branch Method B on P2 at A › tester sells P2 at B Saves at once. No wait
8 Many lines, shared products User 1 sells P2 + P3, user 2 sells P3 + P2 (lines in the other order), together Both save. No error, no endless loading (no deadlock)

Returns

# Check Steps Pass when
9 Two sale returns Sale of 5 × P2. Two users each return 3 against it, together One saves. The other is refused: more than sold less returned. Returned total = 3
10 Two purchase returns Purchase of 10 × P2. Two users each return 6, together One saves, the other is refused. Returned total = 6
11 Held lock, return Developer holds the sale row (SELECT sale_id FROM sale.sale WHERE sale_id = <id> FOR UPDATE) › tester saves a return Waits until COMMIT, then saves

Stock transfers

# Check Steps Pass when
12 Dispatch twice Two tabs, same draft, Dispatch together One dispatch. Other: "Only a draft transfer can be dispatched; this one is DISPATCHED". Stock moved once
13 Edit while dispatching Tab 1 edits the draft, tab 2 dispatches it, Save together Either the edit saves first and the dispatch sends the edited items, or the dispatch goes first and the edit gets "Only a draft transfer can be changed; this one is DISPATCHED". The stock moved always matches the items shown
14 Receive twice Two users at B receive all outstanding together One receipt. Other: "More was received of … than is outstanding (0)" or "…it is RECEIVED". In transit not below 0
15 Receive and cancel together User at B receives, user AB cancels, together Only one happens. Either received, or cancelled, never both
16 Dispatch and sale together P1 has 1 at A. Dispatch 1 × P1 and sell 1 × P1 together Never both take the last unit when the sale setting is on. Dispatch never takes A below zero
17 Two transfers crossing Transfer A → B of P2 and B → A of P2, dispatched together Both dispatch. No error, no endless loading

One stock row per product per branch

# Check Steps Pass when
18 Unique key exists Developer: \d product.stock Unique constraint on (product_price_id, location_id)
19 Script refuses duplicates Developer, on a copy with a duplicate row: run stock_wac_columns.sql It stops with an error and changes nothing
20 Transfer refuses duplicates On a copy with two rows for one SKU at B: dispatch A → B "The receiving branch has more than one stock row for SKU …; merge them before transferring it"

After every check

  • Stock history of the product: one row per movement, each stock now follows from the previous one. No gaps, no doubles.
  • Stock report quantity = last stock history row.
  • No error text or blank screen. A wait longer than a few seconds without method B is a bug: report it with the time, so the developer can find it in the log.