public void SendCommandCallsBasicPublish(string queueName) { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); var modelMock = new Mock <IModel>(); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); connectionMock.Setup(e => e.CreateModel()).Returns(modelMock.Object); modelMock.Setup(e => e.QueueDeclare("", true, false, true, null)) .Returns(new QueueDeclareOk(queueName, 0, 0)); modelMock.Setup(e => e.CreateBasicProperties()).Returns(new BasicProperties()); var sender = new RabbitMqCommandSender(contextMock.Object); var command = new CommandMessage { DestinationQueue = queueName }; // Act sender.SendCommandAsync(command); Thread.Sleep(WaitTime); // Assert modelMock.Verify(e => e.BasicPublish("", queueName, true, It.IsAny <BasicProperties>(), It.IsAny <byte[]>())); }
public void SendCommandCallsBasicPublishThrowsExceptionOnTimeout(string queueName, string exchangeName) { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); var modelMock = new Mock <IModel>(); contextMock.SetupGet(e => e.ExchangeName).Returns(exchangeName); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); connectionMock.Setup(e => e.CreateModel()).Returns(modelMock.Object); modelMock.Setup(e => e.QueueDeclare("", true, false, true, null)) .Returns(new QueueDeclareOk(queueName, 0, 0)); modelMock.Setup(e => e.CreateBasicProperties()).Returns(new BasicProperties()); var sender = new RabbitMqCommandSender(contextMock.Object); var command = new CommandMessage { DestinationQueue = queueName }; // Act async Task <CommandMessage> Act() => await sender.SendCommandAsync(command); // Assert var exception = Assert.ThrowsExceptionAsync <MessageTimeoutException>(Act); Assert.AreEqual($"No response received from queue {queueName} after {RabbitMqCommandSender.CommandTimeout}ms", exception.Result.Message); }
public void CommandMessageIsReturnedProperly(string correlationId) { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); var modelMock = new Mock <IModel>(); contextMock.SetupGet(e => e.ExchangeName).Returns("test.queue"); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); connectionMock.Setup(e => e.CreateModel()).Returns(modelMock.Object); modelMock.Setup(e => e.QueueDeclare("", true, false, true, null)) .Returns(new QueueDeclareOk("test.queue", 0, 0)); modelMock.Setup(e => e.CreateBasicProperties()).Returns(new BasicProperties()); IBasicConsumer consumer = null; modelMock.Setup(e => e.BasicConsume("test.queue", false, It.IsAny <string>(), false, false, null, It.IsAny <EventingBasicConsumer>())) .Callback <string, bool, string, bool, bool, IDictionary <string, object>, IBasicConsumer>((a, b, c, d, e, f, receivedConsumer) => consumer = receivedConsumer); var sender = new RabbitMqCommandSender(contextMock.Object); var command = new CommandMessage { DestinationQueue = "test.queue", CorrelationId = Guid.Parse(correlationId) }; var result = sender.SendCommandAsync(command); Thread.Sleep(WaitTime); BasicProperties testBasicProperties = new BasicProperties { CorrelationId = correlationId }; var body = new CommandError { EventType = "DummyCommand", Exception = new Exception("TestException"), CorrelationId = Guid.Parse(correlationId) }; var bodyJson = JsonConvert.SerializeObject(body); // Act consumer.HandleBasicDeliver("tag", 20, false, "", "", testBasicProperties, Encoding.Unicode.GetBytes(bodyJson)); // Assert Assert.IsInstanceOfType(result.Result, typeof(CommandMessage)); }
public void ConsumerExitsIfCorrelationIdIsNotCorrect(string queueName) { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); var modelMock = new Mock <IModel>(); contextMock.SetupGet(e => e.ExchangeName).Returns(queueName); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); connectionMock.Setup(e => e.CreateModel()).Returns(modelMock.Object); modelMock.Setup(e => e.QueueDeclare("", true, false, true, null)) .Returns(new QueueDeclareOk(queueName, 0, 0)); modelMock.Setup(e => e.CreateBasicProperties()).Returns(new BasicProperties()); IBasicConsumer consumer = null; modelMock.Setup(e => e.BasicConsume(queueName, false, It.IsAny <string>(), false, false, null, It.IsAny <EventingBasicConsumer>())) .Callback <string, bool, string, bool, bool, IDictionary <string, object>, IBasicConsumer>((a, b, c, d, e, f, receivedConsumer) => consumer = receivedConsumer); var sender = new RabbitMqCommandSender(contextMock.Object); var command = new CommandMessage { DestinationQueue = queueName }; sender.SendCommandAsync(command); Thread.Sleep(WaitTime); BasicProperties testBasicProperties = new BasicProperties { CorrelationId = "ID" }; // Act consumer.HandleBasicDeliver("tag", 20, false, "", "", testBasicProperties, new byte[0]); // Assert modelMock.Verify(e => e.BasicAck(20, false), Times.Never); }
public void SendCommandCreatesChannel() { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); var sender = new RabbitMqCommandSender(contextMock.Object); var command = new CommandMessage(); // Act sender.SendCommandAsync(command); Thread.Sleep(WaitTime); // Assert connectionMock.Verify(e => e.CreateModel(), Times.Once); }
public void SendCommandCallsQueueDeclare() { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); var modelMock = new Mock <IModel>(); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); connectionMock.Setup(e => e.CreateModel()).Returns(modelMock.Object); var sender = new RabbitMqCommandSender(contextMock.Object); var command = new CommandMessage(); // Act sender.SendCommandAsync(command); Thread.Sleep(WaitTime); // Assert modelMock.Verify(e => e.QueueDeclare("", true, false, true, null), Times.Once()); }
public void ExceptionIsThrownIfEventTypeIsCommandError(string exceptionMessage) { // Arrange var connectionMock = new Mock <IConnection>(); var contextMock = new Mock <IBusContext <IConnection> >(); var modelMock = new Mock <IModel>(); contextMock.SetupGet(e => e.ExchangeName).Returns("test.queue"); contextMock.SetupGet(e => e.Connection).Returns(connectionMock.Object); connectionMock.Setup(e => e.CreateModel()).Returns(modelMock.Object); modelMock.Setup(e => e.QueueDeclare("", true, false, true, null)) .Returns(new QueueDeclareOk("test.queue", 0, 0)); modelMock.Setup(e => e.CreateBasicProperties()).Returns(new BasicProperties()); IBasicConsumer consumer = null; modelMock.Setup(e => e.BasicConsume("test.queue", false, It.IsAny <string>(), false, false, null, It.IsAny <EventingBasicConsumer>())) .Callback <string, bool, string, bool, bool, IDictionary <string, object>, IBasicConsumer>((a, b, c, d, e, f, receivedConsumer) => consumer = receivedConsumer); var sender = new RabbitMqCommandSender(contextMock.Object); Guid guid = Guid.NewGuid(); var command = new CommandMessage { DestinationQueue = "test.queue", CorrelationId = guid, ReplyQueue = "reply.queue" }; BasicProperties testBasicProperties = new BasicProperties { CorrelationId = guid.ToString() }; var body = new CommandError { EventType = "CommandError", Exception = new Exception(exceptionMessage), CorrelationId = guid }; var bodyJson = JsonConvert.SerializeObject(body); // Ensure that response arrives 3 seconds later Task.Run(() => { Thread.Sleep(3000); consumer.HandleBasicDeliver("tag", 20, false, "", "", testBasicProperties, Encoding.Unicode.GetBytes(bodyJson)); }); // Act Task Act() => sender.SendCommandAsync(command); // Assert Task <DestinationQueueException> exception = Assert.ThrowsExceptionAsync <DestinationQueueException>(Act); Assert.AreEqual("Received error command from queue test.queue", exception.Result.Message); }