public async Task SendAsync_throws_when_request_is_null() { Mock.Get(_instanceFactory) .Setup(f => f.Invoke(It.IsAny <Type>())) .Returns((Func <Type, object>)_containerFixture.Container.GetInstance); Func <Task> sendFunc = () => _sut.SendAsync <string>(request: null); await sendFunc.Should().ThrowExactlyAsync <ArgumentNullException>(); Mock.Get(_instanceFactory) .Verify(f => f.Invoke(It.IsAny <Type>()), Times.Never()); }
public void Bus_SendAsync_Nominal() { QueueReader.CleanQueueAsync(SERVICENAME).Wait(); var id = Guid.NewGuid(); var message = new TestMessage() { SomeData = id.ToString() }; var serializer = new JSONSerializer(); var publisher = new Mock <IPublishEvents>(); IBus bus = new Bus(serializer, new AzureStorageQueueSendTransport(serializer), publisher.Object); bus.Context = new AFBusMessageContext(); bus.SendAsync(message, SERVICENAME).Wait(); var stringMessage = QueueReader.ReadOneMessageFromQueueAsync(SERVICENAME).Result; var finalMessageEnvelope = JsonConvert.DeserializeObject <AFBusMessageEnvelope>(stringMessage, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); var finalMessage = JsonConvert.DeserializeObject <TestMessage>(finalMessageEnvelope.Body, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); Assert.IsTrue(id.ToString() == finalMessage.SomeData); }
public void When_sending_a_command_and_there_are_no_command_handlers_should_throw_exception() { var serviceContainer = new ServiceContainer(); var bus = new Bus(serviceContainer.GetAllInstances); var command = new Command(); Assert.Throws<NotSupportedException>(() => bus.SendAsync(command).Wait()); }
public void When_sending_a_command_and_there_is_only_one_command_handler_should_invoke_command_handler() { var serviceContainer = new ServiceContainer(); serviceContainer.Register<IHandleMessages<Command>, CommandHandler>(); var bus = new Bus(serviceContainer.GetAllInstances); var command = new Command(); bus.SendAsync(command); command.IsHandled.ShouldBeTrue(); }
public void When_sending_a_command_should_polymorphic_dispatch_to_command_handlers() { var serviceContainer = new ServiceContainer(); serviceContainer.Register<IHandleMessages<Command>, CommandHandler>(); var messageHandler = new MessageHandler(); serviceContainer.RegisterInstance<IHandleMessages<IMessage>>(messageHandler); var bus = new Bus(serviceContainer.GetAllInstances); var command = new Command(); bus.SendAsync(command); command.IsHandled.ShouldBeTrue(); messageHandler.IsHandled.ShouldBeTrue(); }
public void Bus_SendAsync_DelayedMessage() { QueueReader.CleanQueueAsync(SERVICENAME).Wait(); var message = new TestMessage() { SomeData = "delayed" }; var serializer = new JSONSerializer(); var publisher = new Mock <IPublishEvents>(); IBus bus = new Bus(serializer, new AzureStorageQueueSendTransport(serializer), publisher.Object); bus.Context = new AFBusMessageContext(); var before = DateTime.Now; var timeDelayed = new TimeSpan(0, 0, 5); bus.SendAsync(message, SERVICENAME, timeDelayed).Wait(); string stringMessage = null; do { stringMessage = QueueReader.ReadOneMessageFromQueueAsync(SERVICENAME).Result; }while (string.IsNullOrEmpty(stringMessage)); var after = DateTime.Now; var finalMessageEnvelope = JsonConvert.DeserializeObject <AFBusMessageEnvelope>(stringMessage, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); var finalMessage = JsonConvert.DeserializeObject <TestMessage>(finalMessageEnvelope.Body, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); Assert.IsTrue(after - before > timeDelayed, "Delay failed"); }
static async Task Main(string[] args) { var appSettings = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) //.AddJsonFile("AppSettings.json") .AddUserSecrets("tsumugi") .AddEnvironmentVariables() .Build(); var configuration = new TsumugiConfiguration(appSettings["Tsumugi:ConnectionString"], appSettings["Tsumugi:Queue"], appSettings["Tsumugi:Channel"], appSettings["Tsumugi:Emoji"], appSettings["Tsumugi:Bot"]); var bus = new Bus(configuration); bus.ErrorHandler += exception => Console.WriteLine($"Error: {exception.Message}."); await bus.SendAsync("Aloha 2 *dudes*!"); //bus.Send("general", ":panda_face:", "Tsumugi", "Wellp...", new List<Attachment> // { // new Attachment // { // Fields = new List<Field> // { // new Field("ID", "1", true), // new Field("Name", "Sushi", true) // } // }, // new Attachment // { // Fields = new List<Field> // { // new Field("ID", "2", true), // new Field("Name", "Sashimi", true) // } // } // }); }
public async Task SendAsync <TEvent>(TEvent @event) where TEvent : class, IEvent => await Bus.SendAsync(nameof(EventBus), @event);
public async Task SendAsync <TEvent>(TEvent @event) where TEvent : class => await Bus.SendAsync("EventBus", @event);
public async Task When_sending_a_command_should_invoke_command_handler_and_be_able_to_wait_for_async_result() { var serviceContainer = new ServiceContainer(); var bus = new Bus(serviceContainer.GetAllInstances); serviceContainer.Register<IBus>(sf => bus); serviceContainer.Register<IHandleMessages<AsyncCommand>, AsyncCommandHandler>(); var command = new AsyncCommand(); await bus.SendAsync(command); command.IsHandled.ShouldBeTrue(); }
public void When_publishing_an_event_in_a_command_handler_should_handle_event() { var serviceContainer = new ServiceContainer(); var bus = new Bus(serviceContainer.GetAllInstances); serviceContainer.Register<IBus>(sf => bus); serviceContainer.Register<IHandleMessages<Command>, CommandHandlerThatSendsAnEvent>(); serviceContainer.Register<IHandleMessages<EventWithCommand>, EventWithCommandHandler>(); var command = new Command(); bus.SendAsync(command); command.IsHandled.ShouldBeTrue(); }
public void When_query_handler_throws_exception_should_propagate_exception() { var serviceContainer = new ServiceContainer(); serviceContainer.Register<IHandleQueries<QueryWithExcepetion, Response>, QueryWithExceptionHandler>(); var bus = new Bus(serviceContainer.GetAllInstances); Assert.Throws<InvalidOperationException>(() => bus.SendAsync(new QueryWithExcepetion()).Wait()); }
public void When_command_handler_throws_exception_should_propagate_exception() { var serviceContainer = new ServiceContainer(); serviceContainer.Register<IHandleMessages<CommandWithException>, CommandHandlerThatThrowException>(); var bus = new Bus(serviceContainer.GetAllInstances); Assert.Throws<InvalidOperationException>(() => bus.SendAsync(new CommandWithException()).Wait()); }
public void When_sending_a_query_and_there_are_multiple_query_handlers_should_throw_exception() { var serviceContainer = new ServiceContainer(); serviceContainer.Register<IHandleQueries<Query, Response>, QueryHandler>(); serviceContainer.Register<IHandleQueries<Query, Response>, AnotherQueryHandler>("another"); var bus = new Bus(serviceContainer.GetAllInstances); var query = new Query(); Assert.Throws<NotSupportedException>(() => bus.SendAsync(query).Wait()); }
public void When_sending_a_query_and_there_is_only_one_query_handler_should_invoke_query_handler() { var serviceContainer = new ServiceContainer(); serviceContainer.Register<IHandleQueries<Query, Response>, QueryHandler>(); var bus = new Bus(serviceContainer.GetAllInstances); var query = new Query(); var response = bus.SendAsync(query); response.Result.IsHandled.ShouldBeTrue(); }