Testing RocketMQ Producer Messaging Patterns in SpringBoot-Labs
This test suite validates RocketMQ message production functionality in a Spring Boot application, focusing on synchronous, asynchronous, and one-way message sending patterns. It demonstrates comprehensive producer testing with various delivery mechanisms using the RocketMQ ONS client.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
yudaocode/springboot-labs
lab-31/lab-31-rocketmq-ons/src/test/java/cn/iocoder/springboot/lab31/rocketmqdemo/producer/Demo01ProducerTest.java
package cn.iocoder.springboot.lab31.rocketmqdemo.producer;
import cn.iocoder.springboot.lab31.rocketmqdemo.Application;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.CountDownLatch;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Demo01ProducerTest {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private Demo01Producer producer;
@Test
public void testSyncSend() throws InterruptedException {
int id = (int) (System.currentTimeMillis() / 1000);
SendResult result = producer.syncSend(id);
logger.info("[testSyncSend][发送编号:[{}] 发送结果:[{}]]", id, result);
// 阻塞等待,保证消费
new CountDownLatch(1).await();
}
@Test
public void testASyncSend() throws InterruptedException {
int id = (int) (System.currentTimeMillis() / 1000);
producer.asyncSend(id, new SendCallback() {
@Override
public void onSuccess(SendResult result) {
logger.info("[testASyncSend][发送编号:[{}] 发送成功,结果为:[{}]]", id, result);
}
@Override
public void onException(Throwable e) {
logger.info("[testASyncSend][发送编号:[{}] 发送异常]]", id, e);
}
});
// 阻塞等待,保证消费
new CountDownLatch(1).await();
}
@Test
public void testOnewaySend() throws InterruptedException {
int id = (int) (System.currentTimeMillis() / 1000);
producer.onewaySend(id);
logger.info("[testOnewaySend][发送编号:[{}] 发送完成]", id);
// 阻塞等待,保证消费
new CountDownLatch(1).await();
}
}