示例#1
0
        private void CheckAckNackCounts(DicomTagReaderConsumer consumer, int desiredAckCount, int desiredNackCount)
        {
            var mockdeliverArgs = Mock.Of <BasicDeliverEventArgs>();

            mockdeliverArgs.DeliveryTag     = 1;
            mockdeliverArgs.Body            = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(_helper.TestAccessionDirectoryMessage));
            mockdeliverArgs.BasicProperties = new BasicProperties {
                Headers = new Dictionary <string, object>()
            };

            var header = new MessageHeader();

            header.Populate(mockdeliverArgs.BasicProperties.Headers);

            // Have to convert these to bytes since RabbitMQ normally does that when sending
            mockdeliverArgs.BasicProperties.Headers["MessageGuid"]            = Encoding.UTF8.GetBytes(header.MessageGuid.ToString());
            mockdeliverArgs.BasicProperties.Headers["ProducerExecutableName"] = Encoding.UTF8.GetBytes(header.ProducerExecutableName);
            mockdeliverArgs.BasicProperties.Headers["Parents"] = Encoding.UTF8.GetBytes(string.Join("->", header.Parents));

            var fatalCalled = false;

            consumer.OnFatal += (sender, args) => fatalCalled = true;

            consumer.SetModel(_mockModel);
            consumer.ProcessMessage(mockdeliverArgs);

            Assert.AreEqual(desiredAckCount, consumer.AckCount);
            Assert.AreEqual(desiredNackCount, consumer.NackCount);
            Assert.False(fatalCalled);
        }
示例#2
0
        public void Consumer_UnhandledException_TriggersFatal()
        {
            var mockDeliverArgs = Mock.Of <BasicDeliverEventArgs>();

            mockDeliverArgs.DeliveryTag     = 1;
            mockDeliverArgs.BasicProperties = new BasicProperties {
                Headers = new Dictionary <string, object>()
            };
            var header = new MessageHeader();

            header.Populate(mockDeliverArgs.BasicProperties.Headers);
            mockDeliverArgs.BasicProperties.Headers["MessageGuid"]            = Encoding.UTF8.GetBytes(header.MessageGuid.ToString());
            mockDeliverArgs.BasicProperties.Headers["ProducerExecutableName"] = Encoding.UTF8.GetBytes(header.ProducerExecutableName);
            mockDeliverArgs.BasicProperties.Headers["Parents"] = Encoding.UTF8.GetBytes(string.Join("->", header.Parents));

            var consumer = new TestConsumer();

            consumer.SetModel(Mock.Of <IModel>());

            var fatalCalled = false;

            consumer.OnFatal += (sender, args) => fatalCalled = true;

            consumer.ProcessMessage(mockDeliverArgs);

            Thread.Sleep(1000);
            Assert.True(fatalCalled);
        }
示例#3
0
        public void TestInvalidMessagesNack()
        {
            //TODO: Refactor this to a helper function in Smi.Common.Tests
            var mockDeliverArgs = Mock.Of <BasicDeliverEventArgs>();

            mockDeliverArgs.DeliveryTag     = 1;
            mockDeliverArgs.BasicProperties = new BasicProperties {
                Headers = new Dictionary <string, object>()
            };
            var header = new MessageHeader();

            header.Populate(mockDeliverArgs.BasicProperties.Headers);
            mockDeliverArgs.BasicProperties.Headers["MessageGuid"]            = Encoding.UTF8.GetBytes(header.MessageGuid.ToString());
            mockDeliverArgs.BasicProperties.Headers["ProducerExecutableName"] = Encoding.UTF8.GetBytes(header.ProducerExecutableName);
            mockDeliverArgs.BasicProperties.Headers["Parents"] = Encoding.UTF8.GetBytes(string.Join("->", header.Parents));

            var consumer = new MongoDbPopulatorMessageConsumer <DicomFileMessage>(_helper.Globals.MongoDatabases.DicomStoreOptions, _helper.Globals.MongoDbPopulatorOptions, _helper.Globals.MongoDbPopulatorOptions.ImageQueueConsumerOptions);

            var nackCount = 0;
            var mockModel = new Mock <IModel>();

            mockModel.Setup(x => x.BasicNack(It.IsAny <ulong>(), It.IsAny <bool>(), It.IsAny <bool>())).Callback(() => ++ nackCount);
            consumer.SetModel(mockModel.Object);

            mockDeliverArgs.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(null));
            consumer.ProcessMessage(mockDeliverArgs);
            Assert.AreEqual(1, nackCount);
        }
示例#4
0
        public IBasicProperties GetBasicProperties()
        {
            var props = new BasicProperties
            {
                ContentEncoding = ContentEncoding,
                ContentType     = ContentType,
                Timestamp       = new AmqpTimestamp(UnixTimestamp),
                Headers         = new Dictionary <string, object>()
            };

            MessageHeader.Populate(props.Headers);
            XDeathHeaders.Populate(props.Headers);

            return(props);
        }
示例#5
0
        private static BasicDeliverEventArgs GetMockDeliverArgs(ExtractFileMessage message)
        {
            var mockDeliverArgs = Mock.Of <BasicDeliverEventArgs>(MockBehavior.Strict);

            mockDeliverArgs.DeliveryTag     = 1;
            mockDeliverArgs.Body            = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
            mockDeliverArgs.BasicProperties = new BasicProperties {
                Headers = new Dictionary <string, object>()
            };
            var header = new MessageHeader();

            header.Populate(mockDeliverArgs.BasicProperties.Headers);
            // Have to convert these to bytes since RabbitMQ normally does that when sending
            mockDeliverArgs.BasicProperties.Headers["MessageGuid"]            = Encoding.UTF8.GetBytes(header.MessageGuid.ToString());
            mockDeliverArgs.BasicProperties.Headers["ProducerExecutableName"] = Encoding.UTF8.GetBytes(header.ProducerExecutableName);
            mockDeliverArgs.BasicProperties.Headers["Parents"] = Encoding.UTF8.GetBytes(string.Join("->", header.Parents));
            return(mockDeliverArgs);
        }
示例#6
0
        /// <summary>
        /// Checks that ExtractionRequestQueueConsumer publishes messages correctly according to the input message isIdentifiableExtraction value
        /// </summary>
        /// <param name="globals"></param>
        /// <param name="isIdentifiableExtraction"></param>
        /// <param name="expectedRoutingKey"></param>
        private static void AssertMessagePublishedWithSpecifiedKey(GlobalOptions globals, bool isIdentifiableExtraction, string expectedRoutingKey)
        {
            var fakeFulfiller = new FakeFulfiller();

            var    mockFileMessageProducerModel = new Mock <IProducerModel>(MockBehavior.Strict);
            string fileMessageRoutingKey        = null;

            mockFileMessageProducerModel
            .Setup(x => x.SendMessage(It.IsAny <IMessage>(), It.IsAny <IMessageHeader>(), It.IsNotNull <string>()))
            .Callback((IMessage _, IMessageHeader __, string routingKey) => { fileMessageRoutingKey = routingKey; })
            .Returns(new MessageHeader());
            mockFileMessageProducerModel.Setup(x => x.WaitForConfirms());

            var mockFileInfoMessageProducerModel = new Mock <IProducerModel>(MockBehavior.Strict);

            mockFileInfoMessageProducerModel
            .Setup(x => x.SendMessage(It.IsAny <IMessage>(), It.IsAny <IMessageHeader>(), It.IsNotNull <string>()))
            .Returns(new MessageHeader());
            mockFileInfoMessageProducerModel.Setup(x => x.WaitForConfirms());

            var msg = new ExtractionRequestMessage
            {
                JobSubmittedAt           = DateTime.UtcNow,
                ExtractionJobIdentifier  = Guid.NewGuid(),
                ProjectNumber            = "1234",
                ExtractionDirectory      = "1234/foo",
                IsIdentifiableExtraction = isIdentifiableExtraction,
                KeyTag = "foo",
                ExtractionIdentifiers = new List <string> {
                    "foo"
                },
                Modality = null,
            };
            var mockDeliverArgs = Mock.Of <BasicDeliverEventArgs>(MockBehavior.Strict);

            mockDeliverArgs.DeliveryTag     = 1;
            mockDeliverArgs.Body            = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));
            mockDeliverArgs.BasicProperties = new BasicProperties {
                Headers = new Dictionary <string, object>()
            };
            var header = new MessageHeader();

            header.Populate(mockDeliverArgs.BasicProperties.Headers);
            // Have to convert these to bytes since RabbitMQ normally does that when sending
            mockDeliverArgs.BasicProperties.Headers["MessageGuid"]            = Encoding.UTF8.GetBytes(header.MessageGuid.ToString());
            mockDeliverArgs.BasicProperties.Headers["ProducerExecutableName"] = Encoding.UTF8.GetBytes(header.ProducerExecutableName);
            mockDeliverArgs.BasicProperties.Headers["Parents"] = Encoding.UTF8.GetBytes(string.Join("->", header.Parents));

            var consumer = new ExtractionRequestQueueConsumer(
                globals.CohortExtractorOptions,
                fakeFulfiller,
                new NullAuditExtractions(), new DefaultProjectPathResolver(),
                mockFileMessageProducerModel.Object,
                mockFileInfoMessageProducerModel.Object);

            var fatalCalled = false;
            FatalErrorEventArgs fatalErrorEventArgs = null;

            consumer.OnFatal += (sender, args) =>
            {
                fatalCalled         = true;
                fatalErrorEventArgs = args;
            };

            var mockModel = new Mock <IModel>(MockBehavior.Strict);

            mockModel.Setup(x => x.IsClosed).Returns(false);
            mockModel.Setup(x => x.BasicAck(It.IsAny <ulong>(), It.IsAny <bool>())).Verifiable();

            consumer.SetModel(mockModel.Object);
            consumer.ProcessMessage(mockDeliverArgs);

            Thread.Sleep(500); // Fatal call is race-y
            Assert.False(fatalCalled, $"Fatal was called with {fatalErrorEventArgs}");
            mockModel.Verify(x => x.BasicAck(It.IsAny <ulong>(), It.IsAny <bool>()), Times.Once);
            Assert.AreEqual(expectedRoutingKey, fileMessageRoutingKey);
        }