Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public void Can_lock_cache()
        {
            var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions()));

            var key        = ".Task";
            var expiration = TimeSpan.FromMinutes(2);

            var actionCount = 0;
            var action      = new Action(() =>
            {
                cacheManager.IsSet(key).ShouldBeTrue();

                cacheManager.PerformActionWithLock(key, expiration,
                                                   () => Assert.Fail("Action in progress")).ShouldBeFalse();

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

            cacheManager.PerformActionWithLock(key, expiration, action).ShouldBeTrue();
            actionCount.ShouldEqual(1);

            Assert.Throws <ApplicationException>(() => cacheManager.PerformActionWithLock(key, expiration, action));
            actionCount.ShouldEqual(2);

            cacheManager.PerformActionWithLock(key, expiration, action).ShouldBeTrue();
            actionCount.ShouldEqual(3);
        }
Exemplo n.º 3
0
        public void CanPerformLock()
        {
            var key        = new CacheKey("Nop.Task");
            var expiration = TimeSpan.FromMinutes(2);

            var actionCount = 0;
            var action      = new Action(() =>
            {
                var isSet = _staticCacheManager.GetAsync <object>(key, () => null);
                isSet.Should().NotBeNull();

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

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

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

            Assert.Throws <ApplicationException>(() =>
                                                 _staticCacheManager.PerformActionWithLock(key.Key, expiration, action));

            actionCount.Should().Be(2);

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