1+ package com .baeldung .concurrent .delayqueue ;
2+
3+ import org .junit .FixMethodOrder ;
4+ import org .junit .Test ;
5+ import org .junit .runners .MethodSorters ;
6+
7+ import java .util .concurrent .*;
8+
9+ import static junit .framework .TestCase .assertEquals ;
10+
11+ @ FixMethodOrder (MethodSorters .NAME_ASCENDING )
12+ public class DelayQueueTest {
13+ @ Test
14+ public void givenDelayQueue_whenProduceElement_thenShouldConsumeAfterGivenDelay () throws InterruptedException {
15+ //given
16+ ExecutorService executor = Executors .newFixedThreadPool (2 );
17+ BlockingQueue <DelayObject > queue = new DelayQueue <>();
18+ int numberOfElementsToProduce = 2 ;
19+ int delayOfEachProducedMessageMilliseconds = 500 ;
20+ DelayQueueConsumer consumer = new DelayQueueConsumer (queue , numberOfElementsToProduce );
21+ DelayQueueProducer producer
22+ = new DelayQueueProducer (queue , numberOfElementsToProduce , delayOfEachProducedMessageMilliseconds );
23+
24+ //when
25+ executor .submit (producer );
26+ executor .submit (consumer );
27+
28+ //then
29+ executor .awaitTermination (5 , TimeUnit .SECONDS );
30+ executor .shutdown ();
31+ assertEquals (consumer .numberOfConsumedElements .get (), numberOfElementsToProduce );
32+
33+ }
34+
35+ @ Test
36+ public void givenDelayQueue_whenProduceElementWithHugeDelay_thenConsumerWasNotAbleToConsumeMessageInGivenTime () throws InterruptedException {
37+ //given
38+ ExecutorService executor = Executors .newFixedThreadPool (2 );
39+ BlockingQueue <DelayObject > queue = new DelayQueue <>();
40+ int numberOfElementsToProduce = 1 ;
41+ int delayOfEachProducedMessageMilliseconds = 10_000 ;
42+ DelayQueueConsumer consumer = new DelayQueueConsumer (queue , numberOfElementsToProduce );
43+ DelayQueueProducer producer
44+ = new DelayQueueProducer (queue , numberOfElementsToProduce , delayOfEachProducedMessageMilliseconds );
45+
46+ //when
47+ executor .submit (producer );
48+ executor .submit (consumer );
49+
50+ //then
51+ executor .awaitTermination (5 , TimeUnit .SECONDS );
52+ executor .shutdown ();
53+ assertEquals (consumer .numberOfConsumedElements .get (), 0 );
54+
55+ }
56+
57+ @ Test
58+ public void givenDelayQueue_whenProduceElementWithNegativeDelay_thenConsumeMessageImmediately () throws InterruptedException {
59+ //given
60+ ExecutorService executor = Executors .newFixedThreadPool (2 );
61+ BlockingQueue <DelayObject > queue = new DelayQueue <>();
62+ int numberOfElementsToProduce = 1 ;
63+ int delayOfEachProducedMessageMilliseconds = -10_000 ;
64+ DelayQueueConsumer consumer = new DelayQueueConsumer (queue , numberOfElementsToProduce );
65+ DelayQueueProducer producer
66+ = new DelayQueueProducer (queue , numberOfElementsToProduce , delayOfEachProducedMessageMilliseconds );
67+
68+ //when
69+ executor .submit (producer );
70+ executor .submit (consumer );
71+
72+ //then
73+ executor .awaitTermination (1 , TimeUnit .SECONDS );
74+ executor .shutdown ();
75+ assertEquals (consumer .numberOfConsumedElements .get (), 1 );
76+
77+ }
78+ }
0 commit comments