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))); }
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))); }