Files
Assignment-9/src/test/java/banking/TestA.java
T

49 lines
1.1 KiB
Java

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()
);
}
}