15
15
16
16
package com .rabbitmq .client .test .functional ;
17
17
18
- import com .rabbitmq .client .*;
19
18
import com .rabbitmq .client .test .BrokerTestCase ;
20
19
import org .junit .Test ;
20
+ import static org .junit .Assert .assertEquals ;
21
+ import static org .junit .Assert .assertTrue ;
22
+ import static org .junit .Assert .fail ;
21
23
22
24
import java .io .IOException ;
23
25
import java .util .Arrays ;
27
29
import java .util .concurrent .LinkedBlockingQueue ;
28
30
import java .util .concurrent .TimeUnit ;
29
31
30
- import static org .junit .Assert .assertEquals ;
31
- import static org .junit .Assert .fail ;
32
+ import java .util .concurrent .CountDownLatch ;
33
+
34
+ import com .rabbitmq .client .DefaultConsumer ;
35
+ import com .rabbitmq .client .Envelope ;
36
+
37
+ import com .rabbitmq .client .AMQP ;
38
+ import com .rabbitmq .client .Channel ;
39
+ import com .rabbitmq .client .MessageProperties ;
32
40
33
41
public class ConsumerPriorities extends BrokerTestCase {
42
+
34
43
@ Test public void validation () throws IOException {
35
44
assertFailValidation (args ("banana" ));
36
45
assertFailValidation (args (new HashMap <Object , Object >()));
@@ -50,6 +59,8 @@ private void assertFailValidation(Map<String, Object> args) throws IOException {
50
59
}
51
60
52
61
private static final int COUNT = 10 ;
62
+ private static final long DELIVERY_TIMEOUT_MS = 100 ;
63
+ private static final long CANCEL_OK_TIMEOUT_MS = 10 * 1000 ;
53
64
54
65
@ Test public void consumerPriorities () throws Exception {
55
66
String queue = channel .queueDeclare ().getQueue ();
@@ -61,13 +72,20 @@ private void assertFailValidation(Map<String, Object> args) throws IOException {
61
72
channel .basicConsume (queue , true , args (-1 ), lowConsumer );
62
73
63
74
publish (queue , COUNT , "high" );
75
+ assertContents (highConsumer , COUNT , "high" );
64
76
channel .basicCancel (high );
77
+ assertTrue (
78
+ "High priority consumer should have been cancelled" ,
79
+ highConsumer .cancelLatch .await (CANCEL_OK_TIMEOUT_MS , TimeUnit .MILLISECONDS )
80
+ );
65
81
publish (queue , COUNT , "med" );
82
+ assertContents (medConsumer , COUNT , "med" );
66
83
channel .basicCancel (med );
84
+ assertTrue (
85
+ "Medium priority consumer should have been cancelled" ,
86
+ medConsumer .cancelLatch .await (CANCEL_OK_TIMEOUT_MS , TimeUnit .MILLISECONDS )
87
+ );
67
88
publish (queue , COUNT , "low" );
68
-
69
- assertContents (highConsumer , COUNT , "high" );
70
- assertContents (medConsumer , COUNT , "med" );
71
89
assertContents (lowConsumer , COUNT , "low" );
72
90
}
73
91
@@ -77,12 +95,12 @@ private Map<String, Object> args(Object o) {
77
95
return map ;
78
96
}
79
97
80
- private void assertContents (QueueMessageConsumer c , int count , String msg ) throws InterruptedException {
98
+ private void assertContents (QueueMessageConsumer qc , int count , String msg ) throws InterruptedException {
81
99
for (int i = 0 ; i < count ; i ++) {
82
- byte [] body = c .nextDelivery (100 );
100
+ byte [] body = qc .nextDelivery (DELIVERY_TIMEOUT_MS );
83
101
assertEquals (msg , new String (body ));
84
102
}
85
- assertEquals (null , c .nextDelivery ());
103
+ assertEquals (null , qc .nextDelivery (DELIVERY_TIMEOUT_MS ));
86
104
}
87
105
88
106
private void publish (String queue , int count , String msg ) throws IOException {
@@ -91,10 +109,12 @@ private void publish(String queue, int count, String msg) throws IOException {
91
109
}
92
110
}
93
111
94
- class QueueMessageConsumer extends DefaultConsumer {
112
+ private static class QueueMessageConsumer extends DefaultConsumer {
95
113
96
114
BlockingQueue <byte []> messages = new LinkedBlockingQueue <byte []>();
97
115
116
+ CountDownLatch cancelLatch = new CountDownLatch (1 );
117
+
98
118
public QueueMessageConsumer (Channel channel ) {
99
119
super (channel );
100
120
}
@@ -104,14 +124,14 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
104
124
messages .add (body );
105
125
}
106
126
107
- byte [] nextDelivery () {
108
- return messages .poll ();
127
+ @ Override
128
+ public void handleCancelOk (String consumerTag ) {
129
+ cancelLatch .countDown ();
109
130
}
110
131
111
132
byte [] nextDelivery (long timeoutInMs ) throws InterruptedException {
112
133
return messages .poll (timeoutInMs , TimeUnit .MILLISECONDS );
113
134
}
114
135
115
136
}
116
-
117
137
}
0 commit comments