test: load-test concurrent intent accept for race conditions - #162
Open
Paranoa-dev wants to merge 1 commit into
Open
test: load-test concurrent intent accept for race conditions#162Paranoa-dev wants to merge 1 commit into
Paranoa-dev wants to merge 1 commit into
Conversation
- Add atomic acceptIfOpen() and fillIfAccepted() methods to IntentsService that mirror DB-level conditional updates (UPDATE ... WHERE state='open') - Refactor IntentsController to use atomic methods, eliminating the non-atomic read-check-update pattern in accept() and fill() - Add concurrent HTTP load tests (20 parallel requests) asserting only one accept/fill succeeds per intent - Add unit tests for atomic service methods with simulated concurrency - Fix e2e test config: disable diagnostics, add stellar-sdk mock, include load test files in test runner Closes stellar-vortex-protocol#63
|
@Paranoa-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Load-test the persistence layer for concurrent accept/fill races
Summary
This PR adds a race-condition load test for concurrent
accept()andfill()calls on the same intent, and fixes the underlying non-atomic read-check-update pattern inIntentsControllerby introducing atomic conditional-update methods inIntentsService.Problem
The current
IntentsController.accept()andIntentsController.fill()follow a read → check → update pattern:This is safe in a single-threaded Node.js process with no
awaitbetween steps, but once state lives in a shared database across multiple backend instances (horizontal scaling), this pattern breaks — two instances could both read"open"state and both succeed in accepting the same intent.Solution
1. Atomic conditional-update methods (
src/intents/intents.service.ts)Added
acceptIfOpen()andfillIfAccepted()— synchronous, atomic check-and-update methods that mirror the DB pattern:Both return
nullwhen the preconditions are not met (intent not found, wrong state, wrong solver).2. Controller refactored (
src/intents/intents.controller.ts)Updated
accept()andfill()to use the atomic methods instead of the separateget → check → updatepattern. Error handling now checks the return value of the atomic method.3. Load test (
test/load/concurrent-accept.test.ts)accept()(orfill()) requests at the same intent via supertest, asserts exactly one succeeds (201) and the rest fail (409 Conflict)acceptIfOpen()/fillIfAccepted()50 times synchronously on the same intent, asserts exactly one winner4. E2e config fixes (
test/jest-e2e.json)Fixed pre-existing issues that prevented e2e tests from running:
@types/joi,@stellar/stellar-sdk)@stellar/stellar-sdk(ESM-only package incompatible with Jest's CJS resolver)Test Results
Migration Path
When moving to a real database, replace
acceptIfOpen()andfillIfAccepted()with parameterized SQL usingWHERE state = '...'clauses. The in-memory implementation already guarantees atomicity and serves as the reference contract.Checklist
npm run lintpassesnpm run typecheckpassesnpm testpassesnpm run test:e2epassesCloses #63