-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from Project-MONAI/ac-1619-nds
Ac 1619 nds
- Loading branch information
Showing
2 changed files
with
249 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
using Monai.Deploy.Messaging.Messages; | ||
using Moq; | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Exceptions; | ||
using Xunit; | ||
|
||
namespace Monai.Deploy.Messaging.RabbitMQ.Tests.Unit | ||
|
@@ -165,6 +166,215 @@ await Task.Run(() => | |
Assert.Equal(message.Body, s_messageReceived.Body); | ||
} | ||
|
||
[Fact(DisplayName = "Subscribes to a topic and previously created dead-letter queue is down")] | ||
public void SubscribesToATopicAndDeadLetterQueueIsDown() | ||
{ | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.EndPoint, "endpoint"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.Username, "username"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.Password, "password"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.VirtualHost, "virtual-host"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.Exchange, "exchange"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.DeadLetterExchange, "exchange-dead-letter"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.DeliveryLimit, "3"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.RequeueDelay, "30"); | ||
|
||
var jsonMessage = new JsonMessage<string>("hello world", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "1"); | ||
var message = jsonMessage.ToMessage(); | ||
|
||
var creationTime = DateTimeOffset.FromUnixTimeSeconds(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()); | ||
|
||
var basicProperties = new Mock<IBasicProperties>(); | ||
basicProperties.SetupGet(p => p.MessageId).Returns(jsonMessage.MessageId); | ||
basicProperties.SetupGet(p => p.AppId).Returns(jsonMessage.ApplicationId); | ||
basicProperties.SetupGet(p => p.ContentType).Returns(jsonMessage.ContentType); | ||
basicProperties.SetupGet(p => p.CorrelationId).Returns(jsonMessage.CorrelationId); | ||
basicProperties.SetupGet(p => p.Headers["CreationDateTime"]).Returns(Encoding.UTF8.GetBytes(creationTime.ToString("o", CultureInfo.InvariantCulture))); | ||
|
||
basicProperties.SetupGet(p => p.Type).Returns("topic"); | ||
basicProperties.SetupGet(p => p.Timestamp).Returns(new AmqpTimestamp(creationTime.ToUnixTimeSeconds())); | ||
|
||
_model.Setup(p => p.QueueDeclare( | ||
"queue", | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<IDictionary<string, object>>())) | ||
.Returns(new QueueDeclareOk("queue-name", 1, 1)); | ||
|
||
var exception = new OperationInterruptedException(new ShutdownEventArgs(ShutdownInitiator.Peer, 404, | ||
"NOT_FOUND - home node '[email protected]' of durable queue 'md.workflow.request-dead-letter' in vhost 'monaideploy' is down or inaccessible")); | ||
|
||
_model.Setup(p => p.QueueDeclare( | ||
"queue-dead-letter", | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<IDictionary<string, object>>())) | ||
.Throws(exception); | ||
|
||
_model.Setup(p => p.QueueBind( | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<IDictionary<string, object>>())); | ||
_model.Setup(p => p.BasicQos( | ||
It.IsAny<uint>(), | ||
It.IsAny<ushort>(), | ||
It.IsAny<bool>())); | ||
_model.Setup(p => p.BasicConsume( | ||
It.IsAny<string>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<string>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<IDictionary<string, object>>(), | ||
It.IsAny<IBasicConsumer>())) | ||
.Callback<string, bool, string, bool, bool, IDictionary<string, object>, IBasicConsumer>( | ||
(queue, autoAck, tag, noLocal, exclusive, args, consumer) => | ||
{ | ||
consumer.HandleBasicDeliver(tag, Convert.ToUInt64(jsonMessage.DeliveryTag, CultureInfo.InvariantCulture), false, "exchange", "topic", basicProperties.Object, new ReadOnlyMemory<byte>(message.Body)); | ||
}); | ||
|
||
var service = new RabbitMQMessageSubscriberService(_options, _logger.Object, _connectionFactory.Object); | ||
|
||
service.Subscribe("topic", "queue", (args) => | ||
{ | ||
Assert.Equal(message.ApplicationId, args.Message.ApplicationId); | ||
Assert.Equal(message.ContentType, args.Message.ContentType); | ||
Assert.Equal(message.MessageId, args.Message.MessageId); | ||
Assert.Equal(creationTime.ToUniversalTime(), args.Message.CreationDateTime.ToUniversalTime()); | ||
Assert.Equal(message.DeliveryTag, args.Message.DeliveryTag); | ||
Assert.Equal("topic", args.Message.MessageDescription); | ||
Assert.Equal(message.MessageId, args.Message.MessageId); | ||
Assert.Equal(message.Body, args.Message.Body); | ||
}); | ||
|
||
service.SubscribeAsync("topic", "queue", async (args) => | ||
{ | ||
await Task.Run(() => | ||
{ | ||
s_messageReceived = args.Message; | ||
service.Acknowledge(args.Message); | ||
}).ConfigureAwait(false); | ||
}); | ||
|
||
// wait for it to pick up meassage | ||
Task.Delay(500).GetAwaiter().GetResult(); | ||
|
||
Assert.NotNull(s_messageReceived); | ||
Assert.Equal(message.ApplicationId, s_messageReceived.ApplicationId); | ||
Assert.Equal(message.ContentType, s_messageReceived.ContentType); | ||
Assert.Equal(message.MessageId, s_messageReceived.MessageId); | ||
Assert.Equal(creationTime.ToUniversalTime(), s_messageReceived.CreationDateTime.ToUniversalTime()); | ||
Assert.Equal(message.DeliveryTag, s_messageReceived.DeliveryTag); | ||
Assert.Equal("topic", s_messageReceived.MessageDescription); | ||
Assert.Equal(message.MessageId, s_messageReceived.MessageId); | ||
Assert.Equal(message.Body, s_messageReceived.Body); | ||
} | ||
|
||
[Fact(DisplayName = "Subscribes to a topic and dead-letter queue subscription fails with generic exception")] | ||
public void SubscribesToATopicAndDeadLetterQueueSubscriptionFailsWithGenericException() | ||
{ | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.EndPoint, "endpoint"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.Username, "username"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.Password, "password"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.VirtualHost, "virtual-host"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.Exchange, "exchange"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.DeadLetterExchange, "exchange-dead-letter"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.DeliveryLimit, "3"); | ||
_options.Value.SubscriberSettings.Add(ConfigurationKeys.RequeueDelay, "30"); | ||
|
||
var jsonMessage = new JsonMessage<string>("hello world", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "1"); | ||
var message = jsonMessage.ToMessage(); | ||
|
||
var creationTime = DateTimeOffset.FromUnixTimeSeconds(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()); | ||
|
||
var basicProperties = new Mock<IBasicProperties>(); | ||
basicProperties.SetupGet(p => p.MessageId).Returns(jsonMessage.MessageId); | ||
basicProperties.SetupGet(p => p.AppId).Returns(jsonMessage.ApplicationId); | ||
basicProperties.SetupGet(p => p.ContentType).Returns(jsonMessage.ContentType); | ||
basicProperties.SetupGet(p => p.CorrelationId).Returns(jsonMessage.CorrelationId); | ||
basicProperties.SetupGet(p => p.Headers["CreationDateTime"]).Returns(Encoding.UTF8.GetBytes(creationTime.ToString("o", CultureInfo.InvariantCulture))); | ||
|
||
basicProperties.SetupGet(p => p.Type).Returns("topic"); | ||
basicProperties.SetupGet(p => p.Timestamp).Returns(new AmqpTimestamp(creationTime.ToUnixTimeSeconds())); | ||
|
||
_model.Setup(p => p.QueueDeclare( | ||
"queue", | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<IDictionary<string, object>>())) | ||
.Returns(new QueueDeclareOk("queue-name", 1, 1)); | ||
|
||
var exception = new OperationInterruptedException(new ShutdownEventArgs(ShutdownInitiator.Application, 500, | ||
"Something else went wrong")); | ||
|
||
_model.Setup(p => p.QueueDeclare( | ||
"queue-dead-letter", | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<IDictionary<string, object>>())) | ||
.Throws(exception); | ||
|
||
_model.Setup(p => p.QueueBind( | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<IDictionary<string, object>>())); | ||
_model.Setup(p => p.BasicQos( | ||
It.IsAny<uint>(), | ||
It.IsAny<ushort>(), | ||
It.IsAny<bool>())); | ||
_model.Setup(p => p.BasicConsume( | ||
It.IsAny<string>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<string>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<bool>(), | ||
It.IsAny<IDictionary<string, object>>(), | ||
It.IsAny<IBasicConsumer>())) | ||
.Callback<string, bool, string, bool, bool, IDictionary<string, object>, IBasicConsumer>( | ||
(queue, autoAck, tag, noLocal, exclusive, args, consumer) => | ||
{ | ||
consumer.HandleBasicDeliver(tag, Convert.ToUInt64(jsonMessage.DeliveryTag, CultureInfo.InvariantCulture), false, "exchange", "topic", basicProperties.Object, new ReadOnlyMemory<byte>(message.Body)); | ||
}); | ||
|
||
var service = new RabbitMQMessageSubscriberService(_options, _logger.Object, _connectionFactory.Object); | ||
|
||
var act = () => | ||
{ | ||
service.Subscribe("topic", "queue", (args) => | ||
{ | ||
Assert.Equal(message.ApplicationId, args.Message.ApplicationId); | ||
Assert.Equal(message.ContentType, args.Message.ContentType); | ||
Assert.Equal(message.MessageId, args.Message.MessageId); | ||
Assert.Equal(creationTime.ToUniversalTime(), args.Message.CreationDateTime.ToUniversalTime()); | ||
Assert.Equal(message.DeliveryTag, args.Message.DeliveryTag); | ||
Assert.Equal("topic", args.Message.MessageDescription); | ||
Assert.Equal(message.MessageId, args.Message.MessageId); | ||
Assert.Equal(message.Body, args.Message.Body); | ||
|
||
}); | ||
}; | ||
Assert.Throws<OperationInterruptedException>(act); | ||
|
||
var asyncAct = () => | ||
{ | ||
service.SubscribeAsync("topic", "queue", async (args) => | ||
{ | ||
await Task.Run(() => | ||
{ | ||
s_messageReceived = args.Message; | ||
service.Acknowledge(args.Message); | ||
}).ConfigureAwait(false); | ||
|
||
}); | ||
}; | ||
Assert.Throws<OperationInterruptedException>(asyncAct); | ||
} | ||
|
||
[Fact(DisplayName = "Acknowledge a message")] | ||
public void AcknowledgeAMessage() | ||
{ | ||
|