示例#1
0
        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);
        }
示例#2
0
        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();
            }
        }
示例#3
0
文件: Program.cs 项目: sampsonye/Nice
        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();
        }
示例#4
0
 /// <summary>
 /// 获取ID
 /// </summary>
 /// <returns></returns>
 public static long GenId()
 {
     return(snowflake.NextId());
 }
示例#5
0
 /// <summary>
 /// 获取分布式唯一Id
 /// </summary>
 /// <returns></returns>
 public long GetId()
 {
     return(snowflake.NextId());
 }
示例#6
0
文件: Program.cs 项目: sampsonye/Nice
 public void TestGenerate()
 {
     var id = _snowflake.NextId();
 }