Ejemplo n.º 1
0
        public void TestGetItemStale()
        {
            var memcachedMock = new Mock <IMemcachedClient>();
            var enyimProvider = new EnyimMemcachedProvider <IAwesomeInterface>(memcachedMock.Object);

            var cachePolicy = new CachePolicy()
            {
                CacheDuration = 10
            };

            var proxyContext = ProxyRequest <IAwesomeInterface> .FromExpression(a => a.ParameteredMethod("", 1));

            var hashKey = proxyContext.CreateHash();
            var obj     = new[] { "", "" };

            object val = new MemcachedObject <IEnumerable <string> >()
            {
                Created = DateTime.Now.AddSeconds(-15),
                Object  = obj
            };

            memcachedMock.Setup(a => a.TryGet(hashKey, out val)).Returns(true);

            var result = enyimProvider.GetItem <IEnumerable <string> >(proxyContext, cachePolicy);

            Assert.IsTrue(result.State == CachedObjectState.Stale, "Provider did not return stale, but: " + result.State);
            Assert.AreEqual(obj, result.Object, "Provider did not return object");
            Assert.IsNull(result.ThrownException, "Provider did not return null in exception field");
        }
Ejemplo n.º 2
0
        public void TestGetItemExceptionExpired()
        {
            var memcachedMock = new Mock <IMemcachedClient>();
            var enyimProvider = new EnyimMemcachedProvider <IAwesomeInterface>(memcachedMock.Object);

            var cachePolicy = new CachePolicy()
            {
                CacheDuration = 2
            };

            var proxyContext = ProxyRequest <IAwesomeInterface> .FromExpression(a => a.ParameteredMethod("", 1));

            var hashKey   = proxyContext.CreateHash();
            var exception = new Exception("Rofl exception");

            object val = new MemcachedObject <IEnumerable <string> >()
            {
                Created     = DateTime.Now.AddSeconds(-11),
                IsException = true,
                Exception   = exception
            };

            memcachedMock.Setup(a => a.TryGet(hashKey, out val)).Returns(true);

            var result = enyimProvider.GetItem <IEnumerable <string> >(proxyContext, cachePolicy);

            Assert.IsTrue(result.State == CachedObjectState.None);
            Assert.IsNull(result.ThrownException, "Provider did not return null in exception field");
            Assert.IsNull(result.Object, "Provider did not return null in object field");
        }
        public void TestSerializationOfTrickyExceptions()
        {
            var transcoder = new ZippedNewtonsoftTranscoder();
            var memObject = new MemcachedObject<string>();
            memObject.Created = DateTime.Now;
            memObject.Object = null;
            memObject.IsException = true;

            try
            {
                var bla = new SqlConnection("server=localhost;database=lolkalererer;uid=ffucktard;pwd=bøs!;application name=lawl;Connect Timeout=1");
                var command = new SqlCommand("EXECUTE NonExistantStoredProcedure", bla);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                memObject.Exception = e;
            }

            var cachedItem = transcoder.Serialize(memObject);
            var _memObj = transcoder.Deserialize(cachedItem);

            Assert.IsNull(_memObj);
        }
        public void TestSerializationOfTrickyExceptions()
        {
            var transcoder = new ZippedNewtonsoftTranscoder();
            var memObject  = new MemcachedObject <string>();

            memObject.Created     = DateTime.Now;
            memObject.Object      = null;
            memObject.IsException = true;

            try
            {
                var bla     = new SqlConnection("server=localhost;database=lolkalererer;uid=ffucktard;pwd=bøs!;application name=lawl;Connect Timeout=1");
                var command = new SqlCommand("EXECUTE NonExistantStoredProcedure", bla);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                memObject.Exception = e;
            }

            var cachedItem = transcoder.Serialize(memObject);
            var _memObj    = transcoder.Deserialize(cachedItem);

            Assert.IsNull(_memObj);
        }
        public void TestGetItemExceptionExpired()
        {
            var memcachedMock = new Mock<IMemcachedClient>();
            var enyimProvider = new EnyimMemcachedProvider<IAwesomeInterface>(memcachedMock.Object);

            var cachePolicy = new CachePolicy()
            {
                CacheDuration = 2
            };

            var proxyContext = ProxyRequest<IAwesomeInterface>.FromExpression(a => a.ParameteredMethod("", 1));
            var hashKey = proxyContext.CreateHash();
            var exception = new Exception("Rofl exception");

            object val = new MemcachedObject<IEnumerable<string>>()
            {
                Created = DateTime.Now.AddSeconds(-11),
                IsException = true,
                Exception = exception
            };

            memcachedMock.Setup(a => a.TryGet(hashKey, out val)).Returns(true);

            var result = enyimProvider.GetItem<IEnumerable<string>>(proxyContext, cachePolicy);
            Assert.IsTrue(result.State == CachedObjectState.None);
            Assert.IsNull(result.ThrownException, "Provider did not return null in exception field");
            Assert.IsNull(result.Object, "Provider did not return null in object field");
        }
        public void StoreItem <TResult>(ProxyRequest <T, TResult> proxyRequest, CachePolicy cachePolicy, TResult item)
        {
            var key          = proxyRequest.CreateHash();
            var cachedObject = new MemcachedObject <TResult>()
            {
                Created = DateTime.Now,
                Object  = item
            };

            if (cachePolicy.MaxAge > 0)
            {
                _client.Store(StoreMode.Set, key, cachedObject, TimeSpan.FromSeconds(cachePolicy.MaxAge));
            }
            else
            {
                _client.Store(StoreMode.Set, key, cachedObject);
            }
        }
        public void StoreException <TResult>(ProxyRequest <T, TResult> proxyRequest, CachePolicy cachePolicy, Exception exception)
        {
            var key          = proxyRequest.CreateHash();
            var cachedObject = new MemcachedObject <TResult>()
            {
                Created     = DateTime.Now,
                IsException = true,
                Exception   = exception,
            };

            if (cachePolicy.MaxAge > 0)
            {
                _client.Store(StoreMode.Set, key, cachedObject, TimeSpan.FromSeconds(cachePolicy.MaxAge));
            }
            else
            {
                _client.Store(StoreMode.Set, key, cachedObject);
            }
        }
        public void TestGetItemStale()
        {
            var memcachedMock = new Mock<IMemcachedClient>();
            var enyimProvider = new EnyimMemcachedProvider<IAwesomeInterface>(memcachedMock.Object);

            var cachePolicy = new CachePolicy()
            {
                CacheDuration = 10
            };

            var proxyContext = ProxyRequest<IAwesomeInterface>.FromExpression(a => a.ParameteredMethod("", 1));

            var hashKey = proxyContext.CreateHash();
            var obj = new[] { "", "" };

            object val = new MemcachedObject<IEnumerable<string>>()
            {
                Created = DateTime.Now.AddSeconds(-15),
                Object = obj
            };

            memcachedMock.Setup(a => a.TryGet(hashKey, out val)).Returns(true);

            var result = enyimProvider.GetItem<IEnumerable<string>>(proxyContext, cachePolicy);
            Assert.IsTrue(result.State == CachedObjectState.Stale, "Provider did not return stale, but: " + result.State);
            Assert.AreEqual(obj, result.Object, "Provider did not return object");
            Assert.IsNull(result.ThrownException, "Provider did not return null in exception field");
        }