Fix CommandDaoImplTest might failed (#16175)

This commit is contained in:
Wenjun Ruan 2024-06-19 15:09:01 +08:00 committed by GitHub
parent 6644abd5f7
commit e18ed376ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<Command>().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<Command> 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<Command> 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;
}
}