public void When_add_duplicate_command_then_should_throw() { var module = new CommandHandlerModule(); module.For<Command>().Handle((_, __) => Task.FromResult(0)); Action act = () => module.For<Command>().Handle((_, __) => Task.FromResult(0)); act.ShouldThrow<InvalidOperationException>(); }
public void When_add_duplicate_command_then_should_throw() { var module = new CommandHandlerModule(); module.For <Command>().Finally((_, __) => Task.FromResult(0L)); Action act = () => module.For <Command>().Finally((_, __) => Task.FromResult(0L)); act.ShouldThrow <InvalidOperationException>(); }
public void Can_get_command_types() { var module = new CommandHandlerModule(); module.For <Command>().Finally((_, __) => Task.FromResult(-1L)); var commandTypes = module.CommandTypes.ToList(); commandTypes.Count.ShouldBe(1); commandTypes.Single().ShouldBe(typeof(Command)); }
public void Can_get_command_types() { var module = new CommandHandlerModule(); module.For<Command>().Handle((_,__) => Task.FromResult(0)); var commandTypes = module.CommandTypes.ToList(); commandTypes.Should().HaveCount(1); commandTypes.Single().Should().Be(typeof(Command)); }
public async Task Should_invoke_predispatch_hook() { var module = new CommandHandlerModule(); string correlationId = null; const string correlationIdKey = "CorrelationId"; module.For<Command>().Handle((commandMessage, __) => { correlationId = commandMessage.Metadata.Get<string>(correlationIdKey); return Task.FromResult(0); }); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).Name.ToLower(), typeof(Command) } }; var settings = new CommandHandlingSettings(new CommandHandlerResolver(module), commandMediaTypeMap) { OnPredispatch = (metadata, headers) => { var correlationIdHeader = headers.SingleOrDefault(kvp => kvp.Key == correlationIdKey); if(correlationIdHeader.Value != null) { metadata[correlationIdKey] = correlationIdHeader.Value.SingleOrDefault(); } } }; var midFunc = CommandHandlingMiddleware.HandleCommands(settings); using(var client = midFunc.CreateEmbeddedClient()) { await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap, customizeRequest: request => { request.Headers.Add(correlationIdKey, "cor-1"); }); correlationId.Should().Be("cor-1"); } }
public ProcessManagerHandlerTests() { TestLogger.Configure(); var source = new TaskCompletionSource<bool>(); _nodeStarted = source.Task; var notListening = new IPEndPoint(IPAddress.None, 0); _node = EmbeddedVNodeBuilder.AsSingleNode() .WithInternalTcpOn(notListening) .WithExternalTcpOn(notListening) .WithInternalHttpOn(notListening) .WithExternalHttpOn(notListening); _node.NodeStatusChanged += (_, e) => { if(e.NewVNodeState != VNodeState.Master) { return; } source.SetResult(true); }; _node.Start(); _connection = EmbeddedEventStoreConnection.Create(_node); _commands = new List<object>(); _serializer = new DefaultGetEventStoreJsonSerializer(); var commandHandlerModule = new CommandHandlerModule(); commandHandlerModule.For<ShipOrder>() .Handle((message, _) => { _commands.Add(message.Command); return Task.FromResult(0); }); commandHandlerModule.For<BillCustomer>() .Handle((message, _) => { _commands.Add(message.Command); return Task.FromResult(0); }); var resolver = new CommandHandlerResolver(commandHandlerModule); var commandHandlingMiddleware = CommandHandlingMiddleware.HandleCommands(new CommandHandlingSettings(resolver)); var embeddedClient = commandHandlingMiddleware.CreateEmbeddedClient(); var processHandler = ProcessHandler.For<OrderFulfillment, CompareablePosition>( (command, token) => embeddedClient.PutCommand(command, Guid.NewGuid()), new EventStoreClientProcessManagerCheckpointRepository(_connection, _serializer)) .CorrelateBy<OrderPlaced>(e => e.DomainEvent.OrderId.ToString()) .CorrelateBy<OrderShipped>(e => e.DomainEvent.OrderId.ToString()) .CorrelateBy<BillingSucceeded>(e => e.DomainEvent.OrderId.ToString()) .CorrelateBy<BillingFailed>(e => e.DomainEvent.OrderId.ToString()); _dispatcher = new ResolvedEventDispatcher(_connection, new DefaultGetEventStoreJsonSerializer(), new InMemoryCheckpointRepository(), processHandler.BuildHandlerResolver(), () => { }); _orderId = Guid.NewGuid(); _streamId = ("orders-" + _orderId.ToString("n")).FormatStreamIdWithBucket(); _correlationId = _orderId.ToString(); }
public ProcessManagerHandlerTests() { TestLogger.Configure(); var source = new TaskCompletionSource <bool>(); _nodeStarted = source.Task; var notListening = new IPEndPoint(IPAddress.None, 0); _node = EmbeddedVNodeBuilder.AsSingleNode() .WithInternalTcpOn(notListening) .WithExternalTcpOn(notListening) .WithInternalHttpOn(notListening) .WithExternalHttpOn(notListening); _node.NodeStatusChanged += (_, e) => { if (e.NewVNodeState != VNodeState.Master) { return; } source.SetResult(true); }; _node.Start(); _connection = EmbeddedEventStoreConnection.Create(_node); _commands = new List <object>(); _serializer = new DefaultGetEventStoreJsonSerializer(); var commandHandlerModule = new CommandHandlerModule(); commandHandlerModule.For <ShipOrder>() .Handle((message, _) => { _commands.Add(message.Command); return(Task.FromResult(0)); }); commandHandlerModule.For <BillCustomer>() .Handle((message, _) => { _commands.Add(message.Command); return(Task.FromResult(0)); }); var resolver = new CommandHandlerResolver(commandHandlerModule); var commandHandlingMiddleware = CommandHandlingMiddleware.HandleCommands(new CommandHandlingSettings(resolver)); var embeddedClient = commandHandlingMiddleware.CreateEmbeddedClient(); var processHandler = ProcessHandler.For <OrderFulfillment, CompareablePosition>( (command, token) => embeddedClient.PutCommand(command, Guid.NewGuid()), new EventStoreClientProcessManagerCheckpointRepository(_connection, _serializer)) .CorrelateBy <OrderPlaced>(e => e.DomainEvent.OrderId.ToString()) .CorrelateBy <OrderShipped>(e => e.DomainEvent.OrderId.ToString()) .CorrelateBy <BillingSucceeded>(e => e.DomainEvent.OrderId.ToString()) .CorrelateBy <BillingFailed>(e => e.DomainEvent.OrderId.ToString()); _dispatcher = new ResolvedEventDispatcher(_connection, new DefaultGetEventStoreJsonSerializer(), new InMemoryCheckpointRepository(), processHandler.BuildHandlerResolver(), () => { }); _orderId = Guid.NewGuid(); _streamId = ("orders-" + _orderId.ToString("n")).FormatStreamIdWithBucket(); _correlationId = _orderId.ToString(); }
public static CommandHandlerResolver WhenEqualToMessage(CommandHandlerModule module) => WhenEqualToMessage(module.Handlers);