Back to Repositories

Testing RabbitMQ RPC Producer Implementation in SpringBoot-Labs

This test suite validates RabbitMQ RPC (Remote Procedure Call) functionality in a Spring Boot application, focusing on synchronous message sending and response handling. The tests ensure proper producer-consumer communication with blocking mechanisms for guaranteed message consumption.

Test Coverage Overview

The test suite covers synchronous RPC messaging patterns in RabbitMQ, focusing on producer operations.

  • Tests synchronous message sending with response handling
  • Validates message ID generation and correlation
  • Ensures proper message consumption through blocking mechanisms
  • Includes logging verification for successful message delivery

Implementation Analysis

The implementation utilizes Spring Boot’s test framework with JUnit for RabbitMQ producer testing.

Key patterns include:
  • SpringRunner for test execution context
  • Dependency injection for producer components
  • CountDownLatch for synchronization control
  • System time-based message ID generation

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Spring Boot Test context
  • SLF4J logging framework
  • Spring’s dependency injection
  • RabbitMQ client libraries
  • CountDownLatch for thread synchronization

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation using Spring Boot test context
  • Effective logging for test execution tracking
  • Synchronization handling for asynchronous operations
  • Clear test method naming conventions
  • Commented out test cases for alternative scenarios

yudaocode/springboot-labs

lab-04-rabbitmq/lab-04-rabbitmq-demo-rpc/src/test/java/cn/iocoder/springboot/lab04/rabbitmqdemo/producer/Demo14ProducerTest.java

            
package cn.iocoder.springboot.lab04.rabbitmqdemo.producer;

import cn.iocoder.springboot.lab04.rabbitmqdemo.Application;
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 Demo14ProducerTest {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private Demo14Producer producer;

    @Test
    public void testSyncSend() throws InterruptedException {
        int id = (int) (System.currentTimeMillis() / 1000);
        String result = producer.syncSend(id);
        logger.info("[testSyncSend][发送编号:[{}] 发送成功 消费结果:[{}]]", id, result);

        // 阻塞等待,保证消费
        new CountDownLatch(1).await();
    }

//    @Test
//    public void testSyncSend2() throws InterruptedException {
//        int id = (int) (System.currentTimeMillis() / 1000);
//        String result = producer.syncSend(id);
//        logger.info("[testSyncSend][发送编号:[{}] 发送成功 消费结果:[{}]]", id, result);
//
//        // 阻塞等待,保证消费
//        new CountDownLatch(1).await();
//    }

}