Skip to content

Task 3-2: Integration Test - Concurrent Confirm (Race Condition)

Phase: 3 - Testing Priority: High Depends on: task-1-2 Reference: docs/BountyHunter-Backend/details/feature-nft-management/SPEC.md

Background

Test race condition khi 2 renters cùng lúc gọi confirm-rent cho cùng 1 rental order. Sau khi task-1-2 thêm distributed lock, cần verify lock hoạt động đúng.

Test scenarios

Concurrent confirm - 2 renters

  • [ ] Setup: 1 rental order status=NEW_VERIFIED
  • [ ] 2 renters gọi PUT /confirm-rent/{orderId} đồng thời (thread A và thread B) — dùng CountDownLatch hoặc parallel HTTP requests
  • [ ] Expected: chỉ 1 renter thành công (status=RENTING), 1 nhận error RENTAL_ORDER_NOT_AVAILABLE
  • [ ] Verify: chỉ 1 renter bị charge B-Coin (balance check sau cả 2 requests hoàn tất)
  • [ ] Verify: owner chỉ nhận 1 lần deposit
  • [ ] Verify: order status=RENTING với đúng 1 renterId (không bị overwrite)

Lock timeout scenario

  • [ ] Simulate lock holder chạy chậm (mock delay trong lock bằng Thread.sleep trong test)
  • [ ] Verify: other renter chờ hoặc nhận timeout error (không bị deadlock vô hạn)
  • [ ] Verify: sau timeout → lock được release, order state consistent

After lock release

  • [ ] Successful confirm → lock released
  • [ ] Verify: order không thể confirm lần 2 (status guard status != NEW blocks, không phải lock blocks)
  • [ ] Verify: RENTAL_ORDER_NOT_AVAILABLE error khi cố confirm order đã RENTING

Verification / Acceptance Criteria

  • [ ] Concurrent confirm: đúng 1 success, đúng 1 RENTAL_ORDER_NOT_AVAILABLE
  • [ ] No double charge: tổng B-Coin bị deduct = 1 × rentalFee
  • [ ] No double credit: owner nhận đúng 1 lần
  • [ ] Order renterId là consistent (không bị race condition overwrite)
  • [ ] Lock timeout không gây deadlock — service vẫn responsive sau timeout

Files to Modify

  • Test files trong webmarketplace/src/test/ hoặc integration test module