The PR below shows changes in the HTTP handler for refund finalization. After the update, complaints appeared about unstable retries of the same request and about successful responses in situations where the operation was not actually completed.
You need to review the diff and find behavioral defects. Assume that the service stores finalized refunds only in memory, and the idempotency key is passed in the Idempotency-Key header.
POST /refunds/finalize accepts JSON of the form {"refund_id":"rf-1","amount":150}.201 Created and JSON with the fields refund_id, amount, status.Idempotency-Key must return the previously saved result without re-finalizing.handler.go.Store is used.Idempotency-Key and the commit-error scenario via the X-Commit-Mode: fail header.Example request:
curl -i -X POST http://localhost:8080/refunds/finalize \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: req-7' \
-d '{"refund_id":"rf-1","amount":150}'
Example of reproducing commit failure:
curl -i -X POST http://localhost:8080/refunds/finalize \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: req-8' \
-H 'X-Commit-Mode: fail' \
-d '{"refund_id":"rf-2","amount":200}'
Idempotency-Key, the result must be determined atomically: concurrent retries must not finalize the same request multiple times and must not write to shared state in an unsafe way.500 must be returned, and a successful JSON response must not be sent.1.24net/http, single processgo test ./... or go run .X-Commit-Mode: fail header deterministically forces Commit to return an errorUse line comments to explain the problem, its impact, the fix and how to test it.