fix Snoefake bug

This commit is contained in:
Looly
2020-11-10 11:28:09 +08:00
parent e5574fa173
commit 6216eb9994
2 changed files with 36 additions and 11 deletions

View File

@@ -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");
}
}
});
}
}