Ejemplo n.º 1
0
        public void TestFails()
        {
            foreach (var method in this.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
            {
                Mock <IExtensionRegistry> extensionsMock = new Mock <IExtensionRegistry>(MockBehavior.Strict);
                extensionsMock.Setup(p => p.GetExtensions(typeof(IExtensionConfigProvider))).Returns(Enumerable.Empty <object>());
                extensionsMock.Setup(p => p.GetExtensions(typeof(ITriggerBindingProvider))).Returns(Enumerable.Empty <object>());
                extensionsMock.Setup(p => p.GetExtensions(typeof(IBindingProvider))).Returns(Enumerable.Empty <object>());
                extensionsMock.Setup(p => p.GetExtensions(typeof(IArgumentBindingProvider <>))).Returns(Enumerable.Empty <object>());
                Mock <IFunctionExecutor> executorMock = new Mock <IFunctionExecutor>(MockBehavior.Strict);

                IFunctionIndexCollector stubIndex = new Mock <IFunctionIndexCollector>().Object;

                FunctionIndexer indexer = new FunctionIndexer(
                    new Mock <ITriggerBindingProvider>(MockBehavior.Strict).Object,
                    new Mock <IBindingProvider>(MockBehavior.Strict).Object,
                    new Mock <IJobActivator>(MockBehavior.Strict).Object,
                    executorMock.Object,
                    extensionsMock.Object,
                    new SingletonManager(),
                    new TestTraceWriter(TraceLevel.Verbose),
                    null);

                Assert.Throws <FunctionIndexingException>(() => indexer.IndexMethodAsync(method, stubIndex, CancellationToken.None).GetAwaiter().GetResult());
            }
        }
Ejemplo n.º 2
0
        public async Task IndexMethod_IfMethodReturnsAsyncVoid_Throws()
        {
            var traceWriter    = new TestTraceWriter(TraceLevel.Verbose);
            var loggerFactory  = new LoggerFactory();
            var loggerProvider = new TestLoggerProvider();

            loggerFactory.AddProvider(loggerProvider);

            // Arrange
            IFunctionIndexCollector index   = CreateStubFunctionIndex();
            FunctionIndexer         product = CreateProductUnderTest(traceWriter: traceWriter, loggerFactory: loggerFactory);

            // Act & Assert
            await product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod("ReturnAsyncVoid"), index, CancellationToken.None);

            string expectedMessage = "Function 'ReturnAsyncVoid' is async but does not return a Task. Your function may not run correctly.";

            // Validate TraceWriter
            var traceWarning = traceWriter.Traces.First(p => p.Level == TraceLevel.Warning);

            Assert.Equal(expectedMessage, traceWarning.Message);

            // Validate Logger
            var logger        = loggerProvider.CreatedLoggers.Single(l => l.Category == Logging.LogCategories.Startup);
            var loggerWarning = logger.LogMessages.Single();

            Assert.Equal(LogLevel.Warning, loggerWarning.Level);
            Assert.Equal(expectedMessage, loggerWarning.FormattedMessage);
        }
        // Helper to do the indexing.
        private static Tuple <FunctionDescriptor, IFunctionDefinition> IndexMethod(string methodName, INameResolver nameResolver = null)
        {
            MethodInfo method = typeof(FunctionIndexerIntegrationTests).GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            Assert.NotNull(method);

            FunctionIndexer indexer = FunctionIndexerFactory.Create(CloudStorageAccount.DevelopmentStorageAccount,
                                                                    nameResolver);

            Tuple <FunctionDescriptor, IFunctionDefinition> indexEntry = null;
            Mock <IFunctionIndexCollector> indexMock = new Mock <IFunctionIndexCollector>(MockBehavior.Strict);

            indexMock
            .Setup((i) => i.Add(
                       It.IsAny <IFunctionDefinition>(),
                       It.IsAny <FunctionDescriptor>(),
                       It.IsAny <MethodInfo>()))
            .Callback <IFunctionDefinition, FunctionDescriptor, MethodInfo>(
                (ifd, fd, i) => indexEntry = Tuple.Create(fd, ifd));
            IFunctionIndexCollector index = indexMock.Object;

            indexer.IndexMethodAsync(method, index, CancellationToken.None).GetAwaiter().GetResult();

            return(indexEntry);
        }
Ejemplo n.º 4
0
        public void IndexMethod_IfMethodReturnsVoid_DoesNotThrow()
        {
            // Arrange
            IFunctionIndexCollector index   = CreateStubFunctionIndex();
            FunctionIndexer         product = CreateProductUnderTest();

            // Act & Assert
            product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod("ReturnVoid"),
                                     index, CancellationToken.None).GetAwaiter().GetResult();
        }
Ejemplo n.º 5
0
        public void IndexMethod_IgnoresMethod_IfNonJobMethod(string method)
        {
            // Arrange
            Mock <IFunctionIndexCollector> indexMock = new Mock <IFunctionIndexCollector>();
            FunctionIndexer product = CreateProductUnderTest();

            // Act
            product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod(method), indexMock.Object, CancellationToken.None).GetAwaiter().GetResult();

            // Verify
            indexMock.Verify(i => i.Add(It.IsAny <IFunctionDefinition>(), It.IsAny <FunctionDescriptor>(), It.IsAny <MethodInfo>()), Times.Never);
        }
Ejemplo n.º 6
0
        public void GetFunctionTimeout_ReturnsExpected()
        {
            // Arrange
            var             collector = new TestIndexCollector();
            FunctionIndexer product   = CreateProductUnderTest();

            // Act & Assert
            product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod("Timeout_Set"),
                                     collector, CancellationToken.None).GetAwaiter().GetResult();

            Assert.Equal(TimeSpan.FromMinutes(30), collector.Functions.First().TimeoutAttribute.Timeout);
        }
Ejemplo n.º 7
0
        public void GetFunctionTraceLevel_ReturnsExpectedLevel(string method, TraceLevel level)
        {
            // Arrange
            var             collector = new TestIndexCollector();
            FunctionIndexer product   = CreateProductUnderTest();

            // Act & Assert
            product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod(method),
                                     collector, CancellationToken.None).GetAwaiter().GetResult();

            Assert.Equal(level, collector.Functions.First().TraceLevel);
        }
 public void TestFails()
 {
     foreach (var method in this.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
     {
         IFunctionIndexCollector stubIndex = new Mock <IFunctionIndexCollector>().Object;
         FunctionIndexer         indexer   = new FunctionIndexer(
             new Mock <ITriggerBindingProvider>(MockBehavior.Strict).Object,
             new Mock <IBindingProvider>(MockBehavior.Strict).Object,
             new Mock <IJobActivator>(MockBehavior.Strict).Object);
         Assert.Throws <FunctionIndexingException>(() => indexer.IndexMethodAsync(method, stubIndex, CancellationToken.None).GetAwaiter().GetResult());
     }
 }
Ejemplo n.º 9
0
        public void IndexMethod_IfMethodReturnsTaskOfTResult_Throws()
        {
            // Arrange
            IFunctionIndexCollector index   = CreateDummyFunctionIndex();
            FunctionIndexer         product = CreateProductUnderTest();

            // Act & Assert
            FunctionIndexingException exception = Assert.Throws <FunctionIndexingException>(
                () => product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod("ReturnGenericTask"), index,
                                               CancellationToken.None).GetAwaiter().GetResult());
            InvalidOperationException innerException = exception.InnerException as InvalidOperationException;

            Assert.NotNull(innerException);
            Assert.Equal("Functions must return Task or void.", innerException.Message);
        }
Ejemplo n.º 10
0
        public async Task IndexMethod_IfMethodReturnsAsyncVoid_Throws()
        {
            var traceWriter = new TestTraceWriter(TraceLevel.Verbose);

            // Arrange
            IFunctionIndexCollector index   = CreateStubFunctionIndex();
            FunctionIndexer         product = CreateProductUnderTest(traceWriter: traceWriter);

            // Act & Assert
            await product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod("ReturnAsyncVoid"), index, CancellationToken.None);

            var warning = traceWriter.Traces.First(p => p.Level == TraceLevel.Warning);

            Assert.Equal("Function 'ReturnAsyncVoid' is async but does not return a Task. Your function may not run correctly.", warning.Message);
        }
        public void TestFails()
        {
            foreach (var method in this.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
            {
                Mock<IExtensionRegistry> extensionsMock = new Mock<IExtensionRegistry>(MockBehavior.Strict);
                extensionsMock.Setup(p => p.GetExtensions(typeof(ITriggerBindingProvider))).Returns(Enumerable.Empty<object>());
                extensionsMock.Setup(p => p.GetExtensions(typeof(IBindingProvider))).Returns(Enumerable.Empty<object>());
                Mock<IFunctionExecutor> executorMock = new Mock<IFunctionExecutor>(MockBehavior.Strict);

                IFunctionIndexCollector stubIndex = new Mock<IFunctionIndexCollector>().Object;
                FunctionIndexer indexer = new FunctionIndexer(
                    new Mock<ITriggerBindingProvider>(MockBehavior.Strict).Object,
                    new Mock<IBindingProvider>(MockBehavior.Strict).Object,
                    new Mock<IJobActivator>(MockBehavior.Strict).Object,
                    executorMock.Object,
                    extensionsMock.Object);

                Assert.Throws<FunctionIndexingException>(() => indexer.IndexMethodAsync(method, stubIndex, CancellationToken.None).GetAwaiter().GetResult());
            }
        }
        public void IndexMethod_Throws_IfMethodHasUnboundOutParameterWithJobsAttribute()
        {
            // Arrange
            Mock <IFunctionIndexCollector> indexMock = new Mock <IFunctionIndexCollector>(MockBehavior.Strict);
            int calls = 0;

            indexMock
            .Setup(i => i.Add(It.IsAny <IFunctionDefinition>(), It.IsAny <FunctionDescriptor>(), It.IsAny <MethodInfo>()))
            .Callback(() => calls++);
            IFunctionIndexCollector index   = indexMock.Object;
            FunctionIndexer         product = CreateProductUnderTest();

            // Act & Assert
            FunctionIndexingException exception = Assert.Throws <FunctionIndexingException>(
                () => product.IndexMethodAsync(typeof(FunctionIndexerTests).GetMethod("FailIndexing"), index,
                                               CancellationToken.None).GetAwaiter().GetResult());
            InvalidOperationException innerException = exception.InnerException as InvalidOperationException;

            Assert.NotNull(innerException);
            Assert.Equal($"Cannot bind parameter 'parsed' to type Foo&. Make sure the parameter Type is supported by the binding. {Resource.ExtensionInitializationMessage}", innerException.Message);
        }