2
2
3
3
import com .rabbitmq .client .*;
4
4
import com .rabbitmq .client .impl .nio .NioParams ;
5
+ import org .junit .After ;
6
+ import org .junit .Before ;
5
7
import org .junit .Test ;
6
8
7
9
import java .io .IOException ;
14
16
*/
15
17
public class JavaNioTest {
16
18
19
+ public static final String QUEUE = "nio.queue" ;
20
+
21
+ private Connection testConnection ;
22
+
23
+ @ Before
24
+ public void init () throws Exception {
25
+ ConnectionFactory connectionFactory = new ConnectionFactory ();
26
+ connectionFactory .useNio ();
27
+ testConnection = connectionFactory .newConnection ();
28
+ }
29
+
30
+ @ After
31
+ public void tearDown () throws Exception {
32
+ if (testConnection != null ) {
33
+ testConnection .createChannel ().queueDelete (QUEUE );
34
+ testConnection .close ();
35
+ }
36
+ }
37
+
17
38
@ Test
18
39
public void connection () throws Exception {
19
40
CountDownLatch latch = new CountDownLatch (1 );
@@ -101,6 +122,18 @@ public void nioLoopCleaning() throws Exception {
101
122
}
102
123
}
103
124
125
+ @ Test public void messageSize () throws Exception {
126
+ for (int i = 0 ; i < 50 ; i ++) {
127
+ sendAndVerifyMessage (testConnection , 76390 );
128
+ }
129
+ }
130
+
131
+ private void sendAndVerifyMessage (Connection connection , int size ) throws Exception {
132
+ CountDownLatch latch = new CountDownLatch (1 );
133
+ boolean messageReceived = basicGetBasicConsume (connection , QUEUE , latch , size );
134
+ assertTrue ("Message has not been received" , messageReceived );
135
+ }
136
+
104
137
private Connection basicGetBasicConsume (ConnectionFactory connectionFactory , String queue , final CountDownLatch latch )
105
138
throws IOException , TimeoutException {
106
139
Connection connection = connectionFactory .newConnection ();
@@ -121,6 +154,28 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
121
154
return connection ;
122
155
}
123
156
157
+ private boolean basicGetBasicConsume (Connection connection , String queue , final CountDownLatch latch , int msgSize )
158
+ throws Exception {
159
+ Channel channel = connection .createChannel ();
160
+ channel .queueDeclare (queue , false , false , false , null );
161
+ channel .queuePurge (queue );
162
+
163
+ channel .basicPublish ("" , queue , null , new byte [msgSize ]);
164
+
165
+ final String tag = channel .basicConsume (queue , false , new DefaultConsumer (channel ) {
166
+
167
+ @ Override
168
+ public void handleDelivery (String consumerTag , Envelope envelope , AMQP .BasicProperties properties , byte [] body ) throws IOException {
169
+ getChannel ().basicAck (envelope .getDeliveryTag (), false );
170
+ latch .countDown ();
171
+ }
172
+ });
173
+
174
+ boolean done = latch .await (20 , TimeUnit .SECONDS );
175
+ channel .basicCancel (tag );
176
+ return done ;
177
+ }
178
+
124
179
private void safeClose (Connection connection ) {
125
180
if (connection != null ) {
126
181
try {
0 commit comments