今天在写spring boot 单元测试的时候,往redis的stream写入数据,消息立马就没有了,测试类如下
@SpringBootTest(classes = {DemoApplication.class}, properties = {"spring.profiles.active=unittest"})
class DemoApplicationTests {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private RedisService redisService;
@Test
public void testAdd() throws InterruptedException {
final int max_thread = 1;
CountDownLatch countDownLatch = new CountDownLatch(max_thread);
long begin = System.currentTimeMillis();
for (int i = 0; i < max_thread; i++) {
redisService.xaddBatch(countDownLatch);
}
countDownLatch.await();
long end = System.currentTimeMillis();
logger.info("总共写入数据{}条数据,总耗时{}s", RedisService.TOTAL_COUNT, (end - begin) / 1000);
}
}
原来是代码里头有一个服务,一启动就自动读消息导致的
@Service
public class CustomStreamListenerProcessor {
@Autowired
private Executor executor;
@Autowired(required = false)
private List<CustomStreamListener> listeners;
@Autowired
private StringRedisTemplate template;
@PostConstruct
public void doReadMsg() {
if (listeners != null && listeners.size() > 0) {
listeners.stream().forEach(item -> executor.execute(new CustomStreamListenerWrapper(item, template)));
}
}
}
@PostConstruct注解表示这个类初始化完成执行这个方法,在这个方法里头会自动消费消息,导致我始终无法读到消息
怎么办呢,最简单的办法是,把CustomStreamListenerProcessor 的@Service的注解去掉,然后测试完再加回去,这显然不是好办法。
其中一个办法使用Profile,就是是往CustomStreamListenerProcessor 的@Service加一个@Profile("!unittest"),表示只有在非unittest情况下才能注册使用。然后再测试类中修改下面的代码,加上
@SpringBootTest(classes = {DemoApplication.class}, properties = {"spring.profiles.active=unittest"})
表示启用unittest的profile
这就搞定了,不要改来改去代码,一些服务类只在非测试profiles才注册使用