Пример #1
0
        public void ShouldCallImplementedInterfaceMethod()
        {
            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var interceptor1 = new Interceptor1();
                var interceptor2 = new Interceptor2();
                var interceptor3 = new Interceptor3();
                var interceptor4 = new Interceptor4();

                var interceptors = new IResponseCachingInterceptor[] { interceptor1, interceptor2, interceptor3, interceptor4 };

                //随机组合进行测试
                interceptors = interceptors.OrderBy(_ => random.Next()).ToArray();

                Console.WriteLine($"time: {i:D3} - order:{string.Join(" -> ", interceptors.Select(m => m.GetType().Name))}");

                var interceptorAggregator = new InterceptorAggregator(interceptors);

                interceptorAggregator.OnCacheStoringAsync(null, null, null, (_, _, _) => Task.FromResult <ResponseCacheEntry>(null));
                interceptorAggregator.OnResponseWritingAsync(null, null, (_, _) => Task.FromResult(true));

                NormalCheck(interceptors);
            }
        }
Пример #2
0
        private static void ShouldNoAnyCalled(IResponseCachingInterceptor interceptor)
        {
            var callCountable = interceptor as IInterceptorCallCountable;

            Assert.IsNotNull(callCountable);

            Assert.AreEqual(0, callCountable.OnCacheStoringCallCount, interceptor.GetType().Name);
            Assert.AreEqual(0, callCountable.OnResponseWritingCallCount, interceptor.GetType().Name);
        }
Пример #3
0
        private static void NormalCheck(IResponseCachingInterceptor interceptor)
        {
            switch (interceptor)
            {
            case Interceptor1 interceptor1:
                Assert.AreEqual(0, interceptor1.OnCacheStoringCallCount);
                Assert.AreEqual(1, interceptor1.OnResponseWritingCallCount);

                break;

            case Interceptor2 interceptor2:
                Assert.AreEqual(1, interceptor2.OnCacheStoringCallCount);
                Assert.AreEqual(1, interceptor2.OnResponseWritingCallCount);

                break;

            case Interceptor3 interceptor3:

                Assert.AreEqual(1, interceptor3.OnCacheStoringCallCount);
                Assert.AreEqual(1, interceptor3.OnResponseWritingCallCount);

                break;

            case Interceptor4 interceptor4:

                Assert.AreEqual(1, interceptor4.OnCacheStoringCallCount);
                Assert.AreEqual(0, interceptor4.OnResponseWritingCallCount);

                break;

            case ShortCircuitsInterceptor shortCircuitsInterceptor:

                Assert.AreEqual(1, shortCircuitsInterceptor.OnCacheStoringCallCount);
                Assert.AreEqual(1, shortCircuitsInterceptor.OnResponseWritingCallCount);

                break;

            default:
                break;
            }
        }
Пример #4
0
        public void ShouldShortCircuits()
        {
            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var interceptor1             = new Interceptor1();
                var interceptor2             = new Interceptor2();
                var interceptor3             = new Interceptor3();
                var interceptor4             = new Interceptor4();
                var shortCircuitsInterceptor = new ShortCircuitsInterceptor();

                var interceptors = new IResponseCachingInterceptor[] { interceptor1, interceptor2, interceptor3, interceptor4, shortCircuitsInterceptor };

                //随机组合进行测试
                interceptors = interceptors.OrderBy(_ => random.Next()).ToArray();

                Console.WriteLine($"time: {i:D3} - order:{string.Join(" -> ", interceptors.Select(m => m.GetType().Name))}");

                var interceptorAggregator = new InterceptorAggregator(interceptors);

                interceptorAggregator.OnCacheStoringAsync(null, null, null, (_, _, _) => Task.FromResult <ResponseCacheEntry>(null));
                interceptorAggregator.OnResponseWritingAsync(null, null, (_, _) => Task.FromResult(true));

                bool shortCircuited = false;
                foreach (var interceptor in interceptors)
                {
                    if (shortCircuited)
                    {
                        ShouldNoAnyCalled(interceptor);
                    }
                    else
                    {
                        NormalCheck(interceptor);
                        shortCircuited = interceptor is ShortCircuitsInterceptor;
                    }
                }
            }
        }