test: add unit tests for multithreading scenarios

This commit is contained in:
2026-06-04 21:00:09 +03:30
parent 6d8bee27f1
commit 73395fa188
10 changed files with 281 additions and 3 deletions
+46 -1
View File
@@ -1,4 +1,49 @@
package banking;
import dev.banking.model.BankAccount;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestA {
}
@Test
void concurrentDeposits() throws Exception {
BankAccount account = new BankAccount(1, 0);
int threadCount = 100;
int depositsPerThread = 1000;
ExecutorService executor =
Executors.newFixedThreadPool(threadCount);
CountDownLatch latch =
new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
executor.submit(() -> {
for (int j = 0; j < depositsPerThread; j++) {
account.deposit(1);
}
latch.countDown();
});
}
latch.await();
executor.shutdown();
assertEquals(
threadCount * depositsPerThread,
account.getBalance()
);
}
}
+53 -1
View File
@@ -1,4 +1,56 @@
package banking;
import dev.banking.model.BankAccount;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestB {
}
@Test
void concurrentDepositWithdraw() throws Exception {
BankAccount account =
new BankAccount(1, 1_000_000);
ExecutorService executor =
Executors.newFixedThreadPool(100);
CountDownLatch latch =
new CountDownLatch(100);
for (int i = 0; i < 50; i++) {
executor.submit(() -> {
for (int j = 0; j < 10000; j++) {
account.deposit(1);
}
latch.countDown();
});
executor.submit(() -> {
for (int j = 0; j < 10000; j++) {
account.withdraw(1);
}
latch.countDown();
});
}
latch.await();
executor.shutdown();
assertEquals(
1_000_000,
account.getBalance()
);
}
}
+95 -1
View File
@@ -1,4 +1,98 @@
package banking;
import dev.banking.model.BankAccount;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
public class TestC {
}
@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);
}
@Test
void deadlockFreeTransfers() {
assertTimeoutPreemptively(
Duration.ofSeconds(5),
() -> {
BankAccount a =
new BankAccount(1, 1_000_000);
BankAccount b =
new BankAccount(2, 1_000_000);
ExecutorService executor =
Executors.newFixedThreadPool(50);
for (int i = 0; i < 50000; i++) {
executor.submit(() ->
a.transfer(b, 1));
executor.submit(() ->
b.transfer(a, 1));
}
executor.shutdown();
assertTrue(
executor.awaitTermination(
5,
TimeUnit.SECONDS
)
);
}
);
}
}