Example #1
0
        public async Task StartListening_Inovkes_Queue_Trigger_Method_For_All_Items_In_Queue_Batch()
        {
            var mockTracer  = new Mock <ITraceWriter>(MockBehavior.Strict);
            var mockInvoker = new Mock <IQueueTriggerMethodInvoker>(MockBehavior.Strict);

            var pollIntervalSeconds = 5;

            var listener = new LocalQueueListener(mockTracer.Object, mockInvoker.Object, pollIntervalSeconds);

            var method = typeof(QueueListenerTestFunctions).GetMethods().FirstOrDefault();

            // Add test message to the queue
            var queueData1 = new TestQueueParameterType {
                TestProperty = "Test Data 1"
            };
            var queueData2 = new TestQueueParameterType {
                TestProperty = "Test Data 2"
            };
            var queueData3 = new TestQueueParameterType {
                TestProperty = "Test Data 3"
            };

            await QueueUtilities.DeleteQueue(testQueueName);

            await QueueUtilities.QueueMessageDataAsync(testQueueName, queueData1);

            await QueueUtilities.QueueMessageDataAsync(testQueueName, queueData2);

            await QueueUtilities.QueueMessageDataAsync(testQueueName, queueData3);

            mockTracer.Setup(t => t.Info(It.Is <string>(s => s == $"Start Listening on queue '{testQueueName}'"))).Verifiable();

            mockTracer.Setup(t => t.Info(It.Is <string>(s => s == $"Processing new message from '{testQueueName}'"))).Verifiable();

            mockInvoker.Setup(i => i.InvokeAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <MethodInfo>()))
            .Returns(Task.FromResult(0))
            .Verifiable();

            mockTracer.Setup(t => t.Info(It.Is <string>(s => s == $"Stop listening to queues"))).Verifiable();

            listener.StartListening(testQueueName, method);

            var cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.Token.WaitHandle.WaitOne(new TimeSpan(0, 0, (pollIntervalSeconds - 1)));

            listener.StopAll();

            mockTracer.VerifyAll();
            mockTracer.Verify(t => t.Info(It.Is <string>(s => s == $"Processing new message from '{testQueueName}'")), Times.Exactly(3));

            mockInvoker.Verify(i => i.InvokeAsync(It.Is <CloudQueueMessage>(m => VerifyCloudQueueMessasge(m, queueData1)), It.Is <MethodInfo>(m => m == method)), Times.Once());
            mockInvoker.Verify(i => i.InvokeAsync(It.Is <CloudQueueMessage>(m => VerifyCloudQueueMessasge(m, queueData2)), It.Is <MethodInfo>(m => m == method)), Times.Once());
            mockInvoker.Verify(i => i.InvokeAsync(It.Is <CloudQueueMessage>(m => VerifyCloudQueueMessasge(m, queueData3)), It.Is <MethodInfo>(m => m == method)), Times.Once());
        }
Example #2
0
        public async Task StartListening_Deletes_Queue_Message_After_Processing()
        {
            var mockTracer          = new Mock <ITraceWriter>(MockBehavior.Strict);
            var mockInvoker         = new Mock <IQueueTriggerMethodInvoker>(MockBehavior.Strict);
            var pollIntervalSeconds = 5;

            var listener = new LocalQueueListener(mockTracer.Object, mockInvoker.Object, pollIntervalSeconds, messageVisibilityTimeoutSeconds: 1);

            var method = typeof(QueueListenerTestFunctions).GetMethods().FirstOrDefault();

            // Add test message to the queue
            var queueData = new TestQueueParameterType
            {
                TestProperty = "This is a test object to be queued"
            };
            await QueueUtilities.DeleteQueue(testQueueName);

            await QueueUtilities.QueueMessageDataAsync(testQueueName, queueData);

            mockTracer
            .Setup(t => t.Info(It.IsAny <string>()))
            .Verifiable();

            mockInvoker
            .Setup(i => i.InvokeAsync(
                       It.Is <CloudQueueMessage>(m => VerifyCloudQueueMessasge(m, queueData)),
                       It.Is <MethodInfo>(m => m == method)))
            .Returns(Task.FromResult(1))
            .Verifiable();


            listener.StartListening(testQueueName, method);

            var cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.Token.WaitHandle.WaitOne(new TimeSpan(0, 0, (pollIntervalSeconds - 1)));

            listener.StopAll();

            var nextMessage = await QueueUtilities.GetNextMessageFromQueue(testQueueName);

            Assert.Null(nextMessage);

            mockTracer.VerifyAll();
            mockInvoker.VerifyAll();
        }
Example #3
0
        public async Task QueueTriggerMethodInvoker_InvokeAsync_Invokes_Non_Async_Method_Correctly()
        {
            var nonAsyncMethod = typeof(TestFunctions).GetMethods().First(m => m.Name == nameof(TestFunctions.QueueTriggerFunction1));
            var param          = "Test Parameter";
            var queueMessage   = QueueUtilities.CreateQueueMessage(param);

            var mockTracer = new Mock <ITraceWriter>(MockBehavior.Strict);

            TestFunctions.TraceWriter = mockTracer.Object;

            mockTracer.Setup(t => t.Info(It.Is <string>(m => m == $"QueueTriggerFunction1 called with parameter {param}")));

            var invoker = new QueueTriggerMethodInvoker();

            await invoker.InvokeAsync(queueMessage, nonAsyncMethod);

            mockTracer.VerifyAll();
        }
Example #4
0
        private bool VerifyCloudQueueMessasge(CloudQueueMessage message, TestQueueParameterType expectedMessageObject)
        {
            var actualMessageObject = QueueUtilities.GetObjectQueueMessage <TestQueueParameterType>(message);

            return(expectedMessageObject.TestProperty == actualMessageObject.TestProperty);
        }