Пример #1
0
 public async Task Should_Execute_Async()
 {
     //Act
     await _lockService.ExecuteInLockAsync("test", async() =>
     {
         await Task.Delay(3000);
     });
 }
        public async Task Should_AquireLock_Timeout()
        {
            var lockKey = Guid.NewGuid().ToString();

            var tasks = new List <Task>();

            for (int i = 0; i < 2; i++)
            {
                tasks.Add(_lockService.ExecuteInLockAsync(lockKey, async() =>
                {
                    await Task.Delay(5 * 1000);
                }));
            }

            await Assert.ThrowsAsync <DistributedLockTimeoutException>(async() => await Task.WhenAll(tasks));
        }
Пример #3
0
        private static async Task Main(string[] args)
        {
            var assemblies = new[] { Assembly.GetExecutingAssembly() };

            var enode = ECommonConfiguration.Create()
                        .UseAutofac()
                        .RegisterCommonComponents()
                        .UseLog4Net()
                        .UseJsonNet()
                        .CreateENode(new ConfigurationSetting())
                        .RegisterBusinessComponents(assemblies)
                        .RegisterENodeComponents()
                        .UseRedisLockService();

            enode.GetCommonConfiguration()
            .BuildContainer();

            enode.InitializeBusinessAssemblies(assemblies)
            .InitializeRedisLockService(_redisOptions);

            _lockService = ObjectContainer.Resolve <ILockService>();

            var listDics = new List <(int index, Dictionary <int, int> dic)>();

            for (int i = 0; i < 10; i++)
            {
                listDics.Add((i, new Dictionary <int, int>()));
            }
            var tasks     = new List <Task>();
            var stopWatch = new Stopwatch();

            var lockKey = Guid.NewGuid().ToString();

            stopWatch.Start();
            for (int i = 0; i < 1000; i++)
            {
                foreach (var item in listDics)
                {
                    tasks.Add(_lockService.ExecuteInLockAsync($"{lockKey}-{item.index}", async() =>
                    {
                        await Task.Delay(10);
                        item.dic.Add(item.dic.Count, Thread.CurrentThread.GetHashCode());
                        Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss,fff")} {Thread.CurrentThread.GetHashCode()} {item.dic.Count} complete");
                    }));
                }
            }
            await Task.WhenAll(tasks);

            stopWatch.Stop();

            Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss,fff")} all complete use time[{stopWatch.ElapsedMilliseconds}]");
            await Task.Delay(60000);

            Console.ReadKey();
        }
Пример #4
0
        private static void Main(string[] args)
        {
            var assemblies = new[] { Assembly.GetExecutingAssembly() };

            var enode = ECommonConfiguration.Create()
                        .UseAutofac()
                        .RegisterCommonComponents()
                        .UseLog4Net()
                        .UseJsonNet()
                        .CreateENode(new ConfigurationSetting())
                        .RegisterBusinessComponents(assemblies)
                        .RegisterENodeComponents()
                        .UseRedisLockService();

            enode.GetCommonConfiguration()
            .BuildContainer();

            enode.InitializeBusinessAssemblies(assemblies)
            .InitializeRedisLockService(_redisOptions);

            _lockService = ObjectContainer.Resolve <ILockService>();

            var dic   = new Dictionary <int, int>();
            var tasks = new List <Task>();

            for (int i = 0; i < 400; i++)
            {
                tasks.Add(
                    _lockService.ExecuteInLockAsync("test", t =>
                {
                    dic.Add((int)t, System.Threading.Thread.CurrentThread.GetHashCode());
                }, i));
            }

            Task.WaitAll(tasks.ToArray());

            Console.ReadKey();
        }