public void BaseTest() { var snowflake = new Snowflake(0, 0, 0); var val = snowflake.NextId(); var val2 = snowflake.NextId(); Console.WriteLine($"val:{val} val2:{val2}"); Assert.InRange(val, 0L, long.MaxValue); Assert.InRange(val2, 0L, long.MaxValue); Assert.True(val < val2); }
public void NextIdTest() { var snowflake = new Snowflake(10, 21); var list = new List <long>(10_0000); var tasks = new Task[100]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Run(() => { for (int i = 0; i < 1000; i++) { var id = snowflake.NextId(); list.Add(id); } }); } Task.WaitAll(tasks); var set = new HashSet <long>(list); if (set.Count != list.Count) { Assert.Fail(); } }
static void Main(string[] args) { var snowflake = new Snowflake(1, 1, 0); for (int i = 0; i < 1000; i++) { Console.WriteLine(snowflake.NextId()); } Console.ReadLine(); }
/// <summary> /// 获取ID /// </summary> /// <returns></returns> public static long GenId() { return(snowflake.NextId()); }
/// <summary> /// 获取分布式唯一Id /// </summary> /// <returns></returns> public long GetId() { return(snowflake.NextId()); }
public void TestGenerate() { var id = _snowflake.NextId(); }