Ejemplo n.º 1
0
        public async Task DeleteAsync <TResult>(Expression <Func <T, TResult> > expression)
        {
            var invocation = ProxiedMethodInvocationGenerator <T> .FromExpression(expression);

            var key    = invocation.GetHashString(_hashScramble);
            var result = await _memcachedCluster.Delete(key);
        }
Ejemplo n.º 2
0
        public void TestCachePolicyWithException()
        {
            var implementation = new Mock <ITestInterface>(MockBehavior.Strict);
            var policyProvider = new Mock <ICachePolicyProvider <ITestInterface> >(MockBehavior.Strict);
            var cache          = new Mock <ICacheProvider <ITestInterface> >(MockBehavior.Strict);

            var lookupHandler       = new SyncLookupHandler <ITestInterface>(implementation.Object, policyProvider.Object, cache.Object);
            var invocationSignature = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbers(1, 2));

            var method     = invocationSignature.Method;
            var parameters = invocationSignature.Parameters;

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

            policyProvider.Setup(a => a.GetPolicy(method, parameters)).Returns(cachePolicy).Verifiable();
            cache.Setup(a => a.GetAsync(invocationSignature, cachePolicy)).ReturnsAsync(new CachedObject <int>(CachedObjectState.Exception, new TestException())).Verifiable();

            var result = lookupHandler.Lookup(invocationSignature);

            policyProvider.VerifyAll();
            cache.VerifyAll();
        }
Ejemplo n.º 3
0
        public void TestCachePolicyNoneException()
        {
            var implementation = new Mock <ITestInterface>(MockBehavior.Strict);
            var policyProvider = new Mock <ICachePolicyProvider <ITestInterface> >(MockBehavior.Strict);
            var cache          = new Mock <ICacheProvider <ITestInterface> >(MockBehavior.Strict);

            var lookupHandler = new SyncLookupHandler <ITestInterface>(implementation.Object, policyProvider.Object, cache.Object);
            var invocation    = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbers(1, 2));

            var method     = invocation.Method;
            var parameters = invocation.Parameters;

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

            policyProvider.Setup(a => a.GetPolicy(method, parameters)).Returns(cachePolicy).Verifiable();

            var exceptionThrown = new TestException();

            implementation.Setup(a => a.AddNumbers(1, 2)).Throws(exceptionThrown).Verifiable();

            cache.Setup(a => a.GetAsync(invocation, cachePolicy)).ReturnsAsync(new CachedObject <int>(CachedObjectState.None, null)).Verifiable();
            cache.Setup(a => a.StoreExceptionAsync(invocation, cachePolicy, exceptionThrown)).Returns(Task.Factory.StartNew(() => { })).Verifiable();

            var result = lookupHandler.Lookup(invocation);

            policyProvider.VerifyAll();
            implementation.VerifyAll();
            cache.VerifyAll();
        }
 public async Task DeleteAsync <TResult>(Expression <Func <T, TResult> > expression)
 {
     await Task.Factory.StartNew(() =>
     {
         var invocation = ProxiedMethodInvocationGenerator <T> .FromExpression(expression);
         var hash       = invocation.GetHashString <T, TResult>();
         _cache.Remove(hash);
     });
 }
        public void TestParameterless()
        {
            var configuredMethod = GetConfiguredMethod <ITestInterface>(a => a.ParameterlessMethod());
            var proxyContext     = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.ParameterlessMethod());

            var matched = configuredMethod.IsMatch(proxyContext.Method, proxyContext.Parameters);

            Assert.IsTrue(matched, "MethodConfig didn't match");
        }
        public async void TestEmptyCacheSyncronization()
        {
            var implementation = new Mock <ITestInterface>(MockBehavior.Strict);
            var policyProvider = new Mock <ICachePolicyProvider <ITestInterface> >(MockBehavior.Strict);
            var cache          = new Mock <ICacheProvider <ITestInterface> >(MockBehavior.Strict);

            var lookupHandler = new SyncLookupHandler <ITestInterface>(implementation.Object, policyProvider.Object, cache.Object);
            var invocation    = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbers(1, 2));

            var method     = invocation.Method;
            var parameters = invocation.Parameters;

            const int implReturnValue = 3;

            implementation.Setup(a => a.AddNumbers(1, 2)).Returns(implReturnValue);

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

            policyProvider.Setup(a => a.GetPolicy(method, parameters)).Returns(cachePolicy);

            var cachedObject = new CachedObject <int>(CachedObjectState.None, null);

            cache.Setup(a => a.GetAsync(invocation, cachePolicy)).ReturnsAsync(cachedObject);

            var cacheStoreTask = new Task(() => {});

            cache.Setup(a => a.StoreAsync(invocation, cachePolicy, implReturnValue)).Returns(() => cacheStoreTask);

            const int taskCount = 10;
            var       tasks     = new Task <int> [taskCount];

            for (var i = 0; i < taskCount; i++)
            {
                var idx = i;
                tasks[idx] = Task.Factory.StartNew(() =>
                {
                    if (idx == (taskCount - 1))
                    {
                        cacheStoreTask.Start();
                    }
                    return(lookupHandler.Lookup(invocation));
                });
            }

            await Task.WhenAll(tasks);

            Assert.IsTrue(tasks.All(a => a.Result == implReturnValue));
            implementation.Verify(a => a.AddNumbers(1, 2), Times.Once);
            Assert.IsTrue(cacheStoreTask.Wait(5000), "Store action on cache did not appear to have been called");
            cache.Verify(a => a.StoreAsync(invocation, cachePolicy, implReturnValue), Times.Once);
        }
Ejemplo n.º 7
0
        public async void TestCachePolicyStaleAsyncUpdateExceptionBubble()
        {
            var implementation = new Mock <ITestInterface>(MockBehavior.Strict);
            var policyProvider = new Mock <ICachePolicyProvider <ITestInterface> >(MockBehavior.Strict);
            var cache          = new Mock <ICacheProvider <ITestInterface> >(MockBehavior.Strict);

            var lookupHandler = new SyncLookupHandler <ITestInterface>(implementation.Object, policyProvider.Object, cache.Object);
            var invocation    = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbers(1, 2));

            var method     = invocation.Method;
            var parameters = invocation.Parameters;

            var cachePolicy = new CachePolicy()
            {
                CacheDuration = 20, BubbleExceptions = true
            };

            policyProvider.Setup(a => a.GetPolicy(method, parameters)).Returns(cachePolicy);

            var cachedObject = new CachedObject <int>(CachedObjectState.Stale, 3);

            cache.Setup(a => a.GetAsync(invocation, cachePolicy)).ReturnsAsync(cachedObject).Verifiable();

            var thrownException = new TestException();

            implementation.Setup(a => a.AddNumbers(1, 2)).Throws(thrownException).Verifiable();

            var awaitableStoreTask = new Task(() => { });

            cache.Setup(a => a.StoreExceptionAsync(invocation, cachePolicy, thrownException)).Returns(() =>
            {
                awaitableStoreTask.Start();
                return(awaitableStoreTask);
            }).Verifiable();

            var task = await Task.Factory.StartNew(async() =>
            {
                var res = lookupHandler.Lookup(invocation);
                await awaitableStoreTask;
                return(res);
            });

            var result = await task;

            Assert.AreEqual(result, cachedObject.Object);
            policyProvider.VerifyAll();
            cache.VerifyAll();
            implementation.VerifyAll();
        }
Ejemplo n.º 8
0
        public async void TestDirectCallInvocationAsync()
        {
            var implementationMock = new Mock <ITestInterface>(MockBehavior.Strict);

            implementationMock.Setup(a => a.AddNumbersAsync(1, 2)).ReturnsAsync(1 + 2);

            var handlerMock             = new Mock <IProxyHandler <ITestInterface> >(MockBehavior.Strict);
            var proxiedMethodInvocation = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbersAsync(1, 2));

            handlerMock.Setup(a => a.HandleAsync(proxiedMethodInvocation)).Returns(proxiedMethodInvocation.InvokeAsync(implementationMock.Object));

            var sleipner      = new SleipnerProxy <ITestInterface>(implementationMock.Object);
            var proxiedObject = sleipner.WrapWith(handlerMock.Object);

            var result = await proxiedObject.AddNumbersAsync(1, 2);

            Assert.AreEqual(3, result);
        }
        public async void TestNoCachePolicyNull()
        {
            var implementation = new Mock <ITestInterface>(MockBehavior.Strict);
            var policyProvider = new Mock <ICachePolicyProvider <ITestInterface> >(MockBehavior.Strict);
            var cache          = new Mock <ICacheProvider <ITestInterface> >(MockBehavior.Strict);

            var lookupHandler       = new AsyncLookupHandler <ITestInterface>(implementation.Object, policyProvider.Object, cache.Object);
            var invocationSignature = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbersAsync(1, 2));

            var method     = invocationSignature.Method;
            var parameters = invocationSignature.Parameters;

            policyProvider.Setup(a => a.GetPolicy(method, parameters)).Returns(() => null).Verifiable();
            implementation.Setup(a => a.AddNumbersAsync(1, 2)).ReturnsAsync(1 + 2).Verifiable();

            await lookupHandler.LookupAsync(invocationSignature);

            policyProvider.VerifyAll();
            implementation.VerifyAll();
        }
Ejemplo n.º 10
0
        public static CachePolicy GetPolicy <T, TResult>(this ICachePolicyProvider <T> provider, Expression <Func <T, TResult> > expression) where T : class
        {
            var invocation = ProxiedMethodInvocationGenerator <T> .FromExpression(expression);

            return(provider.GetPolicy(invocation));
        }
Ejemplo n.º 11
0
        public async void TestEmptyCacheExceptionSyncronization()
        {
            var implementation = new Mock <ITestInterface>(MockBehavior.Strict);
            var policyProvider = new Mock <ICachePolicyProvider <ITestInterface> >(MockBehavior.Strict);
            var cache          = new Mock <ICacheProvider <ITestInterface> >(MockBehavior.Strict);

            var lookupHandler = new AsyncLookupHandler <ITestInterface>(implementation.Object, policyProvider.Object, cache.Object);
            var invocation    = ProxiedMethodInvocationGenerator <ITestInterface> .FromExpression(a => a.AddNumbersAsync(1, 2));

            var method     = invocation.Method;
            var parameters = invocation.Parameters;

            var thrownException = new TestException();

            implementation.Setup(a => a.AddNumbersAsync(1, 2)).ThrowsAsync(thrownException);

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

            policyProvider.Setup(a => a.GetPolicy(method, parameters)).Returns(cachePolicy);

            var cachedObject = new CachedObject <int>(CachedObjectState.None, null);

            cache.Setup(a => a.GetAsync(invocation, cachePolicy)).ReturnsAsync(cachedObject);

            var cacheStoreTask = new Task(() =>
            {
                Thread.Sleep(50);
            });

            cache.Setup(a => a.StoreExceptionAsync(invocation, cachePolicy, thrownException)).Returns(() =>
            {
                cacheStoreTask.Start();

                return(cacheStoreTask);
            });

            const int taskCount = 10;
            var       tasks     = new Task <int> [taskCount];

            for (var i = 0; i < taskCount; i++)
            {
                tasks[i] = lookupHandler.LookupAsync(invocation);
            }

            foreach (var t in tasks)
            {
                try
                {
                    await t;
                    Assert.Fail("Task didn't throw exception");
                }
                catch (TestException e)
                {
                }
            }

            implementation.Verify(a => a.AddNumbersAsync(1, 2), Times.Once);
            Assert.IsTrue(cacheStoreTask.Wait(5000), "Store action on cache did not appear to have been called");
            cache.Verify(a => a.StoreExceptionAsync(invocation, cachePolicy, thrownException), Times.Once);
        }
Ejemplo n.º 12
0
        public static bool IsMatch <T, TResult>(this IConfiguredMethod <T> method, Expression <Func <T, TResult> > expression) where T : class
        {
            var request = ProxiedMethodInvocationGenerator <T> .FromExpression(expression);

            return(method.IsMatch(request.Method, request.Parameters));
        }