The warehouse service includes a simplified reservation preview module. The preview_reservation method must return a list of available SKU codes for the requested codes and must not change warehouse state if there is an unknown code in the input. In addition, this method must not hold the global state lock longer than necessary to read a snapshot of stock: after the data has been obtained, any comparatively long response preparation should proceed without Mutex.
A code review of the PR changes below is required.
Behavior requirements:
Warehouse::add_stock increases stock for a SKU.Warehouse::preview_reservation accepts a list of codes and returns Ok(Vec<String>) only for those codes whose stock is greater than zero.Mutex<HashMap<...>> lock must be used only to read a snapshot of the current stock; further filtering and response formatting must be performed after the lock is released.Environment and run:
cargo test.Rust 1.85.0, edition 2021; standard library only, no external dependencies. Single-package crate, tests are run with cargo test.
Use line comments to explain the problem, its impact, the fix and how to test it.