Ejemplo n.º 1
0
        public void Constructor_DefaultServicesInContainer()
        {
            // Arrange
            var config = new ProcessorConfiguration();

            // Act
            var defaultServices = new DefaultServices(config);

            // Assert
            Assert.IsType(typeof(DefaultCommandHandlerSelector), defaultServices.GetService(typeof(ICommandHandlerSelector)));
            Assert.IsType(typeof(DefaultCommandHandlerActivator), defaultServices.GetService(typeof(ICommandHandlerActivator)));
            Assert.IsType(typeof(DefaultCommandHandlerTypeResolver), defaultServices.GetService(typeof(ICommandHandlerTypeResolver)));
            Assert.IsType(typeof(DefaultAssembliesResolver), defaultServices.GetService(typeof(IAssembliesResolver)));
            Assert.IsType(typeof(DefaultInterceptionProvider), defaultServices.GetService(typeof(IInterceptionProvider)));

            object[] filterProviders = defaultServices.GetServices(typeof(IFilterProvider)).ToArray();
            Assert.Equal(2, filterProviders.Length);
            Assert.IsType(typeof(ConfigurationFilterProvider), filterProviders[0]);
            Assert.IsType(typeof(HandlerFilterProvider), filterProviders[1]);

            Assert.IsType(typeof(DefaultCommandValidator), defaultServices.GetService(typeof(ICommandValidator)));

            object[] interceptors = defaultServices.GetServices(typeof(IInterceptor)).ToArray();
            Assert.Equal(0, interceptors.Length);
        }
Ejemplo n.º 2
0
 private static Lazy<ProcessorConfiguration> CreateConfiguration()
 {
     return new Lazy<ProcessorConfiguration>(delegate
     {
         ProcessorConfiguration config = new ProcessorConfiguration();
         return config;
     });
 }
Ejemplo n.º 3
0
 public void WhenSettingNullValuesThenThrowsArgumentNullException()
 {
     // Arrange
     ProcessorConfiguration config = new ProcessorConfiguration();
     this.disposableResources.Add(config);
     
     // Act & Assert
     ExceptionAssert.ThrowsArgumentNull(() => config.Initializer = null, "value");
     ExceptionAssert.ThrowsArgumentNull(() => config.DependencyResolver = null, "value");
 }
        public DefaultCommandHandlerActivatorTests()
        {
            this.config = new ProcessorConfiguration();

            this.command = new Mock<ICommand>();

            this.dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Loose);
            this.dependencyResolver.Setup(r => r.BeginScope()).Returns(this.dependencyResolver.Object);
            this.config.DependencyResolver = this.dependencyResolver.Object;
        }
        public DefaultInterceptionProviderTests()
        {
            this.configuration = new ProcessorConfiguration();
            this.interceptor = new Mock<IInterceptor>();
         
            this.interceptor.Setup(i => i.OnExecuting());
            this.interceptor.Setup(i => i.OnExecuted());
            this.interceptor.Setup(i => i.OnException(It.IsAny<Exception>()));

            this.configuration.Services.Add(typeof(IInterceptor), this.interceptor.Object);
        }
Ejemplo n.º 6
0
        public void Add_GuardClauses()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);

            // Act & assert
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.Add(serviceType: null, service: new object()), "serviceType");
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.Add(typeof(object), service: null), "service");
            ExceptionAssert.ThrowsArgument(() => defaultServices.Add(typeof(object), new object()), "serviceType");
            ExceptionAssert.ThrowsArgument(() => defaultServices.Add(typeof(IFilterProvider), new object()), "service");
        }
        public void WhenGettingFiltersThenReturnsFiltersFromConfiguration()
        {
            ProcessorConfiguration config = new ProcessorConfiguration();
            IFilter filter = new Mock<IFilter>().Object;
            config.Filters.Add(filter);

            var result = this.provider.GetFilters(config, null);

            Assert.NotNull(result);
            Assert.True(result.All(f => f.Scope == FilterScope.Global));
            Assert.Same(filter, result.ToArray()[0].Instance);
        }
Ejemplo n.º 8
0
        public void WhenGettingServiceWithoutOverrideThenReturnsSameAsOriginal()
        {
            ProcessorConfiguration config = new ProcessorConfiguration();
            HandlerServices services = new HandlerServices(config.Services);

            // Act
            ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver));
            ICommandHandlerTypeResolver globalVal = (ICommandHandlerTypeResolver)config.Services.GetService(typeof(ICommandHandlerTypeResolver));

            // Assert
            // Local handler didn't override, should get same value as global case.
            Assert.Same(localVal, globalVal);
        }
Ejemplo n.º 9
0
        public void WhenGettingServicesWithoutOverrideThenReturnsSameAsOriginals()
        {
            ProcessorConfiguration config = new ProcessorConfiguration();
            HandlerServices services = new HandlerServices(config.Services);

            // Act
            var localVal = services.GetServices(typeof(IFilterProvider));
            var globalVal = config.Services.GetServices(typeof(IFilterProvider));

            // Assert
            // Local handler didn't override, should get same value as global case.
            Assert.Equal(localVal.ToArray(), globalVal.ToArray());
        }
Ejemplo n.º 10
0
        public static void Main()
        {
            using (IUnityContainer container = new UnityContainer())
            {
                container.RegisterInstance(typeof(ISpy), new NullSpy());

                using (ProcessorConfiguration config = new ProcessorConfiguration())
                {
                    config.DefaultHandlerLifetime = HandlerLifetime.Transient;

                    config.RegisterCommandHandler<TestCommand>(async command =>
                    {
                        await Task.FromResult(0);
                    });

                    config.RegisterCommandHandler<TestCommand2>(async command =>
                    {
                        await Task.FromResult(0);
                    });

                    ////  config.RegisterContainer(container);
                    //// config.Services.Replace(typeof(ICommandValidator), new NullValidator());
                    ////  config.EnableDefaultTracing();

                    ////  PerformanceTracer traceWriter = new PerformanceTracer();

                    ////config.Services.Replace(typeof(ITraceWriter), traceWriter);
                    ////    traceWorker.MinimumLevel = Tracing.TraceLevel.DefaultCommandValidator;
                    ////config.Filters.Add(new CustomExceptionFilterAttribute());

                    ////   long initialMemory = GC.GetTotalMemory(false);

                    ////    config.EnableGlobalExceptionHandler();

                    const int MaxIterations = 100000;
                    config.EnableRedisMessageQueuing("localhost");
                    using (MessageProcessor processor = new MessageProcessor(config))
                    {
                        processor.ProcessAsync(new TestCommand());
                        SingleProcessing(processor);
                        ParallelProcessing(MaxIterations, processor);
                        ////   SequentialTaskProcessing(maxIterations, processor);
                        ////   SequentialTaskProcessingV2(maxIterations, processor);
                        ////  SequentialTaskProcessingV3(maxIterations, processor);

                        RunCommandBroker(config.CommandBroker);
                    }
                }
            ////    Console.ReadLine();
            }
        }
Ejemplo n.º 11
0
        public void WhenCreatingProcessorWithDefaultCtorThenPropertiesAreDefined()
        {
            // Act
            ProcessorConfiguration config = new ProcessorConfiguration();
            this.disposableResources.Add(config);

            // Assert
            Assert.True(config.AbortOnInvalidCommand);
            Assert.False(config.ServiceProxyCreationEnabled);
            Assert.NotNull(config.DependencyResolver);
            Assert.NotNull(config.Services);
            Assert.NotNull(config.Initializer);
            Assert.Equal(0, config.Filters.Count);
        }
Ejemplo n.º 12
0
        public void WhenRegisteringResourceToDisposeThenResouseIsDisposed()
        {
            // Arrange
            Mock<IDisposable> disposable = new Mock<IDisposable>();
            ProcessorConfiguration config = new ProcessorConfiguration();
            disposable.Setup(d => d.Dispose());
            config.RegisterForDispose(disposable.Object);

            // Act
            config.Dispose();

            // Assert
            disposable.Verify(d => d.Dispose(), Times.Once());
        }
Ejemplo n.º 13
0
        public void Add_AddsServiceToEndOfServicesList()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider = new Mock<IFilterProvider>().Object;
            IEnumerable<object> servicesBefore = defaultServices.GetServices(typeof(IFilterProvider));

            // Act
            defaultServices.Add(typeof(IFilterProvider), filterProvider);

            // Assert
            IEnumerable<object> servicesAfter = defaultServices.GetServices(typeof(IFilterProvider));
            Assert.Equal(servicesBefore.Concat(new[] { filterProvider }).ToArray(), servicesAfter.ToArray());
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HandlerRequest"/> class. 
        /// The request will be a child request of the <paramref name="parentRequest"/>.
        /// </summary> 
        /// <param name="configuration">The configuration.</param>
        /// <param name="parentRequest">The parent request. </param>
        public HandlerRequest(ProcessorConfiguration configuration, HandlerRequest parentRequest)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            this.Configuration = configuration;
            this.Properties = new Dictionary<string, object>();

            this.ParentRequest = parentRequest;
            if (parentRequest != null)
            {
                this.Processor = parentRequest.Processor;
            }
        }
Ejemplo n.º 15
0
        public void WhenGettingServiceWithOverrideThenReturnsOverride()
       {
           ProcessorConfiguration config = new ProcessorConfiguration();
           HandlerServices services = new HandlerServices(config.Services);

           ICommandHandlerTypeResolver newLocalService = new Mock<ICommandHandlerTypeResolver>().Object;
           services.Replace(typeof(ICommandHandlerTypeResolver), newLocalService);

           // Act            
           ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver));
           ICommandHandlerTypeResolver globalVal = (ICommandHandlerTypeResolver)config.Services.GetService(typeof(ICommandHandlerTypeResolver));

           // Assert
           // Local handler didn't override, should get same value as global case.
           Assert.Same(localVal, newLocalService);
           Assert.NotSame(localVal, globalVal);
       }
Ejemplo n.º 16
0
        private ProcessorConfiguration(ProcessorConfiguration configuration, CommandHandlerSettings settings)
        {
            Contract.Requires(configuration != null);
            Contract.Requires(settings != null);
            
            this.filters = configuration.Filters;
            this.dependencyResolver = configuration.DependencyResolver;
            this.DefaultHandlerLifetime = configuration.DefaultHandlerLifetime;
            this.Properties = configuration.Properties;
            this.CommandBroker = configuration.CommandBroker;

            // per-handler settings
            this.Services = settings.Services;
            this.AbortOnInvalidCommand = settings.AbortOnInvalidCommand;
            this.ServiceProxyCreationEnabled = configuration.ServiceProxyCreationEnabled;

            // Use the original configuration's initializer so that its Initialize()
            // will perform the same logic on this clone as on the original.
            this.Initializer = configuration.Initializer;
        }
Ejemplo n.º 17
0
       public void WhenGettingServiceWithDependencyInjectionThenReturnsFromDependencyInjection()
       {
           // Setting on Handler config overrides the DI container. 
           ProcessorConfiguration config = new ProcessorConfiguration();

           ICommandHandlerTypeResolver newDiService = new Mock<ICommandHandlerTypeResolver>().Object;
           var mockDependencyResolver = new Mock<IDependencyResolver>();
           mockDependencyResolver.Setup(dr => dr.GetService(typeof(ICommandHandlerTypeResolver))).Returns(newDiService);
           config.DependencyResolver = mockDependencyResolver.Object;

           HandlerServices services = new HandlerServices(config.Services);

           ICommandHandlerTypeResolver newLocalService = new Mock<ICommandHandlerTypeResolver>().Object;
           services.Replace(typeof(ICommandHandlerTypeResolver), newLocalService);

           // Act            
           ICommandHandlerTypeResolver localVal = (ICommandHandlerTypeResolver)services.GetService(typeof(ICommandHandlerTypeResolver));
           ICommandHandlerTypeResolver globalVal = (ICommandHandlerTypeResolver)config.Services.GetService(typeof(ICommandHandlerTypeResolver));

           // Assert
           // Local handler didn't override, should get same value as global case.            
           Assert.Same(newDiService, globalVal); // asking the config will give back the DI service
           Assert.Same(newLocalService, localVal); // but asking locally will get back the local service.
       }
Ejemplo n.º 18
0
        public void GetServices_CachesResultFromDependencyInjectionContainer()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var mockDependencyResolver = new Mock<IDependencyResolver>();
            config.DependencyResolver = mockDependencyResolver.Object;

            // Act
            defaultServices.GetServices(typeof(IFilterProvider));
            defaultServices.GetServices(typeof(IFilterProvider));

            // Assert
            mockDependencyResolver.Verify(dr => dr.GetServices(typeof(IFilterProvider)), Times.Once());
        }
Ejemplo n.º 19
0
        public void RemoveAll_SuccessfulMatch()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider1 = new Mock<IFilterProvider>().Object;
            var filterProvider2 = new Mock<IFilterProvider>().Object;
            defaultServices.ReplaceRange(typeof(IFilterProvider), new[] { filterProvider1, filterProvider2 });

            // Act
            defaultServices.RemoveAll(typeof(IFilterProvider), _ => true);

            // Assert
            Assert.Equal(0, defaultServices.GetServices(typeof(IFilterProvider)).Count());
        }
Ejemplo n.º 20
0
        public void RemoteAt_RemovesService()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider1 = new Mock<IFilterProvider>().Object;
            var filterProvider2 = new Mock<IFilterProvider>().Object;
            defaultServices.ReplaceRange(typeof(IFilterProvider), new[] { filterProvider1, filterProvider2 });

            // Act
            defaultServices.RemoveAt(typeof(IFilterProvider), 1);

            // Assert
            Assert.Equal(new[] { filterProvider1 }, defaultServices.GetServices(typeof(IFilterProvider)));
        }
Ejemplo n.º 21
0
        public void InsertRange_GuardClauses()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);

            // Act & assert
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.InsertRange(serviceType: null, index: 0, services: new[] { new object() }), "serviceType");
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.InsertRange(typeof(object), 0, services: null), "services");
            ExceptionAssert.ThrowsArgument(() => defaultServices.InsertRange(typeof(object), 0, new[] { new object() }), "serviceType");
            ExceptionAssert.ThrowsArgument(() => defaultServices.InsertRange(typeof(IFilterProvider), 0, new[] { new object() }), "services");
            ExceptionAssert.ThrowsArgumentOutOfRange(() => defaultServices.InsertRange(typeof(IFilterProvider), -1, new[] { new Mock<IFilterProvider>().Object }), "index");
        }
Ejemplo n.º 22
0
        internal static ProcessorConfiguration ApplyHandlerSettings(CommandHandlerSettings settings, ProcessorConfiguration configuration)
        {
            Contract.Requires(settings != null);
            Contract.Requires(configuration != null);

            if (!settings.IsServiceCollectionInitialized)
            {
                return configuration;
            }

            return new ProcessorConfiguration(configuration, settings);
        }
Ejemplo n.º 23
0
 private static void DefaultInitializer(ProcessorConfiguration configuration)
 {
     // Initialize the tracing layer.
     // This must be the last initialization code to execute
     // because it alters the configuration and expects no
     // further changes.  As a default service, we know it
     // must be present.
     Contract.Requires(configuration != null);
     ITraceManager traceManager = configuration.Services.GetTraceManager();
     
     traceManager.Initialize(configuration);
 }
Ejemplo n.º 24
0
        public void InsertRange_AddsElementAtTheRequestedLocation()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider1 = new Mock<IFilterProvider>().Object;
            var filterProvider2 = new Mock<IFilterProvider>().Object;
            var newFilterProvider1 = new Mock<IFilterProvider>().Object;
            var newFilterProvider2 = new Mock<IFilterProvider>().Object;
            defaultServices.ReplaceRange(typeof(IFilterProvider), new[] { filterProvider1, filterProvider2 });

            // Act
            defaultServices.InsertRange(typeof(IFilterProvider), 1, new[] { newFilterProvider1, newFilterProvider2 });

            // Assert
            Assert.Equal(new[] { filterProvider1, newFilterProvider1, newFilterProvider2, filterProvider2 }, defaultServices.GetServices(typeof(IFilterProvider)));
        }
Ejemplo n.º 25
0
        /// <summary>
        ///  Enables Redis message queuing.
        /// </summary>
        /// <param name="configuration">The <see cref="ProcessorConfiguration"/>.</param>
        /// <param name="redisConfiguration">The Redis configuration string.</param>
        /// <remarks>
        /// The runner count is <see cref="M:Environment.ProcessorsCount"/>.
        /// </remarks>
        public static void EnableRedisMessageQueuing(this ProcessorConfiguration configuration, string redisConfiguration)
        {
            int runnerCount = Environment.ProcessorCount;

            configuration.EnableRedisMessageQueuing(redisConfiguration, runnerCount);
        }
Ejemplo n.º 26
0
        public void RemoveAt_GuardClauses()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);

            // Act & assert
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.RemoveAt(serviceType: null, index: 0), "serviceType");
            ExceptionAssert.ThrowsArgument(() => defaultServices.RemoveAt(typeof(object), 0), "serviceType");
            ExceptionAssert.ThrowsArgumentOutOfRange(() => defaultServices.RemoveAt(typeof(IFilterProvider), -1), "index");
        }
Ejemplo n.º 27
0
        public void Remove_ObjectNotFound()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider1 = new Mock<IFilterProvider>().Object;
            var filterProvider2 = new Mock<IFilterProvider>().Object;
            var notPresentFilterProvider = new Mock<IFilterProvider>().Object;
            defaultServices.ReplaceRange(typeof(IFilterProvider), new[] { filterProvider1, filterProvider2 });

            // Act
            defaultServices.Remove(typeof(IFilterProvider), notPresentFilterProvider);

            // Assert
            Assert.Equal(new[] { filterProvider1, filterProvider2 }, defaultServices.GetServices(typeof(IFilterProvider)));
        }
Ejemplo n.º 28
0
        public void GetServices_PrependsServiceInDependencyInjectionContainer()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            IEnumerable<object> servicesBefore = defaultServices.GetServices(typeof(IFilterProvider));
            var filterProvider = new Mock<IFilterProvider>().Object;
            var mockDependencyResolver = new Mock<IDependencyResolver>();
            mockDependencyResolver.Setup(dr => dr.GetServices(typeof(IFilterProvider))).Returns(new[] { filterProvider });
            config.DependencyResolver = mockDependencyResolver.Object;

            // Act
            IEnumerable<object> servicesAfter = defaultServices.GetServices(typeof(IFilterProvider));

            // Assert
            Assert.Equal(new[] { filterProvider }.Concat(servicesBefore).ToArray(), servicesAfter.ToArray());
        }
Ejemplo n.º 29
0
        public void RemoveAll_GuardClauses()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);

            // Act & assert
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.RemoveAll(serviceType: null, match: _ => true), "serviceType");
            ExceptionAssert.ThrowsArgumentNull(() => defaultServices.RemoveAll(typeof(object), match: null), "match");
            ExceptionAssert.ThrowsArgument(() => defaultServices.RemoveAll(typeof(object), _ => true), "serviceType");
        }
Ejemplo n.º 30
0
        public void ReplaceRange_ReplacesAllValuesWithTheGivenServices()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider1 = new Mock<IFilterProvider>().Object;
            var filterProvider2 = new Mock<IFilterProvider>().Object;

            // Act
            defaultServices.ReplaceRange(typeof(IFilterProvider), new[] { filterProvider1, filterProvider2 });

            // Assert
            Assert.Equal(new[] { filterProvider1, filterProvider2 }, defaultServices.GetServices(typeof(IFilterProvider)));
        }
        public void CreateTranscientHandler_InstancianteEachTime()
        {
            // Arrange
            var config = new ProcessorConfiguration();
            config.DefaultHandlerLifetime = HandlerLifetime.Transient;
            ICommandHandlerActivator activator = new DefaultCommandHandlerActivator();
            CommandHandlerRequest request1 = new CommandHandlerRequest(config, this.command.Object);
            CommandHandlerRequest request2 = new CommandHandlerRequest(config, this.command.Object);
            CommandHandlerDescriptor descriptor = new CommandHandlerDescriptor(config, typeof(SimpleCommand), typeof(SimpleCommandHandler));
            CommandHandlerDescriptor descriptor2 = new CommandHandlerDescriptor(config, typeof(SimpleCommand), typeof(SimpleCommandHandler));

            // Act
            var handler1 = activator.Create(request1, descriptor);
            var handler2 = activator.Create(request1, descriptor);
            var handler3 = activator.Create(request2, descriptor);
            var handler4 = activator.Create(request1, descriptor2);

            // Assert
            Assert.NotNull(handler1);
            Assert.NotNull(handler2);
            Assert.NotNull(handler3);
            Assert.NotNull(handler4);
            Assert.NotSame(handler1, handler2);
            Assert.NotSame(handler1, handler3);
            Assert.NotSame(handler1, handler4);
            Assert.NotSame(handler2, handler3);
            Assert.NotSame(handler2, handler4);
            Assert.NotSame(handler3, handler4);
        }
Ejemplo n.º 32
0
        /// <summary>
        ///  Enables in-memory message queuing.
        /// </summary>
        /// <param name="configuration">The <see cref="ProcessorConfiguration"/>.</param>
        /// <remarks>
        /// The maximum degree of parallelism is <see cref="M:Environment.ProcessorsCount"/>.
        /// </remarks>
        public static void EnableInMemoryMessageQueuing(this ProcessorConfiguration configuration)
        {
            int runnerCount = Environment.ProcessorCount;

            configuration.EnableInMemoryMessageQueuing(runnerCount);
        }