Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void Test()
        {
            var lockParser = new TypeLockParser(nameof(NStandard));

            var report = 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);

            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);
        }