22
33import com .rabbitmq .client .*;
44import com .rabbitmq .client .impl .nio .NioParams ;
5+ import org .junit .After ;
6+ import org .junit .Before ;
57import org .junit .Test ;
68
79import java .io .IOException ;
1416 */
1517public class JavaNioTest {
1618
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+
1738 @ Test
1839 public void connection () throws Exception {
1940 CountDownLatch latch = new CountDownLatch (1 );
@@ -101,6 +122,18 @@ public void nioLoopCleaning() throws Exception {
101122 }
102123 }
103124
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+
104137 private Connection basicGetBasicConsume (ConnectionFactory connectionFactory , String queue , final CountDownLatch latch )
105138 throws IOException , TimeoutException {
106139 Connection connection = connectionFactory .newConnection ();
@@ -121,6 +154,28 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
121154 return connection ;
122155 }
123156
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+
124179 private void safeClose (Connection connection ) {
125180 if (connection != null ) {
126181 try {
0 commit comments