public void EventListenerReceivesMessageAsync(string firstName, string lastName, string email, string phone) { // Arrange using var busContext = new RabbitMqContextBuilder() .WithExchange("TestExchange") .WithConnectionString("amqp://*****:*****@localhost") .CreateContext(); using var host = new MicroserviceHostBuilder() .WithBusContext(busContext) .AddEventListener <PersonEventListener>() .CreateHost(); host.Start(); var publisher = new EventPublisher(busContext); var personEvent = new PersonAddedEvent { Person = new Person { FirstName = firstName, LastName = lastName, Email = email, PhoneNumber = phone } }; // Act publisher.PublishAsync(personEvent); Thread.Sleep(WaitTime); // Assert Assert.AreEqual(personEvent, PersonEventListener.ResultEvent); }
public void CommandReturnsProperException(string message) { // Arrange using var busContext = new RabbitMqContextBuilder() .WithExchange("TestExchange") .WithConnectionString("amqp://*****:*****@localhost") .CreateContext(); using var host = new MicroserviceHostBuilder() .WithBusContext(busContext) .WithQueueName("QueueName2") .AddEventListener <ErrorEventListener>() .CreateHost(); host.Start(); var command = new DummyCommand(message); var publisher = new CommandPublisher(busContext); // Act Task <DummyCommand> Act() => publisher.PublishAsync <DummyCommand>(command); // Arrange var exception = Assert.ThrowsExceptionAsync <DestinationQueueException>(Act); Assert.AreEqual("Received error command from queue Test.Command.Listener", exception.Result.Message); }
public void EventListenerHandlesMultipleEvents(params string[] names) { // Arrange using var busContext = new RabbitMqContextBuilder() .WithExchange("TestExchange") .WithConnectionString("amqp://*****:*****@localhost") .CreateContext(); using var host = new MicroserviceHostBuilder() .WithBusContext(busContext) .WithQueueName("QueueName3") .AddEventListener <SpamEventListener>() .CreateHost(); host.Start(); var publisher = new EventPublisher(busContext); var catEvents = names.Select(e => new CatAddedEvent { Cat = new Cat { Name = e } }).ToList(); // Act foreach (var @event in catEvents) { publisher.Publish(@event); } Thread.Sleep(WaitTime); CollectionAssert.AreEquivalent(catEvents, SpamEventListener.ResultEvents); }
public void CatEventDoesNotTriggerPersonEvent(string name) { // Arrange using var busContext = new RabbitMqContextBuilder() .WithExchange("TestExchange") .WithConnectionString("amqp://*****:*****@localhost") .CreateContext(); using var host = new MicroserviceHostBuilder() .WithBusContext(busContext) .AddEventListener <PersonEventListener>() .AddEventListener <CatEventListener>() .CreateHost(); host.Start(); var publisher = new EventPublisher(busContext); var catEvent = new CatAddedEvent { Cat = new Cat { Name = name } }; // Act publisher.Publish(catEvent); Thread.Sleep(WaitTime); // Assert Assert.IsNull(PersonEventListener.ResultEvent); Assert.AreEqual(catEvent, CatEventListener.ResultEvent); }
static void Main(string[] args) { using ILoggerFactory loggerFactory = LoggerFactory.Create(configure => { string logLevelString = Environment.GetEnvironmentVariable(EnvVarNames.LogLevel) ?? "Information"; LogLevel logLevel = Enum.Parse <LogLevel>(logLevelString); configure.AddConsole().SetMinimumLevel(logLevel); }); MiffyLoggerFactory.LoggerFactory = loggerFactory; RabbitMqLoggerFactory.LoggerFactory = loggerFactory; using IBusContext <IConnection> context = new RabbitMqContextBuilder() .ReadFromEnvironmentVariables() .CreateContext(); using IMicroserviceHost host = new MicroserviceHostBuilder() .SetLoggerFactory(loggerFactory) .WithBusContext(context) .RegisterDependencies(services => { services.AddDbContext <AuditLogContext>(config => { config.UseMySql(Environment.GetEnvironmentVariable(EnvVarNames.DatabaseConnectionString)); config.UseLoggerFactory(loggerFactory); }, ServiceLifetime.Singleton); services.AddTransient <IAuditLogItemRepository, AuditLogItemRepository>(); services.AddTransient <IEventPublisher, EventPublisher>(); using var serviceScope = services.BuildServiceProvider().GetRequiredService <IServiceScopeFactory>().CreateScope(); var auditLogContext = serviceScope.ServiceProvider.GetService <AuditLogContext>(); auditLogContext.Database.Migrate(); }) .UseConventions() .CreateHost(); host.Start(); // Keep app running new ManualResetEvent(false).WaitOne(); }
public void EventListenerDoesCallListenerOnInvalidJson(string body) { // Arrange using var busContext = new RabbitMqContextBuilder() .WithExchange("TestExchange") .WithConnectionString("amqp://*****:*****@localhost") .CreateContext(); using var host = new MicroserviceHostBuilder() .WithBusContext(busContext) .AddEventListener <PersonEventListener>() .CreateHost(); host.Start(); EventPublisher publisher = new EventPublisher(busContext); // Act publisher.Publish(242424, "PeopleApp.Persons.New", Guid.NewGuid(), "PersonEvent", body); Thread.Sleep(WaitTime); // Assert Assert.IsNull(PersonEventListener.ResultEvent); }