-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[warehouse] A minor refactoring of the class WarehouseWorkerPoolTest (#…
…648)
- Loading branch information
1 parent
61c3ddd
commit 6229826
Showing
1 changed file
with
12 additions
and
9 deletions.
There are no files selected for viewing
21 changes: 12 additions & 9 deletions
21
warehouse/src/test/java/com/usthe/warehouse/WarehouseWorkerPoolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |