Merge pull request #5553 from eugenp/Issue-5065
Fixed SemaphoresManualTest
This commit is contained in:
commit
64fb1c159e
@ -4,6 +4,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@ -15,26 +16,28 @@ public class SemaphoresManualTest {
|
|||||||
// ========= login queue ======
|
// ========= login queue ======
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenLoginQueue_whenReachLimit_thenBlocked() {
|
public void givenLoginQueue_whenReachLimit_thenBlocked() throws InterruptedException {
|
||||||
final int slots = 10;
|
final int slots = 10;
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(loginQueue::tryLogin));
|
.forEach(user -> executorService.execute(loginQueue::tryLogin));
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
assertEquals(0, loginQueue.availableSlots());
|
assertEquals(0, loginQueue.availableSlots());
|
||||||
assertFalse(loginQueue.tryLogin());
|
assertFalse(loginQueue.tryLogin());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenLoginQueue_whenLogout_thenSlotsAvailable() {
|
public void givenLoginQueue_whenLogout_thenSlotsAvailable() throws InterruptedException {
|
||||||
final int slots = 10;
|
final int slots = 10;
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(loginQueue::tryLogin));
|
.forEach(user -> executorService.execute(loginQueue::tryLogin));
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
assertEquals(0, loginQueue.availableSlots());
|
assertEquals(0, loginQueue.availableSlots());
|
||||||
loginQueue.logout();
|
loginQueue.logout();
|
||||||
@ -45,13 +48,14 @@ public class SemaphoresManualTest {
|
|||||||
// ========= delay queue =======
|
// ========= delay queue =======
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDelayQueue_whenReachLimit_thenBlocked() {
|
public void givenDelayQueue_whenReachLimit_thenBlocked() throws InterruptedException {
|
||||||
final int slots = 50;
|
final int slots = 50;
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
|
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(delayQueue::tryAdd));
|
.forEach(user -> executorService.execute(delayQueue::tryAdd));
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
assertEquals(0, delayQueue.availableSlots());
|
assertEquals(0, delayQueue.availableSlots());
|
||||||
assertFalse(delayQueue.tryAdd());
|
assertFalse(delayQueue.tryAdd());
|
||||||
@ -65,6 +69,7 @@ public class SemaphoresManualTest {
|
|||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(delayQueue::tryAdd));
|
.forEach(user -> executorService.execute(delayQueue::tryAdd));
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
assertEquals(0, delayQueue.availableSlots());
|
assertEquals(0, delayQueue.availableSlots());
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user