tests/DotNetCommon.Test/DistributeGeneratorTests.cs

(开头部分) 12KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using NUnit.Framework;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DotNetCommon.Test
{
    [TestFixture]
    public class DistributeGeneratorTests
    {
        [Test]
        public void SnowflakeIdWorkerCtorTests()
        {
            Shouldly.Should.NotThrow(() =>
            {
                new SnowflakeIdWorker(1);
                new SnowflakeIdWorker(0);
                new SnowflakeIdWorker(1023);
                new SnowflakeIdWorker(1, DateTime.Parse("2021-01-01"));
                new SnowflakeIdWorker(1, null, timeLength: 44);
            });

            //机器Id和机器Id的bit位数不匹配
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1024));
            //起始时间必须大于2020年
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, DateTime.Parse("2019-01-01")));
            //时间戳取值: 28-44
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, timeLength: 45));
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, timeLength: 27));
            //机器id位数: 5-18
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, machineLength: 4));
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, machineLength: 19));
            //序列号位数: 2-24
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, sequenceLength: 1));
            Shouldly.Should.Throw<Exception>(() => new SnowflakeIdWorker(1, sequenceLength: 25));

        }

        [Test]
        public void SnowflakeIdWorkerTests()
        {
            //雪花算法:连续生成2万个id
            Shouldly.Should.NotThrow(() =>
            {
                var snow = new SnowflakeIdWorker(1);
                var dic = new Dictionary<long, string>();
                for (var i = 0; i < 20000; i++)
                {
                    dic.Add(snow.NextId(), null);
                }
                var dic2 = new Dictionary<long, string>();
                foreach (var i in dic)
                {
                    var id = i.Key;
后面还有 265 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。