Esempio n. 1
0
        public void GetLock_With_Single_Thread_Test()
        {
            var rl = new RedisLock(rk, rs, defaultExpire); //test logic:expireDate is too small
            var r1 = rl.GetLock();
            var r2 = rl.GetLock();

            Assert.True(r1 && r2);
        }
Esempio n. 2
0
        public void GetLock_With_Multi_Threads_Test()
        {
            int threadCount = 10;
            var rl          = new RedisLock(rk, rs, defaultExpire);
            var ls          = new List <string>();
            var count       = 0;

            rk.Del("lockObj");

            while (count <= 30)
            {
                ThreadPool.QueueUserWorkItem(s =>
                {
                    if (rl.GetLock())
                    {
                        ls.Add("got!");
                    }

                    count++;
                });
                //Task.Run(new Action(() =>
                //{
                //    if (rl.GetLock())
                //        ls.Add("got!");

                //    count++;
                //}));

                Thread.Sleep(10);
            }
            //for (int i = 0; i < threadCount; i++)
            //{
            //    new Thread(() =>
            //    {
            //        if (rl.GetLock())
            //            ls.Add("got!");
            //    }).Start();

            //    Thread.Sleep(5);
            //}

            Thread.Sleep(10000);

            Assert.True(ls.Count > 0);
        }