mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix Snoefake bug
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import cn.hutool.core.collection.ConcurrentHashSet;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.thread.ConcurrencyTester;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Snowflake单元测试
|
||||
* @author Looly
|
||||
@@ -43,4 +50,20 @@ public class SnowflakeTest {
|
||||
Assert.assertEquals(2, idWorker.getDataCenterId(nextId));
|
||||
Assert.assertTrue(idWorker.getGenerateDateTime(nextId) - System.currentTimeMillis() < 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void uniqueTest(){
|
||||
// 测试并发环境下生成ID是否重复
|
||||
Snowflake snowflake = IdUtil.createSnowflake(0, 0);
|
||||
|
||||
Set<Long> ids = new ConcurrentHashSet<>();
|
||||
ConcurrencyTester tester = ThreadUtil.concurrencyTest(100, () -> {
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
if(false == ids.add(snowflake.nextId())){
|
||||
throw new UtilException("重复ID!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user