Forum Discussion

thePantz's avatar
thePantz
Contributor
5 years ago

AMQP: Verify that no messages are on a queue

Hello,   I'm trying to write a test case where the expected result is that a message was NOT queued. The AMQP Recieve step will fail once the timeout is reached so I cannot perform a verification ...
  • thePantz's avatar
    thePantz
    5 years ago

    Hello,

     

    The system under test uses RabbitMQ. Unfortunately RabbitMQ does not support JMS... only AMQP.

    After further research I've decided to use RabbitMQs Java Client API to verify this.

     

    I also found some useful SoapUI examples on RabbitMQs github.

     

    I will do something like this:

    import com.rabbitmq.client.*
    
    
    String QUEUE_NAME = testRunner.testCase.testSuite.getProperty("queue_name")
    
    ConnectionFactory factory = new ConnectionFactory()
    // uncomment to change defaults
    //factory.setUsername("guest")
    //factory.setPassword("guest")
    //factory.setVirtualHost("/")
    //factory.setHost("localhost")
    //factory.setPort(5672)
    Connection connection = factory.newConnection()
    Channel channel = connection.createChannel()
    
    channel.queueDeclare(QUEUE_NAME, false, false, false, null)
    
    String message
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        message = new String(body, "UTF-8")
        log.info " [x] Received '$message'"
      }
    }
    channel.basicConsume(QUEUE_NAME, true, consumer)
    
    // Wait for async call to finish int stop = 0 while(message == null && stop++ < 20) { log.info " [*] Waiting for messages." sleep 500 }

    //Check that message should still be null after waiting for Async
    assert message == null
    channel.close() connection.close()