@Test
public void testListenerSendsMessageAndThenContainerCommits() throws Exception {
ConnectionFactory connectionFactory = createConnectionFactory();
RabbitTemplate template = new RabbitTemplate(connectionFactory);
new RabbitAdmin(connectionFactory).declareQueue(sendQueue);
acknowledgeMode = AcknowledgeMode.AUTO;
transactional = true;
CountDownLatch latch = new CountDownLatch(1);
container = createContainer(queue.getName(), new ChannelSenderListener(sendQueue.getName(), latch, false),
connectionFactory);
template.convertAndSend(queue.getName(), "foo");
int timeout = getTimeout();
logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
boolean waited = latch.await(timeout, TimeUnit.SECONDS);
assertTrue("Timed out waiting for message", waited);
// Give message time to reach broker (intermittent test failures)!
Thread.sleep(500L);
// All messages committed
byte[] bytes = (byte[]) template.receiveAndConvert(sendQueue.getName());
assertNotNull(bytes);
assertEquals("bar", new String(bytes));
assertEquals(null, template.receiveAndConvert(queue.getName()));
((DisposableBean) connectionFactory).destroy();
}