From b12849dea655741c5a2fc31bdc4e225c8285677a Mon Sep 17 00:00:00 2001 From: eric-martin Date: Sat, 27 Oct 2018 22:29:02 -0500 Subject: [PATCH] Fixed SemaphoresManualTest --- .../concurrent/semaphores/SemaphoresManualTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java index 8d64bb6809..cf3bdeda5b 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java @@ -4,6 +4,7 @@ import org.junit.Test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; import static org.junit.Assert.assertEquals; @@ -15,26 +16,28 @@ public class SemaphoresManualTest { // ========= login queue ====== @Test - public void givenLoginQueue_whenReachLimit_thenBlocked() { + public void givenLoginQueue_whenReachLimit_thenBlocked() throws InterruptedException { final int slots = 10; final ExecutorService executorService = Executors.newFixedThreadPool(slots); final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots); IntStream.range(0, slots) .forEach(user -> executorService.execute(loginQueue::tryLogin)); executorService.shutdown(); + executorService.awaitTermination(10, TimeUnit.SECONDS); assertEquals(0, loginQueue.availableSlots()); assertFalse(loginQueue.tryLogin()); } @Test - public void givenLoginQueue_whenLogout_thenSlotsAvailable() { + public void givenLoginQueue_whenLogout_thenSlotsAvailable() throws InterruptedException { final int slots = 10; final ExecutorService executorService = Executors.newFixedThreadPool(slots); final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots); IntStream.range(0, slots) .forEach(user -> executorService.execute(loginQueue::tryLogin)); executorService.shutdown(); + executorService.awaitTermination(10, TimeUnit.SECONDS); assertEquals(0, loginQueue.availableSlots()); loginQueue.logout(); @@ -45,13 +48,14 @@ public class SemaphoresManualTest { // ========= delay queue ======= @Test - public void givenDelayQueue_whenReachLimit_thenBlocked() { + public void givenDelayQueue_whenReachLimit_thenBlocked() throws InterruptedException { final int slots = 50; final ExecutorService executorService = Executors.newFixedThreadPool(slots); final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots); IntStream.range(0, slots) .forEach(user -> executorService.execute(delayQueue::tryAdd)); executorService.shutdown(); + executorService.awaitTermination(10, TimeUnit.SECONDS); assertEquals(0, delayQueue.availableSlots()); assertFalse(delayQueue.tryAdd()); @@ -65,6 +69,7 @@ public class SemaphoresManualTest { IntStream.range(0, slots) .forEach(user -> executorService.execute(delayQueue::tryAdd)); executorService.shutdown(); + executorService.awaitTermination(10, TimeUnit.SECONDS); assertEquals(0, delayQueue.availableSlots()); Thread.sleep(1000);