Ejemplo n.º 1
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.º 2
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 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.º 4
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.º 5
0
        public 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 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;

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

            lookupHandler.Lookup(invocationSignature);
            implementation.Verify(a => a.AddNumbers(1, 2), Times.Once);

            policyProvider.VerifyAll();
            implementation.VerifyAll();
        }
 public TResult Handle <TResult>(ProxiedMethodInvocation <T, TResult> methodInvocation)
 {
     return(_syncLookupHandler.Lookup(methodInvocation));
 }