From e18ed376aca4e5089ce659a0ba3f0acb37f417a2 Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Wed, 19 Jun 2024 15:09:01 +0800 Subject: [PATCH] Fix CommandDaoImplTest might failed (#16175) --- .../repository/impl/CommandDaoImplTest.java | 57 ++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/repository/impl/CommandDaoImplTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/repository/impl/CommandDaoImplTest.java index 85867ef3b5..7743a8b8a1 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/repository/impl/CommandDaoImplTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/repository/impl/CommandDaoImplTest.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.dao.repository.impl; import static com.google.common.truth.Truth.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.CommandType; @@ -28,45 +29,63 @@ import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.dao.BaseDaoTest; import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.mapper.CommandMapper; import org.apache.dolphinscheduler.dao.repository.CommandDao; import org.apache.commons.lang3.RandomUtils; import java.util.List; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.RepeatedTest; import org.springframework.beans.factory.annotation.Autowired; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; + class CommandDaoImplTest extends BaseDaoTest { @Autowired private CommandDao commandDao; - @Test + @Autowired + private CommandMapper commandMapper; + + @RepeatedTest(value = 100) void fetchCommandByIdSlot() { - int commandSize = RandomUtils.nextInt(1, 1000); - for (int i = 0; i < commandSize; i++) { - createCommand(CommandType.START_PROCESS, 0); - } + // clear all commands + commandMapper.delete(new QueryWrapper().ge("id", -1)); + int totalSlot = RandomUtils.nextInt(1, 10); int currentSlotIndex = RandomUtils.nextInt(0, totalSlot); int fetchSize = RandomUtils.nextInt(10, 100); - for (int i = 1; i < 5; i++) { - int idStep = i; - List commands = commandDao.queryCommandByIdSlot(currentSlotIndex, totalSlot, idStep, fetchSize); - assertThat(commands.size()).isGreaterThan(0); - assertThat(commands.size()) - .isEqualTo(commandDao.queryAll() - .stream() - .filter(command -> (command.getId() / idStep) % totalSlot == currentSlotIndex) - .limit(fetchSize) - .count()); - + int idStep = RandomUtils.nextInt(1, 5); + int commandSize = RandomUtils.nextInt(currentSlotIndex, 1000); + // Generate commandSize commands + int id = 0; + for (int j = 0; j < commandSize; j++) { + Command command = generateCommand(CommandType.START_PROCESS, 0); + command.setId(id); + id += idStep; + commandDao.insert(command); } + List commands = commandDao.queryCommandByIdSlot(currentSlotIndex, totalSlot, idStep, fetchSize); + assertFalse(commands.isEmpty(), + "Commands should not be empty, currentSlotIndex: " + currentSlotIndex + + ", totalSlot: " + totalSlot + + ", idStep: " + idStep + + ", fetchSize: " + fetchSize + + ", total command size: " + commandSize + + ", total commands: " + commandDao.queryAll()); + assertThat(commands.size()) + .isEqualTo(commandDao.queryAll() + .stream() + .filter(command -> (command.getId() / idStep) % totalSlot == currentSlotIndex) + .limit(fetchSize) + .count()); + } - private void createCommand(CommandType commandType, int processDefinitionCode) { + private Command generateCommand(CommandType commandType, int processDefinitionCode) { Command command = new Command(); command.setCommandType(commandType); command.setProcessDefinitionCode(processDefinitionCode); @@ -83,6 +102,6 @@ class CommandDaoImplTest extends BaseDaoTest { command.setWorkerGroup(Constants.DEFAULT_WORKER_GROUP); command.setProcessInstanceId(0); command.setProcessDefinitionVersion(0); - commandDao.insert(command); + return command; } }