Example #1
0
        public void Test()
        {
            var lockParser = new InstanceLockParser <Model>(nameof(NStandard), x => x.Year, x => x.Month, x => x.Sex, x => "const");
            var model      = new Model {
                Year = 2012, Month = 4,
            };

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var result = Concurrency.Run(resultId =>
            {
                using (var _lock = lockParser.Parse(model).TryBegin(500))
                {
                    if (_lock.Value)
                    {
                        Thread.Sleep(1000);
                        return("Entered");
                    }
                    else
                    {
                        return("Timeout");
                    }
                }
            }, level: 2, threadCount: 2);

            stopwatch.Stop();

            Assert.Equal(1, result.Values.Count(x => x == "Entered"));
            Assert.Equal(1, result.Values.Count(x => x == "Timeout"));
            Assert.True(stopwatch.ElapsedMilliseconds < 2000);
        }
Example #2
0
        public void Test()
        {
            var lockParser = new TypeLockParser(nameof(NStandard));

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var result = Concurrency.Run(resultId =>
            {
                using (var _lock = lockParser.Parse <TypeLockTest>().TryBegin(500))
                {
                    if (_lock.Value)
                    {
                        Thread.Sleep(1000);
                        return("Entered");
                    }
                    else
                    {
                        return("Timeout");
                    }
                }
            }, level: 2, threadCount: 2);

            stopwatch.Stop();

            Assert.Equal(1, result.Values.Count(x => x == "Entered"));
            Assert.Equal(1, result.Values.Count(x => x == "Timeout"));
            Assert.True(stopwatch.ElapsedMilliseconds < 1900);
        }
Example #3
0
        public void Test1()
        {
            var generator = new IdGenerator <string>(() => DateTime.Now.Ticks.ToString());
            var level     = 100;

            var result = Concurrency.Run(cid =>
            {
                return(generator.TakeOne());
            }, level: level);

            Assert.Equal(level, result.Count);
            Assert.Equal(level, result.Select(x => x.Value).Distinct().Count());
        }
Example #4
0
        public void ConcurrencyTest()
        {
            Concurrency.Run(cid =>
            {
                Assert.Null(StringScope.Current);
                using (new StringScope("outter"))
                {
                    Assert.Equal("outter", StringScope.Current.Model);

                    using (new StringScope("inner"))
                    {
                        Assert.Equal("inner", StringScope.Current.Model);
                        Assert.Equal(2, StringScope.Scopes.Count);
                    }
                }
                return(0);
            }, level: 20, threadCount: 4);
        }
Example #5
0
        public void Test1()
        {
            var cache = new Cache <DateTime>
            {
                CacheMethod            = () => DateTime.Now,
                UpdateExpirationMethod = cacheTime => cacheTime.Add(TimeSpan.FromSeconds(1)),
            };

            var updateCount = 0;

            cache.OnCacheUpdated += (cacheTime, value) => updateCount += 1;

            var result = Concurrency.Run(id =>
            {
                var value = cache.Value;
                Console.WriteLine($"{id}\t{cache}");
                Thread.Sleep(500);
                return(value);
            }, 20, 5).Select(x => x.Value);

            Assert.Equal(updateCount, result.Distinct().Count());
        }