예제 #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);
        }
예제 #2
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 report = 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);

            Assert.Equal(1, report.Results.Count(x => x.Return == "Entered"));
            Assert.Equal(1, report.Results.Count(x => x.Return == "Timeout"));
            Assert.True(report.AverageElapsed?.TotalMilliseconds < 2000);
        }