chore: 二级用例001开始

This commit is contained in:
CaptainB 2024-01-10 10:04:09 +08:00 committed by 刘瑞斌
parent 9670672158
commit 3a59afebfa
3 changed files with 15 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package io.metersphere.sdk.constants;
public enum ApplicationNumScope {
API_DEFINITION,
API_TEST_CASE,
API_MOCK,
API_SCENARIO,
UI_SCENARIO,

View File

@ -14,6 +14,8 @@ import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class NumGenerator {
private static final long INIT = 100001L; // 代表从100001开始各种domain的 num
@ -23,20 +25,24 @@ public class NumGenerator {
private static StringRedisTemplate stringRedisTemplate;
private static ProjectMapper projectMapper;
private static final List<ApplicationNumScope> SUB_NUM = List.of(ApplicationNumScope.API_TEST_CASE, ApplicationNumScope.API_MOCK);
/**
* @param prefix 前缀: PROJECT_ID, 或者 PROJECT_ID + "_" + DOMAIN 例如接口用例的前缀为: 100001_12345
* @param scope 用例类型
*/
public static long nextNum(String prefix, ApplicationNumScope scope) {
RIdGenerator idGenerator = redisson.getIdGenerator(prefix + "_" + scope.name());
// 每次都尝试初始化容量为1只有一个线程可以初始化成功
if (scope.equals(ApplicationNumScope.API_TEST_CASE)) {
// 二级的用例
idGenerator.tryInit(Long.parseLong(prefix.split("_")[1] + INIT), LIMIT);
// 二级的用例
if (SUB_NUM.contains(scope)) {
// 每次都尝试初始化容量为1只有一个线程可以初始化成功
idGenerator.tryInit(1, LIMIT);
return Long.parseLong(prefix.split("_")[1] + StringUtils.leftPad(String.valueOf(idGenerator.nextId()), 3, "0"));
} else {
// 每次都尝试初始化容量为1只有一个线程可以初始化成功
idGenerator.tryInit(INIT, LIMIT);
return idGenerator.nextId();
}
return idGenerator.nextId();
}
@QuartzScheduled(cron = "0 1 0 * * ?")

View File

@ -48,8 +48,8 @@ public class RIdGeneratorTests {
public void testId2() throws Exception {
String projectId = "100001";
long capacity = 10; // 容量代表每个项目最多可以生成多少个id
long init = 100001L; // 代表从1000001开始项目的 num
long capacity = 2000; // 容量代表每个项目最多可以生成多少个id
long init = 1; // 代表从1000001开始项目的 num
long apiNum = 100005;
long start = System.currentTimeMillis();
AtomicLong atomicLong = new AtomicLong(init);
@ -69,7 +69,7 @@ public class RIdGeneratorTests {
}
executorService.close();
System.out.println("耗时: " + (System.currentTimeMillis() - start) + "ms");
Assertions.assertEquals(capacity + Long.parseLong(apiNum + "" + init), atomicLong.get() + 1);
Assertions.assertEquals(Long.parseLong("" + apiNum + capacity), atomicLong.get());
}
@Test