public static FunctionIndexer Create(CloudStorageAccount account = null, INameResolver nameResolver = null, IExtensionRegistry extensionRegistry = null) { IStorageAccount storageAccount = account != null ? new StorageAccount(account) : null; IStorageAccountProvider storageAccountProvider = new SimpleStorageAccountProvider { StorageAccount = account }; IExtensionTypeLocator extensionTypeLocator = new NullExtensionTypeLocator(); ContextAccessor<IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor<IMessageEnqueuedWatcher>(); ContextAccessor<IBlobWrittenWatcher> blobWrittenWatcherAccessor = new ContextAccessor<IBlobWrittenWatcher>(); ISharedContextProvider sharedContextProvider = new SharedContextProvider(); ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, new FixedHostIdProvider("test"), new SimpleQueueConfiguration(maxDequeueCount: 5), BackgroundExceptionDispatcher.Instance, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, new DefaultExtensionRegistry(), TextWriter.Null); IBindingProvider bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, new DefaultExtensionRegistry()); IFunctionOutputLoggerProvider outputLoggerProvider = new NullFunctionOutputLoggerProvider(); var task = outputLoggerProvider.GetAsync(CancellationToken.None); task.Wait(); IFunctionOutputLogger outputLogger = task.Result; IFunctionExecutor executor = new FunctionExecutor(new NullFunctionInstanceLogger(), outputLogger, BackgroundExceptionDispatcher.Instance); if (extensionRegistry == null) { extensionRegistry = new DefaultExtensionRegistry(); } return new FunctionIndexer(triggerBindingProvider, bindingProvider, DefaultJobActivator.Instance, executor, extensionRegistry); }
public BasePointItem(PointType type, ushort address, FunctionExecutor commandExecutor, string name, IStateUpdater stateUpdater) { this.type = type; this.address = address; this.commandExecutor = commandExecutor; this.name = name; this.stateUpdater = stateUpdater; commandExecutor.UpdatePointEvent += CommandExecutor_UpdatePointEvent; WriteCommand = new RelayCommand(WriteCommand_Execute); ReadCommand = new RelayCommand(ReadCommand_Execute); }
public void TestExecuteCreateTempFile() { FunctionExecutor tester = (new FunctionTester <FileOperations>()).Compile( new PropertyValue(FileOperationsShared.ActionPropertyName, FileOperationsShared.ActionType.CreateTempFile)); FunctionResult result = tester.Execute( new ParameterValue(FileOperationsShared.SourceFilePathPropertyName, null), new ParameterValue(FileOperationsShared.DestinationFolderPathPropertyName, null), new ParameterValue(FileOperationsShared.DestinationFilePathPropertyName, null)); Assert.IsTrue(System.IO.File.Exists(result.Value), "Temp file not exists."); }
public void TestArgStringArrayReturnVoid() { FunctionExecutor.Run( (string[] args) => { Assert.Equal("arg1", args[0]); Assert.Equal("arg2", args[1]); }, new string[] { "arg1", "arg2" } ); }
public void StartFunctionTimeout_NoCancellationTokenParameter_ReturnsNull() { MethodInfo method = typeof(Functions).GetMethod("NoCancellationTokenParameter", BindingFlags.Static | BindingFlags.Public); _descriptor.Method = method; System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, _globalFunctionTimeout, _cancellationTokenSource, _traceWriter); Assert.Null(timer); _mockFunctionInstance.VerifyAll(); }
public void StartFunctionTimeout_NoTimeout_ReturnsNull() { MethodInfo method = GetType().GetMethod("GlobalLevel", BindingFlags.Static | BindingFlags.Public); _descriptor.Method = method; System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, null, _cancellationTokenSource, _traceWriter); Assert.Null(timer); _mockFunctionInstance.VerifyAll(); }
public static TestJobHost <TProgram> Create <TProgram>(CloudStorageAccount storageAccount, int maxDequeueCount) { IStorageAccountProvider storageAccountProvider = new SimpleStorageAccountProvider { StorageAccount = storageAccount, // use null logging string since unit tests don't need logs. DashboardAccount = null }; IExtensionTypeLocator extensionTypeLocator = new NullExtensionTypeLocator(); IHostIdProvider hostIdProvider = new FixedHostIdProvider("test"); INameResolver nameResolver = null; IQueueConfiguration queueConfiguration = new SimpleQueueConfiguration(maxDequeueCount); ContextAccessor <IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor <IMessageEnqueuedWatcher>(); ContextAccessor <IBlobWrittenWatcher> blobWrittenWatcherAccessor = new ContextAccessor <IBlobWrittenWatcher>(); ISharedContextProvider sharedContextProvider = new SharedContextProvider(); IExtensionRegistry extensions = new DefaultExtensionRegistry(); SingletonManager singletonManager = new SingletonManager(); IFunctionOutputLoggerProvider outputLoggerProvider = new NullFunctionOutputLoggerProvider(); var task = outputLoggerProvider.GetAsync(CancellationToken.None); task.Wait(); IFunctionOutputLogger outputLogger = task.Result; IFunctionExecutor executor = new FunctionExecutor(new NullFunctionInstanceLogger(), outputLogger, BackgroundExceptionDispatcher.Instance, new TestTraceWriter(TraceLevel.Verbose)); var triggerBindingProvider = DefaultTriggerBindingProvider.Create( nameResolver, storageAccountProvider, extensionTypeLocator, hostIdProvider, queueConfiguration, BackgroundExceptionDispatcher.Instance, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, extensions, new TestTraceWriter(TraceLevel.Verbose)); var bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, extensions); var functionIndexProvider = new FunctionIndexProvider(new FakeTypeLocator(typeof(TProgram)), triggerBindingProvider, bindingProvider, DefaultJobActivator.Instance, executor, new DefaultExtensionRegistry(), singletonManager); IJobHostContextFactory contextFactory = new TestJobHostContextFactory { FunctionIndexProvider = functionIndexProvider, StorageAccountProvider = storageAccountProvider, Queues = queueConfiguration, SingletonManager = singletonManager }; IServiceProvider serviceProvider = new TestJobHostConfiguration { ContextFactory = contextFactory }; return(new TestJobHost <TProgram>(serviceProvider)); }
private FunctionResult Execute(string filePath, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExists, TextCodepage codepage = TextCodepage.Default, bool isText = true) { FunctionExecutor tester = (new FunctionTester <FileOpen.FileOpen>()).Compile( new PropertyValue(FileOpenShared.IsTextPropertyName, isText), new PropertyValue(FileOpenShared.CodepagePropertyName, codepage), new PropertyValue(FileOpenShared.FileDoesNotExistPropertyName, fileDoesNotExist), new PropertyValue(FileOpenShared.FileExistsPropertyName, fileExists)); var result = tester.Execute(new ParameterValue(FileOpenShared.FilePathPropertyName, filePath)); Assert.IsFalse(FileHelpers.IsFileLocked(filePath), "File must not open before the execution path is active."); return(result); }
private FunctionResult Execute(string sourceFilePath, bool keepFileName, string destinationPath, FileOperationsShared.ActionType action, FileOperationsShared.ExistsOption fileExistsOption) { FunctionExecutor tester = (new FunctionTester <FileOperations>()).Compile( new PropertyValue(FileOperationsShared.ActionPropertyName, action), new PropertyValue(FileOperationsShared.KeepFileNamePropertyName, keepFileName), new PropertyValue(FileOperationsShared.FileExistsPropertyName, fileExistsOption)); return(tester.Execute( new ParameterValue(FileOperationsShared.SourceFilePathPropertyName, sourceFilePath), new ParameterValue(FileOperationsShared.DestinationFolderPathPropertyName, keepFileName ? destinationPath : null), new ParameterValue(FileOperationsShared.DestinationFilePathPropertyName, keepFileName ? null : destinationPath))); }
private FunctionResult Execute(string fileName, FileReadOptions readType, TextFileReaderFields fields, int skipHeaderLines = 0, int skipFooterLines = 0, TextCodepage codepage = TextCodepage.Default) { FunctionExecutor tester = (new FunctionTester <TextFileRead.TextFileRead>()).Compile( new PropertyValue(TextFileReadShared.CodepagePropertyName, codepage), new PropertyValue(TextFileReadShared.ReadTypePropertyName, readType), new PropertyValue(TextFileReadShared.FieldsPropertyName, fields), new PropertyValue(TextFileReadShared.SkipHeaderLinesPropertyName, skipHeaderLines), new PropertyValue(TextFileReadShared.SkipFooterLinesPropertyName, skipFooterLines)); var result = tester.Execute(new ParameterValue(FileShared.FilePathPropertyName, fileName)); Assert.IsFalse(FileHelpers.IsFileLocked(fileName)); return(result); }
public void AsyncForeach() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AsyncForeach>(instance => { int res = ((ValueTask <int>)instance.SumWithATwist(AsyncEnumerable.Range(1, 5))).GetAwaiter().GetResult(); res += ((ValueTask <int>)instance.Sum(AsyncEnumerable.Range(1, 3))).GetAwaiter().GetResult(); res += ((ValueTask <int>)instance.SumEmpty()).GetAwaiter().GetResult(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AsyncForeach.cs") .AssertLinesCovered(BuildConfiguration.Debug, // SumWithATwist(IAsyncEnumerable<int>) // Apparently due to entering and exiting the async state machine, line 17 // (the top of an "await foreach" loop) is reached three times *plus* twice // per loop iteration. So, in this case, with five loop iterations, we end // up with 3 + 5 * 2 = 13 hits. (14, 1), (15, 1), (17, 13), (18, 5), (19, 5), (20, 5), (21, 5), (22, 5), (24, 0), (25, 0), (26, 0), (27, 5), (29, 1), (30, 1), // Sum(IAsyncEnumerable<int>) (34, 1), (35, 1), (37, 9), (38, 3), (39, 3), (40, 3), (42, 1), (43, 1), // SumEmpty() (47, 1), (48, 1), (50, 3), (51, 0), (52, 0), (53, 0), (55, 1), (56, 1) ) .AssertBranchesCovered(BuildConfiguration.Debug, // SumWithATwist(IAsyncEnumerable<int>) (17, 2, 1), (17, 3, 5), (19, 0, 5), (19, 1, 0), // Sum(IAsyncEnumerable<int>) (37, 0, 1), (37, 1, 3), // SumEmpty() // If we never entered the loop, that's a branch not taken, which is // what we want to see. (50, 0, 1), (50, 1, 0) ) .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 4); } finally { File.Delete(path); } }
public void StartFunctionTimeout_GlobalTimeout_CreatesExpectedTimer() { MethodInfo method = GetType().GetMethod("GlobalLevel", BindingFlags.Static | BindingFlags.Public); _descriptor.Method = method; TimeoutAttribute attribute = method.GetCustomAttribute <TimeoutAttribute>(); System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, _globalFunctionTimeout, _cancellationTokenSource, _traceWriter); Assert.True(timer.Enabled); Assert.Equal(_globalFunctionTimeout.TotalMilliseconds, timer.Interval); _mockFunctionInstance.VerifyAll(); }
public void GetFunctionTraceLevel_ReturnsExpectedLevel() { _descriptor.Method = typeof(Functions).GetMethod("MethodLevel", BindingFlags.Static | BindingFlags.Public); TraceLevel result = FunctionExecutor.GetFunctionTraceLevel(_mockFunctionInstance.Object); Assert.Equal(TraceLevel.Verbose, result); _descriptor.Method = typeof(Functions).GetMethod("TraceLevelOverride_Off", BindingFlags.Static | BindingFlags.Public); result = FunctionExecutor.GetFunctionTraceLevel(_mockFunctionInstance.Object); Assert.Equal(TraceLevel.Off, result); _descriptor.Method = typeof(Functions).GetMethod("TraceLevelOverride_Error", BindingFlags.Static | BindingFlags.Public); result = FunctionExecutor.GetFunctionTraceLevel(_mockFunctionInstance.Object); Assert.Equal(TraceLevel.Error, result); }
public void StartFunctionTimeout_NoCancellationTokenParameter_ThrowOnTimeoutFalse_ReturnsNull() { MethodInfo method = typeof(Functions).GetMethod("NoCancellationTokenParameter", BindingFlags.Static | BindingFlags.Public); _descriptor.Method = method; TimeoutAttribute attribute = typeof(Functions).GetCustomAttribute <TimeoutAttribute>(); attribute.ThrowOnTimeout = false; System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, attribute, _cancellationTokenSource, _traceWriter); Assert.Null(timer); _mockFunctionInstance.VerifyAll(); }
private FunctionResult Execute(V2.DirectoryOperationsShared.ActionType action, string sourceDirectoryPath = null, string targetDirectoryPath = null, V2.DirectoryOperationsShared.ExistsOption existsOption = V2.DirectoryOperationsShared.ExistsOption.DoNothing, V2.DirectoryOperationsShared.CreateExistsOption createExistsOption = V2.DirectoryOperationsShared.CreateExistsOption.DoNothing, string directory = null, bool replaceIfFileExist = false) { FunctionExecutor tester = (new FunctionTester <DirectoryOperations>()).Compile( new PropertyValue(DirectoryOperationsShared.ActionPropertyName, action), new PropertyValue(DirectoryOperationsShared.ReplaceExistingFilePropertyName, replaceIfFileExist), new PropertyValue(DirectoryOperationsShared.DirectoryExistsPropertyName, existsOption), new PropertyValue(DirectoryOperationsShared.CreateDirectoryExistsPropertyName, createExistsOption)); return(tester.Execute( new ParameterValue(DirectoryOperationsShared.SourceDirectoryPropertyName, sourceDirectoryPath), new ParameterValue(DirectoryOperationsShared.TargetDirectoryPropertyName, targetDirectoryPath), new ParameterValue(DirectoryOperationsShared.DirectoryPropertyName, directory))); }
public void SkipAutoProps(bool skipAutoProps) { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] parameters) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AutoProps>(instance => { instance.AutoPropsNonInit = 10; instance.AutoPropsInit = 20; int readValue = instance.AutoPropsNonInit; readValue = instance.AutoPropsInit; return(Task.CompletedTask); }, persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1])); return(0); }, new string[] { path, skipAutoProps.ToString() }); if (skipAutoProps) { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertNonInstrumentedLines(BuildConfiguration.Debug, 12, 13) .AssertNonInstrumentedLines(BuildConfiguration.Release, 12, 13) .AssertLinesCoveredFromTo(BuildConfiguration.Debug, 9, 11) .AssertLinesCovered(BuildConfiguration.Debug, (7, 1)) .AssertLinesCovered(BuildConfiguration.Release, (10, 1)); } else { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertLinesCoveredFromTo(BuildConfiguration.Debug, 7, 13) .AssertLinesCoveredFromTo(BuildConfiguration.Release, 10, 10) .AssertLinesCoveredFromTo(BuildConfiguration.Release, 12, 13); } } finally { File.Delete(path); } }
public void SelectionStatements_If() { // We need to pass file name to remote process where it save instrumentation result // Similar to msbuild input/output string path = Path.GetTempFileName(); try { // Lambda will run in a custom process to avoid issue with statics and file locking FunctionExecutor.Run(async(string[] pathSerialize) => { // Run load and call a delegate passing class as dynamic to simplify method call CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <SelectionStatements>(instance => { // We call method to trigger coverage hits instance.If(true); // For now we have only async Run helper return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); // we return 0 if we return something different assert fail return(0); }, new string[] { path }); // We retrive and load CoveragePrepareResult and run coverage calculation // Similar to msbuild coverage result task CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); // Generate html report to check // TestInstrumentationHelper.GenerateHtmlReport(result); // Asserts on doc/lines/branches result.Document("Instrumentation.SelectionStatements.cs") // (line, hits) .AssertLinesCovered((11, 1), (15, 0)) // (line,ordinal,hits) .AssertBranchesCovered((9, 0, 1), (9, 1, 0)); } finally { // Cleanup tmp file File.Delete(path); } }
public void SkipAutoPropsInRecords(bool skipAutoProps) { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] parameters) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <RecordWithPropertyInit>(instance => { instance.RecordAutoPropsNonInit = string.Empty; instance.RecordAutoPropsInit = string.Empty; string readValue = instance.RecordAutoPropsInit; readValue = instance.RecordAutoPropsNonInit; return(Task.CompletedTask); }, persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1])); return(0); }, new string[] { path, skipAutoProps.ToString() }); if (skipAutoProps) { TestInstrumentationHelper.GetCoverageResult(path).GenerateReport(show: true) .Document("Instrumentation.AutoProps.cs") .AssertNonInstrumentedLines(BuildConfiguration.Debug, 23, 24) .AssertNonInstrumentedLines(BuildConfiguration.Release, 23, 24) .AssertLinesCovered(BuildConfiguration.Debug, (18, 1), (20, 1), (21, 1), (22, 1)) .AssertLinesCovered(BuildConfiguration.Release, (21, 1)); } else { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertLinesCoveredFromTo(BuildConfiguration.Debug, 18, 24) .AssertLinesCoveredFromTo(BuildConfiguration.Release, 21, 21) .AssertLinesCoveredFromTo(BuildConfiguration.Release, 23, 24); } } finally { File.Delete(path); } }
public async Task GatherConsoleOut() { string outText = null; FunctionExecutor FunctionExecutor = new FunctionExecutor( o => { o.StartInfo.RedirectStandardOutput = true; o.OnExit = p => { outText = p.StandardOutput.ReadToEnd(); }; } ); await FunctionExecutor.RunAsync( () => { Console.Write("hello world"); return(0); }); Assert.Equal("hello world", outText); }
public void StartFunctionTimeout_ClassLevelTimeout_CreatesExpectedTimer() { MethodInfo method = typeof(Functions).GetMethod("ClassLevel", BindingFlags.Static | BindingFlags.Public); _descriptor.Method = method; // we need to set up the Id so that when the timer fires it doesn't throw, but since this is Strict, we need to access it first. _mockFunctionInstance.SetupGet(p => p.Id).Returns(Guid.Empty); Assert.NotNull(_mockFunctionInstance.Object.Id); TimeoutAttribute attribute = typeof(Functions).GetCustomAttribute <TimeoutAttribute>(); System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, attribute, _cancellationTokenSource, _traceWriter); Assert.True(timer.Enabled); Assert.Equal(attribute.Timeout.TotalMilliseconds, timer.Interval); _mockFunctionInstance.VerifyAll(); }
public SCADAService() { modelResourcesDesc = new ModelResourcesDesc(); enumDescs = new EnumDescs(); scadaModel = new SCADAModel(modelResourcesDesc, enumDescs); functionExecutor = new FunctionExecutor(scadaModel); FunctionFactory.SCADAModel = scadaModel; SCADAModelUpdateNotification.SCADAModel = scadaModel; SCADATransactionActor.SCADAModel = scadaModel; CommandService.SCADAModel = scadaModel; IntegrityUpdateService.SCADAModel = scadaModel; CommandService.WriteCommandEnqueuer = functionExecutor; scadaModel.ImportModel(); InitializeHosts(); }
public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context) { switch (symbol.Type) { case SymbolType.Number: case SymbolType.Identifier: return(new ErrorState(symbol)); case SymbolType.Operator: var @operator = OperatorFactory.CreateOperator(symbol); if (@operator is OpeningBracket || @operator is CommaOperator) { return(new ErrorState(symbol)); } if (@operator is AssignmentOperator) { if (_functionSignature.IsValidFunctionDeclaration()) { return(new FunctionBodyReadingState(_functionSignature.ConvertToFunction())); } return(new ErrorState(symbol)); } var function = context._functionsRepository.Get(_functionSignature.Name.Body); var result = FunctionExecutor.Execute(_functionSignature, function, context); stack.Push(new Number(result.Value)); stack.Push(@operator); if (@operator is ClosingBracket) { return(new ClosingBracketOperatorState()); } return(new BinaryOperatorState()); default: return(new ErrorState(symbol)); } }
public static FunctionIndexer Create(CloudStorageAccount account = null, INameResolver nameResolver = null, IExtensionRegistry extensionRegistry = null, TraceWriter traceWriter = null) { Mock <IServiceProvider> services = new Mock <IServiceProvider>(MockBehavior.Strict); StorageClientFactory clientFactory = new StorageClientFactory(); services.Setup(p => p.GetService(typeof(StorageClientFactory))).Returns(clientFactory); IStorageAccount storageAccount = account != null ? new StorageAccount(account, services.Object) : null; IStorageAccountProvider storageAccountProvider = new SimpleStorageAccountProvider(services.Object) { StorageAccount = account }; IExtensionTypeLocator extensionTypeLocator = new NullExtensionTypeLocator(); ContextAccessor <IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor <IMessageEnqueuedWatcher>(); ContextAccessor <IBlobWrittenWatcher> blobWrittenWatcherAccessor = new ContextAccessor <IBlobWrittenWatcher>(); ISharedContextProvider sharedContextProvider = new SharedContextProvider(); TraceWriter logger = traceWriter ?? new TestTraceWriter(TraceLevel.Verbose); SingletonManager singletonManager = new SingletonManager(); IWebJobsExceptionHandler exceptionHandler = new WebJobsExceptionHandler(); var blobsConfiguration = new JobHostBlobsConfiguration(); ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, new FixedHostIdProvider("test"), new SimpleQueueConfiguration(maxDequeueCount: 5), blobsConfiguration, exceptionHandler, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, new DefaultExtensionRegistry(), singletonManager, logger); IBindingProvider bindingProvider = DefaultBindingProvider.Create(nameResolver, null, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, new DefaultExtensionRegistry()); IFunctionOutputLoggerProvider outputLoggerProvider = new NullFunctionOutputLoggerProvider(); IFunctionOutputLogger outputLogger = outputLoggerProvider.GetAsync(CancellationToken.None).Result; IFunctionExecutor executor = new FunctionExecutor(new NullFunctionInstanceLogger(), outputLogger, exceptionHandler, logger); if (extensionRegistry == null) { extensionRegistry = new DefaultExtensionRegistry(); } return(new FunctionIndexer(triggerBindingProvider, bindingProvider, DefaultJobActivator.Instance, executor, extensionRegistry, new SingletonManager(), logger)); }
private BasePointItem CreatePoint(ConfigItem c, int i, FunctionExecutor commandExecutor) { switch (c.RegistryType) { case PointType.DIGITAL_INPUT: return(new DigitalInput(c.RegistryType, (ushort)(c.StartAddress + i), c.DefaultValue, commandExecutor, string.Format("{0} [{1}]", c.Description, i), this, c.MinValue, c.MaxValue)); case PointType.DIGITAL_OUTPUT: return(new DigitalOutput(c.RegistryType, (ushort)(c.StartAddress + i), c.DefaultValue, commandExecutor, string.Format("{0} [{1}]", c.Description, i), this, c.MinValue, c.MaxValue)); case PointType.ANALOG_INPUT: return(new AnalaogInput(c.RegistryType, (ushort)(c.StartAddress + i), c.DefaultValue, commandExecutor, string.Format("{0} [{1}]", c.Description, i), this, c.MinValue, c.MaxValue)); case PointType.ANALOG_OUTPUT: return(new AnalogOutput(c.RegistryType, (ushort)(c.StartAddress + i), c.DefaultValue, commandExecutor, string.Format("{0} [{1}]", c.Description, i), this, c.MinValue, c.MaxValue)); default: return(null); } }
public async Task InvokeAsync_NoCancellation() { bool called = false; Mock <IFunctionInvoker> mockInvoker = new Mock <IFunctionInvoker>(); mockInvoker.Setup(i => i.InvokeAsync(It.IsAny <object[]>())) .Returns(() => { called = true; return(Task.FromResult(0)); }); var timeoutSource = new CancellationTokenSource(); var shutdownSource = new CancellationTokenSource(); bool throwOnTimeout = false; await FunctionExecutor.InvokeAsync(mockInvoker.Object, new object[0], timeoutSource, shutdownSource, throwOnTimeout, TimeSpan.MinValue, null); Assert.True(called); }
public void SkipRecordWithProperties(bool skipAutoProps) { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] parameters) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <ClassWithAutoRecordProperties>(instance => { return(Task.CompletedTask); }, persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1])); return(0); }, new string[] { path, skipAutoProps.ToString() }); if (skipAutoProps) { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertNonInstrumentedLines(BuildConfiguration.Debug, 29, 29) .AssertNonInstrumentedLines(BuildConfiguration.Release, 29, 29) .AssertLinesCovered(BuildConfiguration.Debug, (32, 1), (33, 1), (34, 1)) .AssertLinesCovered(BuildConfiguration.Release, (33, 1)); } else { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertLinesCovered(BuildConfiguration.Debug, (29, 3), (31, 1), (32, 1), (33, 1), (34, 1)) .AssertLinesCovered(BuildConfiguration.Release, (29, 3), (31, 1), (33, 1)); } } finally { File.Delete(path); } }
public static FunctionIndexer Create(CloudStorageAccount account = null, INameResolver nameResolver = null, IExtensionRegistry extensionRegistry = null) { Mock<IServiceProvider> services = new Mock<IServiceProvider>(MockBehavior.Strict); StorageClientFactory clientFactory = new StorageClientFactory(); services.Setup(p => p.GetService(typeof(StorageClientFactory))).Returns(clientFactory); IStorageAccount storageAccount = account != null ? new StorageAccount(account, services.Object) : null; IStorageAccountProvider storageAccountProvider = new SimpleStorageAccountProvider(services.Object) { StorageAccount = account }; IExtensionTypeLocator extensionTypeLocator = new NullExtensionTypeLocator(); ContextAccessor<IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor<IMessageEnqueuedWatcher>(); ContextAccessor<IBlobWrittenWatcher> blobWrittenWatcherAccessor = new ContextAccessor<IBlobWrittenWatcher>(); ISharedContextProvider sharedContextProvider = new SharedContextProvider(); TestTraceWriter logger = new TestTraceWriter(TraceLevel.Verbose); SingletonManager singletonManager = new SingletonManager(); ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, new FixedHostIdProvider("test"), new SimpleQueueConfiguration(maxDequeueCount: 5), BackgroundExceptionDispatcher.Instance, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, new DefaultExtensionRegistry(), singletonManager, logger); IBindingProvider bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, new DefaultExtensionRegistry()); TraceWriter trace = new TestTraceWriter(TraceLevel.Verbose); IFunctionOutputLoggerProvider outputLoggerProvider = new NullFunctionOutputLoggerProvider(); IFunctionOutputLogger outputLogger = outputLoggerProvider.GetAsync(CancellationToken.None).Result; IFunctionExecutor executor = new FunctionExecutor(new NullFunctionInstanceLogger(), outputLogger, BackgroundExceptionDispatcher.Instance, trace, null); if (extensionRegistry == null) { extensionRegistry = new DefaultExtensionRegistry(); } return new FunctionIndexer(triggerBindingProvider, bindingProvider, DefaultJobActivator.Instance, executor, extensionRegistry, new SingletonManager(), trace); }
public void ExcludeFromCodeCoverage_CompilerGeneratedMethodsAndTypes() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <MethodsWithExcludeFromCodeCoverageAttr>(instance => { ((Task <int>)instance.Test("test")).ConfigureAwait(false).GetAwaiter().GetResult(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); var document = result.Document("Instrumentation.ExcludeFromCoverage.cs"); // Invoking method "Test" of class "MethodsWithExcludeFromCodeCoverageAttr" we expect to cover 100% lines for MethodsWithExcludeFromCodeCoverageAttr Assert.DoesNotContain(document.Lines, l => (l.Value.Class == "Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr" || // Compiler generated l.Value.Class.StartsWith("Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr/")) && l.Value.Hits == 0); // and 0% for MethodsWithExcludeFromCodeCoverageAttr2 Assert.DoesNotContain(document.Lines, l => (l.Value.Class == "Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr2" || // Compiler generated l.Value.Class.StartsWith("Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr2/")) && l.Value.Hits == 1); } finally { File.Delete(path); } }
public void AwaitUsing() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AwaitUsing>(instance => { ((ValueTask)instance.HasAwaitUsing()).GetAwaiter().GetResult(); ((Task)instance.Issue914_Repro()).GetAwaiter().GetResult(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AwaitUsing.cs") .AssertLinesCovered(BuildConfiguration.Debug, // HasAwaitUsing() (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), // Issue914_Repro() (21, 1), (22, 1), (23, 1), (24, 1), // Issue914_Repro_Example1() (28, 1), (29, 1), (30, 1), // Issue914_Repro_Example2() (34, 1), (35, 1), (36, 1), (37, 1), // MyTransaction.DisposeAsync() (43, 2), (44, 2), (45, 2) ) .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 0); } finally { File.Delete(path); } }
public async Task InvokeAsync_Stop_Timeout_Throw() { Mock <IFunctionInvoker> mockInvoker = new Mock <IFunctionInvoker>(); mockInvoker.Setup(i => i.InvokeAsync(It.IsAny <object>(), It.IsAny <object[]>())) .Returns(async() => { bool exit = false; Task ignore = Task.Delay(5000).ContinueWith((ct) => exit = true); while (!exit) { await Task.Delay(500); } return(null); }); // setup the instance details for the exception message MethodInfo method = typeof(Functions).GetMethod("ClassLevel", BindingFlags.Static | BindingFlags.Public); _descriptor = FunctionIndexer.FromMethod(method); _mockFunctionInstance.SetupGet(p => p.Id).Returns(Guid.Empty); var timeoutSource = new CancellationTokenSource(); var shutdownSource = new CancellationTokenSource(); object[] parameters = new object[] { shutdownSource.Token }; bool throwOnTimeout = true; TimeSpan timeoutInterval = TimeSpan.FromMilliseconds(1000); shutdownSource.CancelAfter(500); timeoutSource.CancelAfter(timeoutInterval); var ex = await Assert.ThrowsAsync <FunctionTimeoutException>(() => FunctionExecutor.InvokeAsync(mockInvoker.Object, NewArgs(parameters), timeoutSource, shutdownSource, throwOnTimeout, timeoutInterval, _mockFunctionInstance.Object)); var expectedMessage = string.Format("Timeout value of {0} was exceeded by function: {1}", timeoutInterval, _mockFunctionInstance.Object.FunctionDescriptor.ShortName); Assert.Equal(expectedMessage, ex.Message); }
public void AsyncIterator() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AsyncIterator>(instance => { int res = ((Task <int>)instance.Issue1104_Repro()).GetAwaiter().GetResult(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AsyncIterator.cs") .AssertLinesCovered(BuildConfiguration.Debug, // Issue1104_Repro() (14, 1), (15, 1), (17, 203), (18, 100), (19, 100), (20, 100), (22, 1), (23, 1), // CreateSequenceAsync() (26, 1), (27, 202), (28, 100), (29, 100), (30, 100), (31, 100), (32, 1) ) .AssertBranchesCovered(BuildConfiguration.Debug, // Issue1104_Repro(), (17, 0, 1), (17, 1, 100), // CreateSequenceAsync() (27, 0, 1), (27, 1, 100) ) .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 2); } finally { File.Delete(path); } }
public void OnFunctionTimeout_PerformsExpectedOperations() { System.Timers.Timer timer = new System.Timers.Timer(TimeSpan.FromMinutes(1).TotalMilliseconds); timer.Start(); Assert.True(timer.Enabled); Assert.False(_cancellationTokenSource.IsCancellationRequested); MethodInfo method = typeof(Functions).GetMethod("MethodLevel", BindingFlags.Static | BindingFlags.Public); TimeoutAttribute attribute = method.GetCustomAttribute <TimeoutAttribute>(); Guid instanceId = Guid.Parse("B2D1DD72-80E2-412B-A22E-3B4558F378B4"); FunctionExecutor.OnFunctionTimeout(timer, method, instanceId, attribute.Timeout, _traceWriter, _cancellationTokenSource); Assert.False(timer.Enabled); Assert.True(_cancellationTokenSource.IsCancellationRequested); TraceEvent trace = _traceWriter.Traces[0]; Assert.Equal(TraceLevel.Error, trace.Level); Assert.Equal(TraceSource.Execution, trace.Source); Assert.Equal("Timeout value of 00:01:00 exceeded by function 'Functions.MethodLevel' (Id: 'b2d1dd72-80e2-412b-a22e-3b4558f378b4'). Initiating cancellation.", trace.Message); }
public static async Task<JobHostContext> CreateAndLogHostStartedAsync( JobHost host, IStorageAccountProvider storageAccountProvider, IQueueConfiguration queueConfiguration, ITypeLocator typeLocator, IJobActivator activator, INameResolver nameResolver, IConsoleProvider consoleProvider, JobHostConfiguration config, CancellationToken shutdownToken, CancellationToken cancellationToken, IHostIdProvider hostIdProvider = null, FunctionExecutor functionExecutor = null, IFunctionIndexProvider functionIndexProvider = null, IBindingProvider bindingProvider = null, IHostInstanceLoggerProvider hostInstanceLogerProvider = null, IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = null, IFunctionOutputLoggerProvider functionOutputLoggerProvider = null, IBackgroundExceptionDispatcher backgroundExceptionDispatcher = null, SingletonManager singletonManager = null) { if (hostIdProvider == null) { hostIdProvider = new DynamicHostIdProvider(storageAccountProvider, () => functionIndexProvider); } IExtensionTypeLocator extensionTypeLocator = new ExtensionTypeLocator(typeLocator); if (backgroundExceptionDispatcher == null) { backgroundExceptionDispatcher = BackgroundExceptionDispatcher.Instance; } ContextAccessor<IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor<IMessageEnqueuedWatcher>(); ContextAccessor<IBlobWrittenWatcher> blobWrittenWatcherAccessor = new ContextAccessor<IBlobWrittenWatcher>(); ISharedContextProvider sharedContextProvider = new SharedContextProvider(); // Create a wrapper TraceWriter that delegates to both the user // TraceWriter specified on Config (if present), as well as to Console TraceWriter trace = new ConsoleTraceWriter(config.Tracing, consoleProvider.Out); // Register system services with the service container config.AddService<INameResolver>(nameResolver); ExtensionConfigContext context = new ExtensionConfigContext { Config = config, Trace = trace, Host = host }; InvokeExtensionConfigProviders(context); IExtensionRegistry extensions = config.GetExtensions(); ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, hostIdProvider, queueConfiguration, backgroundExceptionDispatcher, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, extensions, trace); if (bindingProvider == null) { bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, extensions); } DefaultLoggerProvider loggerProvider = new DefaultLoggerProvider(storageAccountProvider, trace); if (singletonManager == null) { singletonManager = new SingletonManager(storageAccountProvider, backgroundExceptionDispatcher, config.Singleton, trace, config.NameResolver); } using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken)) { CancellationToken combinedCancellationToken = combinedCancellationSource.Token; await WriteSiteExtensionManifestAsync(combinedCancellationToken); IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken); IHostInstanceLogger hostInstanceLogger = null; if (hostInstanceLogerProvider != null) { hostInstanceLogger = await hostInstanceLogerProvider.GetAsync(combinedCancellationToken); } else { hostInstanceLogger = await((IHostInstanceLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken); } IFunctionInstanceLogger functionInstanceLogger = null; if (functionInstanceLoggerProvider != null) { functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken); } else { functionInstanceLogger = (IFunctionInstanceLogger)(await((IFunctionInstanceLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken)); } IFunctionOutputLogger functionOutputLogger = null; if (functionOutputLoggerProvider != null) { functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken); } else { functionOutputLogger = (IFunctionOutputLogger)(await((IFunctionOutputLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken)); } if (functionExecutor == null) { functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, backgroundExceptionDispatcher, trace); } if (functionIndexProvider == null) { functionIndexProvider = new FunctionIndexProvider(typeLocator, triggerBindingProvider, bindingProvider, activator, functionExecutor, extensions, singletonManager); } IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken); IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, trace); IFunctionExecutor hostCallExecutor; IListener listener; HostOutputMessage hostOutputMessage; if (dashboardAccount != null) { string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken); string sharedQueueName = HostQueueNames.GetHostQueueName(hostId); IStorageQueueClient dashboardQueueClient = dashboardAccount.CreateQueueClient(); IStorageQueue sharedQueue = dashboardQueueClient.GetQueueReference(sharedQueueName); IListenerFactory sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue, queueConfiguration, backgroundExceptionDispatcher, trace, functions, functionInstanceLogger, functionExecutor); Guid hostInstanceId = Guid.NewGuid(); string instanceQueueName = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N")); IStorageQueue instanceQueue = dashboardQueueClient.GetQueueReference(instanceQueueName); IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue, queueConfiguration, backgroundExceptionDispatcher, trace, functions, functionInstanceLogger, functionExecutor); HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor { SharedContainerName = HostContainerNames.Hosts, SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId, InstanceBlobName = hostInstanceId.ToString("N"), ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds }; IStorageBlockBlob blob = dashboardAccount.CreateBlobClient() .GetContainerReference(heartbeatDescriptor.SharedContainerName) .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName); IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob)); IEnumerable<MethodInfo> indexedMethods = functions.ReadAllMethods(); Assembly hostAssembly = GetHostAssembly(indexedMethods); string displayName = hostAssembly != null ? hostAssembly.GetName().Name : "Unknown"; hostOutputMessage = new DataOnlyHostOutputMessage { HostInstanceId = hostInstanceId, HostDisplayName = displayName, SharedQueueName = sharedQueueName, InstanceQueueName = instanceQueueName, Heartbeat = heartbeatDescriptor, WebJobRunIdentifier = WebJobRunIdentifier.Current }; hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand, backgroundExceptionDispatcher, shutdownToken, functionExecutor); IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory, sharedQueueListenerFactory, instanceQueueListenerFactory); listener = CreateHostListener(hostListenerFactory, heartbeatCommand, backgroundExceptionDispatcher, shutdownToken); // Publish this to Azure logging account so that a web dashboard can see it. await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken); } else { hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor); IListener factoryListener = new ListenerFactoryListener(functionsListenerFactory); IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener); listener = shutdownListener; hostOutputMessage = new DataOnlyHostOutputMessage(); } functionExecutor.HostOutputMessage = hostOutputMessage; IEnumerable<FunctionDescriptor> descriptors = functions.ReadAllDescriptors(); int descriptorsCount = descriptors.Count(); if (descriptorsCount == 0) { trace.Warning("No functions found. Try making job classes and methods public.", TraceSource.Indexing); } else { StringBuilder functionsTrace = new StringBuilder(); functionsTrace.AppendLine("Found the following functions:"); foreach (FunctionDescriptor descriptor in descriptors) { functionsTrace.AppendLine(descriptor.FullName); } trace.Info(functionsTrace.ToString(), TraceSource.Indexing); } return new JobHostContext(functions, hostCallExecutor, listener, trace); } }