public void StartListening_Does_Not_Throw_Exception_If_Queue_Not_Yet_Created() { 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 missingQueueName = "missingqueue"; var method = typeof(QueueListenerTestFunctions).GetMethods().FirstOrDefault(); mockTracer .Setup(t => t.Info(It.Is <string>(s => s == $"Start Listening on queue '{missingQueueName}'"))) .Verifiable(); mockTracer .Setup(t => t.Verbose(It.Is <string>(s => s == $"Queue '{missingQueueName}' does not exist."))) .Verifiable(); mockTracer .Setup(t => t.Info(It.Is <string>(s => s == $"Stop listening to queues"))) .Verifiable(); listener.StartListening(missingQueueName, method); // sleep for 1 second before poll interval Thread.Sleep((pollIntervalSeconds - 1) * 1000); listener.StopAll(); mockTracer.VerifyAll(); mockInvoker .Verify(t => t.InvokeAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <MethodInfo>()), Times.Never()); }
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()); }
public void StartListening_Throws_Exception_If_MethodInfo_is_null() { 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(); var ex = Assert.Throws <ArgumentNullException>(() => listener.StartListening("test", null)); Assert.Equal("queueTriggerMethod", ex.ParamName); mockInvoker .Verify(t => t.InvokeAsync(It.IsAny <CloudQueueMessage>(), It.IsAny <MethodInfo>()), Times.Never()); }
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(); }