This commit is contained in:
Looly 2021-08-11 09:20:32 +08:00
parent 163ce2a67c
commit b048cc7fd7
2 changed files with 10 additions and 11 deletions

View File

@ -2,7 +2,6 @@ package cn.hutool.core.lang;
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 cn.hutool.core.util.StrUtil;
@ -19,7 +18,7 @@ import java.util.Set;
*
*/
public class SnowflakeTest {
@Test
public void snowflakeTest1(){
//构建Snowflake提供终端ID和数据中心ID
@ -27,11 +26,11 @@ public class SnowflakeTest {
long nextId = idWorker.nextId();
Assert.assertTrue(nextId > 0);
}
@Test
public void snowflakeTest(){
HashSet<Long> hashSet = new HashSet<>();
//构建Snowflake提供终端ID和数据中心ID
Snowflake idWorker = new Snowflake(0, 0);
for (int i = 0; i < 1000; i++) {
@ -40,13 +39,13 @@ public class SnowflakeTest {
}
Assert.assertEquals(1000L, hashSet.size());
}
@Test
public void snowflakeGetTest(){
//构建Snowflake提供终端ID和数据中心ID
Snowflake idWorker = new Snowflake(1, 2);
long nextId = idWorker.nextId();
Assert.assertEquals(1, idWorker.getWorkerId(nextId));
Assert.assertEquals(2, idWorker.getDataCenterId(nextId));
Assert.assertTrue(idWorker.getGenerateDateTime(nextId) - System.currentTimeMillis() < 10);
@ -56,10 +55,10 @@ public class SnowflakeTest {
@Ignore
public void uniqueTest(){
// 测试并发环境下生成ID是否重复
Snowflake snowflake = IdUtil.createSnowflake(0, 0);
Snowflake snowflake = IdUtil.getSnowflake(0, 0);
Set<Long> ids = new ConcurrentHashSet<>();
ConcurrencyTester tester = ThreadUtil.concurrencyTest(100, () -> {
ThreadUtil.concurrencyTest(100, () -> {
for (int i = 0; i < 5000; i++) {
if(false == ids.add(snowflake.nextId())){
throw new UtilException("重复ID");

View File

@ -68,8 +68,8 @@ public class IdUtilTest {
}
@Test
public void createSnowflakeTest() {
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
public void getSnowflakeTest() {
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
long id = snowflake.nextId();
Assert.assertTrue(id > 0);
}
@ -78,7 +78,7 @@ public class IdUtilTest {
@Ignore
public void snowflakeBenchTest() {
final Set<Long> set = new ConcurrentHashSet<>();
final Snowflake snowflake = IdUtil.createSnowflake(1, 1);
final Snowflake snowflake = IdUtil.getSnowflake(1, 1);
//线程数
int threadCount = 100;