Exemplo n.º 1
0
            public void Interceptors_for_only_the_matching_interface_type_can_be_added_and_removed()
            {
                var mockInterceptor1 = new Mock <IFakeInterceptor1>();
                var mockInterceptor2 = new Mock <IFakeInterceptor2>();

                var dispatcher = new InternalDispatcher <IFakeInterceptor1>();

                dispatcher.Add(mockInterceptor1.Object);
                dispatcher.Add(mockInterceptor2.Object);

                dispatcher.Dispatch(i => i.CallMe(new DbCommandInterceptionContext <string>()));

                mockInterceptor1.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Once());
                mockInterceptor2.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never());

                dispatcher.Remove(mockInterceptor1.Object);
                dispatcher.Remove(mockInterceptor2.Object);

                dispatcher.Dispatch(i => i.CallMe(new DbCommandInterceptionContext <string>()));

                mockInterceptor1.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Once());
                mockInterceptor2.Verify(m => m.CallMe(It.IsAny <DbCommandInterceptionContext <string> >()), Times.Never());
            }
Exemplo n.º 2
0
            public void Interceptors_can_be_added_removed_and_dispatched_to_concurrently()
            {
                var interceptors = new ConcurrentStack <InterceptorForThreads>();
                var dispatcher   = new InternalDispatcher <InterceptorForThreads>();

                const int interceptorCount = 20;
                const int dispatchCount    = 10;

                // Add in parallel
                ExecuteInParallel(
                    () =>
                {
                    var interceptor = new InterceptorForThreads();
                    interceptors.Push(interceptor);
                    dispatcher.Add(interceptor);
                }, interceptorCount);

                Assert.Equal(interceptorCount, interceptors.Count);

                // Dispatch in parallel
                var calledInterceptors = new ConcurrentStack <InterceptorForThreads>();

                ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount);

                Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count);
                interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i)));

                var toRemove = new ConcurrentStack <InterceptorForThreads>(interceptors);

                // Add, remove, and dispatch in parallel
                ExecuteInParallel(
                    () =>
                {
                    dispatcher.Dispatch(i => { });
                    InterceptorForThreads interceptor;
                    toRemove.TryPop(out interceptor);
                    dispatcher.Remove(interceptor);
                    dispatcher.Add(interceptor);
                }, interceptorCount);

                // Dispatch in parallel
                calledInterceptors = new ConcurrentStack <InterceptorForThreads>();
                ExecuteInParallel(() => dispatcher.Dispatch(calledInterceptors.Push), dispatchCount);

                Assert.Equal(dispatchCount * interceptorCount, calledInterceptors.Count);
                interceptors.Each(i => Assert.Equal(dispatchCount, calledInterceptors.Count(c => c == i)));
            }