完成 ID 生成器的单元测试

This commit is contained in:
2024-12-27 16:32:10 +08:00
parent fb5ff43ed6
commit a887771565
4 changed files with 107 additions and 48 deletions

View File

@@ -925,9 +925,10 @@ public class AssertToolsTests {
// #region - Condition
static final class MyException extends RuntimeException {}
@Test
void testCheckCondition() {
class MyException extends RuntimeException {}
AssertTools.checkCondition(true, MyException::new);

View File

@@ -0,0 +1,65 @@
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import cn.hutool.core.collection.ConcurrentHashSet;
public class IdGeneratorTests {
final ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
@Test
void testSnowflakeIdGenerator() { // NOSONAR
final SnowflakeIdGenerator snowflake = new SnowflakeIdGenerator(0, 0);
final Set<Long> ids = new ConcurrentHashSet<>();
for (int i = 0; i < 10000; i++) {
executor.execute(() -> {
for (int j = 0; j < 50000; j++) {
if (false == ids.add(snowflake.nextId())) {
throw new RuntimeException("重复ID");
}
}
});
}
}
@Test
void testIdWorker() { // NOSONAR
final IdWorker idWorker = new IdWorker(0L);
final Set<Long> ids = new ConcurrentHashSet<>();
for (int i = 0; i < 10000; i++) {
executor.execute(() -> {
for (int j = 0; j < 50000; j++) {
if (false == ids.add(idWorker.nextId())) {
throw new RuntimeException("重复ID");
}
}
});
executor.execute(() -> {
for (int j = 0; j < 50000; j++) {
if (false == ids.add(IdGenerator.nextSnowflakeId(0))) {
throw new RuntimeException("重复ID");
}
}
});
}
}
@Test
void testToSimpleString() {
UUID id = UUID.randomUUID();
assertEquals(id.toString().replaceAll("-", ""),
IdGenerator.toSimpleString(id));
}
}