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); }
private static Lazy<ProcessorConfiguration> CreateConfiguration() { return new Lazy<ProcessorConfiguration>(delegate { ProcessorConfiguration config = new ProcessorConfiguration(); return config; }); }
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); }
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); }
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); }
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()); }
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(); } }
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); }
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()); }
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()); }
/// <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; } }
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); }
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; }
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. }
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()); }
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()); }
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))); }
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"); }
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); }
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); }
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))); }
/// <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); }
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"); }
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))); }
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()); }
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"); }
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); }
/// <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); }