- Introduce BankAccount-focused concurrency tests based on Transaction → Processor → Account flow - Add stress tests for high-contention deposits, withdrawals, and bidirectional transfers - Validate atomicity and consistency across multi-threaded execution scenarios - Include deadlock detection tests under heavy transfer contention - Align all test cases with updated module structure and BankAccount concurrency contract
45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
package banking;
|
|
|
|
import dev.banking.model.BankAccount;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
public class BankAccountTransferConsistencyTest {
|
|
|
|
@Test
|
|
void transferConservation() throws Exception {
|
|
|
|
BankAccount a = new BankAccount(1, 100_000);
|
|
BankAccount b = new BankAccount(2, 100_000);
|
|
|
|
ExecutorService executor = Executors.newFixedThreadPool(50);
|
|
CountDownLatch latch = new CountDownLatch(100);
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
executor.submit(() -> {
|
|
for (int j = 0; j < 1000; j++) {
|
|
a.transfer(b, 1);
|
|
}
|
|
latch.countDown();
|
|
});
|
|
|
|
executor.submit(() -> {
|
|
for (int j = 0; j < 1000; j++) {
|
|
b.transfer(a, 1);
|
|
}
|
|
latch.countDown();
|
|
});
|
|
}
|
|
|
|
latch.await();
|
|
executor.shutdown();
|
|
|
|
long total = a.getBalance() + b.getBalance();
|
|
|
|
assertEquals(200_000, total);
|
|
}
|
|
} |