public void UseConventionsRegistersProperTopics(string messageText) { // Arrange var testContext = new TestBusContext(); var hostBuilder = new MicroserviceHostBuilder().WithBusContext(testContext); // Act hostBuilder.UseConventions(); hostBuilder.CreateHost().Start(); // Assert var message = new DummyEvent("IrrelevantTopic") { DummyText = messageText }; new EventPublisher(testContext).Publish(message); Thread.Sleep(WaitTime); Assert.IsNull(EventListenerDummy.HandlesResult); Assert.IsNull(EventListenerDummy2.HandlesResult); Assert.AreEqual(message, EventListenerDummy3.HandlesResult); }
public void TestCommandReceiverCreateTest() { TestBusContext context = new TestBusContext(); var target = context.CreateCommandReceiver("queue"); Assert.AreEqual("queue", target.QueueName); }
public void Initialize() { connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); options = new DbContextOptionsBuilder <KlantContext>() .UseSqlite(connection) .Options; using (var context = new KlantContext(options)) { context.Database.EnsureCreated(); } _context = new TestBusContext(); var services = new ServiceCollection(); services.AddTransient <ICommandPublisher, CommandPublisher>(); services.AddSingleton <DbContextOptions>(options); services.AddSingleton <KlantContext>(); services.AddTransient <IKlantDatamapper, KlantDatamapper>(); services.AddSingleton <IBusContext <IConnection> >(_context); services.AddTransient <IEventPublisher, EventPublisher>(); var builder = new MicroserviceHostBuilder() .RegisterDependencies(services) .WithContext(_context) .AddCommandListener <KlantListener>(); _host = builder.CreateHost(); _host.StartListening(); Thread.Sleep(1000); }
public void Initialize() { connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); options = new DbContextOptionsBuilder <WebshopContext>() .UseSqlite(connection) .Options; using (var context = new WebshopContext(options)) { context.Database.EnsureCreated(); } var services = new ServiceCollection(); services.AddSingleton <DbContextOptions>(options); services.AddTransient <WebshopContext, WebshopContext>(); services.AddTransient <IArtikelDatamapper, ArtikelDatamapper>(); services.AddTransient <IKlantDatamapper, KlantDatamapper>(); _context = new TestBusContext(); var builder = new MicroserviceHostBuilder() .RegisterDependencies(services) .WithContext(_context) .AddEventListener <MagazijnListener>(); _host = builder.CreateHost(); _host.StartListening(); }
public void StartReceivingCommandsTest() { TestBusContext context = new TestBusContext(); var target = context.CreateCommandReceiver("queue"); target.DeclareCommandQueue(); context.DeclareCommandQueue("responseQueue"); var autoReset = new AutoResetEvent(false); target.StartReceivingCommands((cm) => { autoReset.Set(); return(Task.Run(() => new CommandResponseMessage(cm.Message, null, null))); }); context.CommandQueues["queue"].Enqueue(new TestBusCommandMessage(new CommandRequestMessage("message", null), new BasicProperties() { ReplyTo = "responseQueue" })); bool succes = autoReset.WaitOne(5000); Assert.IsTrue(succes); Assert.AreEqual(0, context.CommandQueues["queue"].Count); Thread.Sleep(100); Assert.AreEqual(1, context.CommandQueues["responseQueue"].Count); }
public void ResumingEventListenerReActivatesDeliveryInQueue(string queueName) { // Arrange TestBusContext context = new TestBusContext(); IMessageSender sender = context.CreateMessageSender(); IMessageReceiver receiver = context.CreateMessageReceiver(queueName, new [] { "TestTopic" }); EventMessage message = new EventMessage { Topic = "TestTopic" }; bool callbackCalled = false; receiver.StartReceivingMessages(); receiver.StartHandlingMessages(e => callbackCalled = true); receiver.Pause(); // Act sender.SendMessage(message); Thread.Sleep(WaitTime); receiver.Resume(); Thread.Sleep(WaitTime); // Assert Assert.IsTrue(callbackCalled); }
public void MessageReceiverCallbackGetsCalledMultipleTimesWhenListeningLonger() { var context = new TestBusContext(); var receiver = context.CreateMessageReceiver("receiver", new List <string> { "#" }); var autoResetEvent = new AutoResetEvent(false); var messages = new List <IEventMessage>(); receiver.DeclareQueue(); context.TestQueues["receiver"].Queue.Enqueue(new EventMessage("message", "1")); context.TestQueues["receiver"].Queue.Enqueue(new EventMessage("message", "2")); context.TestQueues["receiver"].Queue.Enqueue(new EventMessage("message", "3")); receiver.StartReceivingMessages((e) => { messages.Add(e); autoResetEvent.Set(); }); autoResetEvent.WaitOne(500); autoResetEvent.WaitOne(500); autoResetEvent.WaitOne(500); Assert.AreEqual(3, messages.Count); Assert.IsTrue(messages.Any(m => m.Message == "1")); Assert.IsTrue(messages.Any(m => m.Message == "2")); Assert.IsTrue(messages.Any(m => m.Message == "3")); }
public void StringEventListenerReceivesProperString(string text) { // Arrange var testContext = new TestBusContext(); var hostBuilder = new MicroserviceHostBuilder().WithBusContext(testContext); using var host = hostBuilder .WithQueueName("test.queue") .AddEventListener <StringEventListenerDummy>() .CreateHost(); host.Start(); //Act var message = new DummyEvent("test.topic") { DummyText = text }; new EventPublisher(testContext).Publish(message); // Assert Thread.Sleep(WaitTime); var expectedResult = JsonConvert.SerializeObject(message); Assert.AreEqual(expectedResult, StringEventListenerDummy.ReceivedData); }
public void PausingEventListenerPausesDeliveryInQueue(string queueName) { var context = new TestBusContext(); var sender = context.CreateCommandSender(); var receiver = context.CreateCommandReceiver(queueName); receiver.DeclareCommandQueue(); var command = new CommandMessage { DestinationQueue = queueName, CorrelationId = Guid.NewGuid(), Body = Encoding.Unicode.GetBytes("Hello World") }; bool messageReceived = false; receiver.StartReceivingCommands(e => { messageReceived = true; return(new CommandMessage()); }); // Act receiver.Pause(); sender.SendCommandAsync(command); Thread.Sleep(WaitTime); // Assert Assert.AreEqual(false, messageReceived); }
public void SentMessageIsProperlySentAndReceivedWithManipulations(string startMessage, string appendMessage) { // Arrange var context = new TestBusContext(); var sender = context.CreateCommandSender(); var receiver = context.CreateCommandReceiver("dest.queue"); receiver.DeclareCommandQueue(); var command = new CommandMessage { DestinationQueue = "dest.queue", CorrelationId = Guid.NewGuid(), Body = Encoding.Unicode.GetBytes(startMessage) }; // Act receiver.StartReceivingCommands(e => { var textBody = Encoding.Unicode.GetString(e.Body); return(new CommandMessage { Body = Encoding.Unicode.GetBytes(textBody + appendMessage) }); }); var result = sender.SendCommandAsync(command); // Assert Assert.AreEqual(startMessage + appendMessage, Encoding.Unicode.GetString(result.Result.Body)); }
public void SentMessageIsProperlyReceivedAndReturned(string destQueue, string expected) { // Arrange var context = new TestBusContext(); var sender = context.CreateCommandSender(); var receiver = context.CreateCommandReceiver(destQueue); receiver.DeclareCommandQueue(); var command = new CommandMessage { DestinationQueue = destQueue, CorrelationId = Guid.NewGuid() }; // Act receiver.StartReceivingCommands(e => new CommandMessage { Body = Encoding.Unicode.GetBytes(expected) }); var result = sender.SendCommandAsync(command); // Assert Assert.AreEqual(expected, Encoding.Unicode.GetString(result.Result.Body)); }
public void CreateTestCommandSenderDeclaresNewQueue() { TestBusContext context = new TestBusContext(); var sender = context.CreateCommandSender(); Assert.IsInstanceOfType(typeof(TestCommandSender), sender.GetType().BaseType); Assert.AreEqual(1, context.CommandQueues.Count); }
public void StartListeningTwiceThrowsException() { TestBusContext context = new TestBusContext(); var receiver = context.CreateCommandReceiver("queue"); receiver.DeclareCommandQueue(); receiver.StartReceivingCommands((cm) => { return(Task.Run(() => new CommandResponseMessage(cm.Message, "", cm.CorrelationId))); }); Assert.ThrowsException <BusConfigurationException>(() => receiver.StartReceivingCommands((cm) => Task.Run(() => new CommandResponseMessage(cm.Message, "", cm.CorrelationId)))); }
public void DeclaringQueueTwiceThrowsException() { TestBusContext context = new TestBusContext(); var receiver = context.CreateCommandReceiver("queue"); receiver.DeclareCommandQueue(); Assert.ThrowsException <BusConfigurationException>(() => receiver.DeclareCommandQueue()); }
public void GenerateRandomQueueNameTest() { TestBusContext context = new TestBusContext(); var sender = (TestCommandSender)context.CreateCommandSender(); var result = sender.GenerateRandomQueueName(); Assert.AreEqual(30, result.Length); }
public void DeclareCommandQueueTest() { TestBusContext context = new TestBusContext(); var target = context.CreateCommandReceiver("queue"); target.DeclareCommandQueue(); Assert.AreEqual(1, context.CommandQueues.Count); Assert.AreEqual("queue", context.CommandQueues.First().Key); }
public void MessageReceiverReturnsExceptionWhenNoQueueIsDefined() { var context = new TestBusContext(); var receiver = context.CreateMessageReceiver("receiver", new List <string> { "#" }); Assert.ThrowsException <KeyNotFoundException>(() => receiver.StartReceivingMessages(null)); }
public void CreateMessageReceiverReturnsMessageReceiver() { // Arrange var context = new TestBusContext(); // Act var result = context.CreateMessageReceiver("test.queue", new string[0]); // Assert Assert.IsInstanceOfType(result, typeof(TestMessageReceiver)); }
public void CreateCommandReceiver_ReturnsTestCommandReceiver() { // Arrange TestBusContext context = new TestBusContext(); // Act var receiver = context.CreateCommandReceiver("TestQueue"); // Assert Assert.IsInstanceOfType(receiver, typeof(TestCommandReceiver)); }
public void CreateMessageSenderReturnsCommandSender() { // Arrange var context = new TestBusContext(); // Act var result = context.CreateCommandSender(); // Assert Assert.IsInstanceOfType(result, typeof(TestCommandSender)); }
public void CreateCommandSender_ReturnsTestCommandSender() { // Arrange TestBusContext context = new TestBusContext(); // Act var sender = context.CreateCommandSender(); // Assert Assert.IsInstanceOfType(sender, typeof(TestCommandSender)); }
public void CreateCommandReceiverReturnsCommandReceiver() { // Arrange var context = new TestBusContext(); // Act var result = context.CreateCommandReceiver("test.queue"); // Assert Assert.IsInstanceOfType(result, typeof(TestCommandReceiver)); }
static void Main(string[] args) { ILoggerFactory loggerFactory = new LoggerFactory(); //Deprecated method, maar kan even niet anders ConsoleLoggerOptions options = new ConsoleLoggerOptions(); loggerFactory.AddProvider( new ConsoleLoggerProvider( (text, logLevel) => logLevel >= LogLevel.Debug, true)); //192.168.99.100 var connectionBuilder = new RabbitMQContextBuilder() .WithExchange("MVM.EventExchange") .WithAddress("localhost", 5672) .WithCredentials(userName: "******", password: "******"); var context = new TestBusContext(); var builder = new MicroserviceHostBuilder() .SetLoggerFactory(loggerFactory) .RegisterDependencies((services) => { services.AddTransient <IDataMapper, SinaasAppelDataMapper>(); services.AddTransient <ICommandPublisher, CommandPublisher>(); services.AddTransient <IEventPublisher, EventPublisher>(); services.AddSingleton <IBusContext <IConnection> >(context); }) .WithContext(context) .UseConventions(); var host = builder.CreateHost(); host.StartListeningInOtherThread(); Console.WriteLine("ServiceHost is listening to incoming events..."); Console.WriteLine("Press any key to quit."); var publisher = new EventPublisher(context); var commandpublisher = new CommandPublisher(context); publisher.Publish(new PolisToegevoegdEvent("MVM.Polisbeheer.PolisToegevoegd") { Message = "Hey" }); publisher.Publish(new HenkToegevoegdEvent("Test") { Test = "Oi" }); }
public void MessageReceiverCallingQueueDeclaredTwiceThrowsException() { var context = new TestBusContext(); var receiver = context.CreateMessageReceiver("receiver", new List <string> { "#" }); receiver.DeclareQueue(); Assert.ThrowsException <BusConfigurationException>(() => receiver.DeclareQueue()); }
public void DeclareCommandQueue_AddsQueue() { // Arrange TestBusContext context = new TestBusContext(); // Act context.DeclareCommandQueue("TestQueue"); // Assert Assert.AreEqual(1, context.CommandQueues.Count); Assert.IsNotNull(context.CommandQueues["TestQueue"]); }
public void CreateMessageReceiver_ReturnsTestMessageReceiver() { // Arrange TestBusContext context = new TestBusContext(); // Act var receiver = context.CreateMessageReceiver("TestQueue", new List <string> { "test.routing.key" }); // Assert Assert.IsInstanceOfType(receiver, typeof(TestMessageReceiver)); }
public void DeclareQueue_AddsQueue() { // Arrange TestBusContext context = new TestBusContext(); // Act context.DeclareQueue("TestQueue", new List <string> { "test.routing.key" }); // Assert Assert.AreEqual(1, context.TestQueues.Count); Assert.IsNotNull(context.TestQueues["TestQueue"]); }
public void ExceptionIsThrownOnCommandQueueNotDeclared(string queueName) { // Arrange var testContext = new TestBusContext(); var testCommandreceiver = new TestCommandReceiver(testContext, queueName); // Act void Act() => testCommandreceiver.StartReceivingCommands(e => e); // Assert var exception = Assert.ThrowsException <BusConfigurationException>(Act); Assert.AreEqual($"Queue {queueName} has not been declared yet.", exception.Message); }
public void ResumeThrowsExceptionWhenQueueNotDeclared() { // Arrange var context = new TestBusContext(); var receiver = new TestCommandReceiver(context, "queue"); // Act void Act() => receiver.Resume(); // Assert BusConfigurationException exception = Assert.ThrowsException <BusConfigurationException>(Act); Assert.AreEqual("Attempting to resume the TestCommandReceiver, but it is not even receiving messages.", exception.Message); }
public void PausePausesReceiver() { // Arrange var context = new TestBusContext(); var receiver = new TestCommandReceiver(context, "queue"); receiver.DeclareCommandQueue(); // Act receiver.Pause(); // Assert Assert.IsTrue(receiver.IsPaused); }