rabbitmq-mock icon indicating copy to clipboard operation
rabbitmq-mock copied to clipboard

rabbitTemplate receive() does not dequeue message

Open lz000 opened this issue 2 years ago • 1 comments

I have this simple test. As you can see, calling rabbitTemplate.receive() did not remove message from the queue, although the same message was received every time I called this method. This issue does not happen if I hook to a real rabbitmq

@TestConfiguration
public class TestConfig {

    @Bean
    @Primary
    ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory(new MockConnectionFactory());
    }

    @Bean
    @Primary
    public RabbitAdmin getTestRabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean
    @Primary
    public RabbitTemplate getTestRabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
}


@SpringBootTest
@Import(TestConfig.class)
class SampleTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Test
    void test() throws Exception {
         rabbitTemplate.send("test-exchange", "test-key", new Message("test".getBytes(StandardCharsets.UTF_8)));

        Thread.sleep(500);
        QueueInformation info0 = rabbitAdmin.getQueueInfo("test-queue");  // QueueInformation [name=test-queue, messageCount=1, consumerCount=0]

        Message message1 = rabbitTemplate.receive("test-queue", -1);
        Thread.sleep(500);
        QueueInformation info1 = rabbitAdmin.getQueueInfo("test-queue");  // QueueInformation [name=test-queue, messageCount=1, consumerCount=1]

        Message message2 = rabbitTemplate.receive("test-queue", -1);
        Thread.sleep(500);
        QueueInformation info2 = rabbitAdmin.getQueueInfo("test-queue");  // QueueInformation [name=test-queue, messageCount=1, consumerCount=1]
    }
}

lz000 avatar Jul 05 '22 17:07 lz000

I am facing the same issue which is quite unexpected i would say.
Is there any documentation that i am missing?

Hatzen avatar May 22 '23 21:05 Hatzen