Beispiel #1
0
        public void should_call_function_once()
        {
            _cachedString.Get("Test", _worker.GetString);
            _cachedString.Get("Test", _worker.GetString);

            _worker.HitCount.Should().Be(1);
        }
Beispiel #2
0
        public T Get <T>()
        {
            var result = Cached.Get(typeof(T));

            if (result == null)
            {
                return(default(T));
            }
            return((T)result);
        }
Beispiel #3
0
        public void Get_RefreshesTheValueIfExpired()
        {
            var called = 0;
            var temp   = new Cached <int>(1);

            var x = temp.Get(() =>
            {
                called++;
                return(-1);
            });

            Thread.Sleep(10);

            x = temp.Get(() =>
            {
                called++;
                return(-1);
            });
            Assert.IsTrue(called == 2);
        }
Beispiel #4
0
        public void Get_RefreshesTheValueOnFirstRead()
        {
            var called = false;
            var temp   = new Cached <int>(1);

            var x = temp.Get(() =>
            {
                called = true;
                return(-1);
            });

            Assert.IsTrue(called);
        }
Beispiel #5
0
        public void Get_UsesTheSuppliedFunctionInsteadOfTheDefaultOne()
        {
            var called = 0;
            var temp   = new Cached <int>(() =>
            {
                called += 10;
                return(-1);
            }, 1);

            var x = temp.Get(() =>
            {
                called++;
                return(-1);
            });

            Assert.IsTrue(called == 1);
        }
Beispiel #6
0
        public void should_honor_ttl()
        {
            int hitCount = 0;
            _cachedString = new Cached<string>();

            for (int i = 0; i < 10; i++)
            {
                _cachedString.Get("key", () =>
                    {
                        hitCount++;
                        return null;
                    }, TimeSpan.FromMilliseconds(300));

                Thread.Sleep(100);
            }

            hitCount.Should().BeInRange(3, 6);
        }
Beispiel #7
0
        public void should_honor_ttl()
        {
            int hitCount = 0;

            _cachedString = new Cached <string>();

            for (int i = 0; i < 100; i++)
            {
                _cachedString.Get("key", () =>
                {
                    hitCount++;
                    return(null);
                }, TimeSpan.FromMilliseconds(300));

                Thread.Sleep(10);
            }

            hitCount.Should().BeInRange(3, 6);
        }
Beispiel #8
0
        public void should_clear_expired_when_they_expire()
        {
            int hitCount = 0;

            _cachedString = new Cached <string>();

            for (int i = 0; i < 10; i++)
            {
                _cachedString.Get("key", () =>
                {
                    hitCount++;
                    return(null);
                }, TimeSpan.FromMilliseconds(300));

                Thread.Sleep(100);
            }

            Thread.Sleep(1000);

            hitCount.Should().BeInRange(3, 7);
            _cachedString.Values.Should().HaveCount(0);
        }