コード例 #1
0
        public void Can_perform_lock()
        {
            var key        = new CacheKey("Nop.Task");
            var expiration = TimeSpan.FromMinutes(2);

            var actionCount = 0;
            var action      = new Action(() =>
            {
                _cacheManager.IsSet(key).Should().BeTrue();

                _cacheManager.PerformActionWithLock(key.Key, expiration,
                                                    () => Assert.Fail("Action in progress"))
                .Should().BeFalse();

                if (++actionCount % 2 == 0)
                {
                    throw new ApplicationException("Alternating actions fail");
                }
            });

            _cacheManager.PerformActionWithLock(key.Key, expiration, action)
            .Should().BeTrue();
            actionCount.Should().Be(1);

            _cacheManager.Invoking(a => a.PerformActionWithLock(key.Key, expiration, action)).Should().Throw <ApplicationException>();
            actionCount.Should().Be(2);

            _cacheManager.PerformActionWithLock(key.Key, expiration, action)
            .Should().BeTrue();
            actionCount.Should().Be(3);
        }