Skip to content

Add regression tests for concurrent open/execute races on the same settlement id in settlementRepository #77

Description

@Jagadeeshftw

📌 Description

SettlementRepository in src/repositories/settlementRepository.ts tracks nextId, reserved, and consumed liquidity in memory. Node's single-threaded event loop makes true races unlikely, but interleaved async handlers (e.g. two execute/cancel calls for the same id issued back-to-back) are not covered by settlementRepository.test.ts, and a bug here could double-release or double-consume reserved liquidity.

🧩 Requirements and context

  • Add tests that fire execute/cancel for the same settlement id from two concurrent promise chains and assert only one succeeds, the other gets a defined error (already-executed/already-cancelled).
  • Assert reserved liquidity is released exactly once regardless of call ordering.
  • Document the repository's concurrency assumptions (single in-process instance, no external locking) in a code comment.

🛠️ Suggested execution

  • Extend src/repositories/settlementRepository.test.ts with interleaved-call scenarios using Promise.all.
  • If a real double-consume/double-release bug surfaces, fix it in settlementRepository.ts and add a targeted regression test.
  • Cross-check src/services/settlementService.ts for any additional guard it relies on the repository to provide.

✅ Acceptance criteria

  • Concurrent execute/cancel calls on one settlement never double-mutate reserved/consumed state.
  • Losing call in a race gets a clear, tested error.
  • Concurrency assumptions documented in code.

🔒 Security notes

Prevents a liquidity-accounting bug (double release) that could let more be withdrawn than was ever reserved.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial Campaign | FWC26GrantFox official campaign issuedatabasePostgreSQL / persistencetestingTests and coverage

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions