Part 5 of 6 · 3 min read
Edge cases and changes
Partial failure, menus and many parties.
Step 5: edge cases, tests and change requests
Edge cases:
| Case | Behaviour |
|---|---|
| Two customers want the last pizza | One tryTake wins, the other gets "out of stock" |
| Menu price changes after placing | The order keeps its own line prices |
| Item sold out between add and place | tryTake refuses the whole order |
| Cancel after cooking begins | Refused by the state table |
| Restaurant rejects the order | A move from placed to cancelled, with the stock returned |
| A listener throws | Wrap each call in try and catch, so the others still run (as in module 3) |
| Order id counter with many threads | Use an atomic counter, as seq here is not thread-safe |
An honest weakness: the code increments seq without a lock, so two threads could make the same order id. Replace it with an AtomicInteger. Saying this unprompted shows careful reading.
Another weakness: stock is taken at place time, but payment usually comes after. If payment fails, the order must be cancelled, and the stock must be returned. That is the compensating action again (Level 6, module 5), and the state table already supports it, since placed may go to cancelled.
Tests to write first: each row of the run table; two threads racing for the last item; the cancel that returns stock; each illegal state move; and a menu price change after placing.
Change requests and the part that changes:
- "Coupons and first-order discounts." New
Discountclasses, and a wrapper that picks the best of several. - "Delivery fee by distance." A
DeliveryFeestrategy, like the discount. - "Items with options, such as size or extra cheese." A
Linethat holds chosen options, and a price that depends on them. - "Scheduled orders." A time on the order, and a job that moves it to accepted at the right moment.
- "Assign a delivery partner." A matcher like the one in module 7. The order state machine gets a step for assignment.
- "Send a message to the customer on each step." A listener class. Nothing else changes.
- "Many restaurants with the same cart." A cart per restaurant, and an order group that holds them.
Common mistake
Taking stock one item at a time. If the second item is out of stock, the first is already taken and must be put back by hand. Check every line first, and then take them all in one step.
Interview angle
Answer in your head first, then open it.
Why separate cart and order?
The cart is a changeable draft. The order is a fixed record with a snapshot of prices, so later changes cannot alter it.
How do you avoid overselling?
One owner of stock with an atomic take-all-or-none.
How do you model the life cycle?
A state machine with a table of legal moves, and listeners told after each change.
Who gets notified?
Listeners registered with the app. The order does not know what they do.
Check yourself:
1. Which change request needs only a new Discount class?
2. Why is seq a weakness?
3. How does the design handle a payment that fails?
Words you will meet
| Word | Meaning |
|---|---|
| Cart | A draft list of items that has not been ordered yet |
| Order | A placed, fixed record of what was bought |
| Line | One item and its quantity within an order |
| Stock | How many units of an item are left |
| Listener | An object told when something changes |