Skip to content

Commit b12849d

Browse files
committed
Fixed SemaphoresManualTest
1 parent 57f89ee commit b12849d

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
78
import java.util.stream.IntStream;
89

910
import static org.junit.Assert.assertEquals;
@@ -15,26 +16,28 @@ public class SemaphoresManualTest {
1516
// ========= login queue ======
1617

1718
@Test
18-
public void givenLoginQueue_whenReachLimit_thenBlocked() {
19+
public void givenLoginQueue_whenReachLimit_thenBlocked() throws InterruptedException {
1920
final int slots = 10;
2021
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
2122
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
2223
IntStream.range(0, slots)
2324
.forEach(user -> executorService.execute(loginQueue::tryLogin));
2425
executorService.shutdown();
26+
executorService.awaitTermination(10, TimeUnit.SECONDS);
2527

2628
assertEquals(0, loginQueue.availableSlots());
2729
assertFalse(loginQueue.tryLogin());
2830
}
2931

3032
@Test
31-
public void givenLoginQueue_whenLogout_thenSlotsAvailable() {
33+
public void givenLoginQueue_whenLogout_thenSlotsAvailable() throws InterruptedException {
3234
final int slots = 10;
3335
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
3436
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
3537
IntStream.range(0, slots)
3638
.forEach(user -> executorService.execute(loginQueue::tryLogin));
3739
executorService.shutdown();
40+
executorService.awaitTermination(10, TimeUnit.SECONDS);
3841

3942
assertEquals(0, loginQueue.availableSlots());
4043
loginQueue.logout();
@@ -45,13 +48,14 @@ public void givenLoginQueue_whenLogout_thenSlotsAvailable() {
4548
// ========= delay queue =======
4649

4750
@Test
48-
public void givenDelayQueue_whenReachLimit_thenBlocked() {
51+
public void givenDelayQueue_whenReachLimit_thenBlocked() throws InterruptedException {
4952
final int slots = 50;
5053
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
5154
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
5255
IntStream.range(0, slots)
5356
.forEach(user -> executorService.execute(delayQueue::tryAdd));
5457
executorService.shutdown();
58+
executorService.awaitTermination(10, TimeUnit.SECONDS);
5559

5660
assertEquals(0, delayQueue.availableSlots());
5761
assertFalse(delayQueue.tryAdd());
@@ -65,6 +69,7 @@ public void givenDelayQueue_whenTimePass_thenSlotsAvailable() throws Interrupted
6569
IntStream.range(0, slots)
6670
.forEach(user -> executorService.execute(delayQueue::tryAdd));
6771
executorService.shutdown();
72+
executorService.awaitTermination(10, TimeUnit.SECONDS);
6873

6974
assertEquals(0, delayQueue.availableSlots());
7075
Thread.sleep(1000);

0 commit comments

Comments
 (0)