Skip to content

Commit

Permalink
[warehouse] A minor refactoring of the class WarehouseWorkerPoolTest (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HattoriHenzo authored Feb 20, 2023
1 parent 61c3ddd commit 6229826
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
package com.usthe.warehouse;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link WarehouseWorkerPool}
*/
class WarehouseWorkerPoolTest {

private static final int NUMBER_OF_THREADS = 10;
private WarehouseWorkerPool pool;
private AtomicInteger counter;
private CountDownLatch latch;

@BeforeEach
void setUp() {
pool = new WarehouseWorkerPool();
counter = new AtomicInteger();
latch = new CountDownLatch(NUMBER_OF_THREADS);

}

@Test
void executeJob() throws InterruptedException {
int numberOfThreads = 10;
AtomicInteger counter = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(numberOfThreads);

for (int i = 0; i < numberOfThreads; i++) {
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
pool.executeJob(() -> {
counter.incrementAndGet();
latch.countDown();
});
}
latch.await();

Assertions.assertEquals(numberOfThreads, counter.get());
assertEquals(NUMBER_OF_THREADS, counter.get());
}

}

0 comments on commit 6229826

Please sign in to comment.