Software Testing & QA (50 Qs)•Part 3: Advanced Internals, Scale & Production Quality (Senior / Lead)
Concurrency and Race Condition Testing Strategies.
senior LevelCode ExampleTesting
In Simple Words: Testing that concurrent operations do not cause double-spending or corrupt bank balances:
typescript
test('concurrent withdrawals prevent double-spending balance', async () => {
const account = await createAccount({ balance: 100 });
// Simulate 5 simultaneous requests attempting to withdraw $100
const requests = Array(5).fill(null).map(() =>
withdraw({ accountId: account.id, amount: 100 })
);
const results = await Promise.allSettled(requests);
const successes = results.filter(r => r.status === 'fulfilled');
// Exactly ONE request must succeed; 4 must be rejected with 409 Conflict
expect(successes.length).toBe(1);
const finalAccount = await getAccount(account.id);
expect(finalAccount.balance).toBe(0); // Never negative!
});