2020import com .rabbitmq .client .test .BrokerTestCase ;
2121import com .rabbitmq .client .AMQP ;
2222import com .rabbitmq .client .GetResponse ;
23+ import com .rabbitmq .client .ReturnListener ;
24+ import com .rabbitmq .utility .BlockingCell ;
2325
2426import java .io .IOException ;
2527import java .util .List ;
@@ -34,6 +36,8 @@ public class Routing extends BrokerTestCase
3436 protected final String Q1 = "foo" ;
3537 protected final String Q2 = "bar" ;
3638
39+ private volatile BlockingCell <Integer > returnCell ;
40+
3741 protected void createResources () throws IOException {
3842 channel .exchangeDeclare (E , "direct" );
3943 channel .queueDeclare (Q1 , false , false , false , null );
@@ -228,5 +232,36 @@ public void testHeadersRouting() throws Exception {
228232 checkGet (Q2 , false );
229233 }
230234
231- }
235+ public void testBasicReturn () throws Exception {
236+ channel .addReturnListener (new ReturnListener () {
237+ public void handleReturn (int replyCode ,
238+ String replyText ,
239+ String exchange ,
240+ String routingKey ,
241+ AMQP .BasicProperties properties ,
242+ byte [] body )
243+ throws IOException {
244+ Routing .this .returnCell .set (replyCode );
245+ }
246+ });
247+ returnCell = new BlockingCell <Integer >();
248+ channel .basicPublish ("" , "unknown" , true , false , null , "mandatory1" .getBytes ());
249+ int replyCode = returnCell .uninterruptibleGet ();
250+ assertEquals (replyCode , AMQP .NO_ROUTE );
251+
252+ returnCell = new BlockingCell <Integer >();
253+ channel .basicPublish ("" , Q1 , true , false , null , "mandatory2" .getBytes ());
254+ GetResponse r = channel .basicGet (Q1 , true );
255+ assertNotNull (r );
256+ assertEquals (new String (r .getBody ()), "mandatory2" );
257+
258+ channel .basicPublish ("" , Q1 , false , true , null , "immediate" .getBytes ());
259+ try {
260+ channel .basicQos (0 ); //flush
261+ fail ("basic.publish{immediate=true} should not be supported" );
262+ } catch (IOException ioe ) {
263+ checkShutdownSignal (AMQP .NOT_IMPLEMENTED , ioe );
264+ }
265+ }
232266
267+ }
0 commit comments