private static RpcFunctionInvocationDispatcher GetTestFunctionDispatcher(string maxProcessCountValue = null, bool addWebhostChannel = false, Mock <IWebHostRpcWorkerChannelManager> mockwebHostLanguageWorkerChannelManager = null, bool throwOnProcessStartUp = false)
        {
            var eventManager            = new ScriptEventManager();
            var metricsLogger           = new Mock <IMetricsLogger>();
            var mockApplicationLifetime = new Mock <IApplicationLifetime>();
            var testEnv = new TestEnvironment();

            if (!string.IsNullOrEmpty(maxProcessCountValue))
            {
                testEnv.SetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerProcessCountSettingName, maxProcessCountValue);
            }

            var loggerFactory = MockNullLoggerFactory.CreateLoggerFactory();

            var options = new ScriptJobHostOptions
            {
                RootLogPath = Path.GetTempPath()
            };

            IOptions <ScriptJobHostOptions> scriptOptions = new OptionsManager <ScriptJobHostOptions>(new TestOptionsFactory <ScriptJobHostOptions>(options));

            var workerConfigOptions = new LanguageWorkerOptions
            {
                WorkerConfigs = TestHelpers.GetTestWorkerConfigs()
            };
            IRpcWorkerChannelFactory        testLanguageWorkerChannelFactory        = new TestRpcWorkerChannelFactory(eventManager, _testLogger, scriptOptions.Value.RootScriptPath, throwOnProcessStartUp);
            IWebHostRpcWorkerChannelManager testWebHostLanguageWorkerChannelManager = new TestRpcWorkerChannelManager(eventManager, _testLogger, scriptOptions.Value.RootScriptPath, testLanguageWorkerChannelFactory);
            IJobHostRpcWorkerChannelManager jobHostLanguageWorkerChannelManager     = new JobHostRpcWorkerChannelManager(loggerFactory);

            if (addWebhostChannel)
            {
                testWebHostLanguageWorkerChannelManager.InitializeChannelAsync("java");
            }
            if (mockwebHostLanguageWorkerChannelManager != null)
            {
                testWebHostLanguageWorkerChannelManager = mockwebHostLanguageWorkerChannelManager.Object;
            }
            var mockFunctionDispatcherLoadBalancer = new Mock <IRpcFunctionInvocationDispatcherLoadBalancer>();

            _javaTestChannel = new TestRpcWorkerChannel(Guid.NewGuid().ToString(), "java", eventManager, _testLogger, false);
            var optionsMonitor = TestHelpers.CreateOptionsMonitor(workerConfigOptions);

            return(new RpcFunctionInvocationDispatcher(scriptOptions,
                                                       metricsLogger.Object,
                                                       testEnv,
                                                       mockApplicationLifetime.Object,
                                                       eventManager,
                                                       loggerFactory,
                                                       testLanguageWorkerChannelFactory,
                                                       optionsMonitor,
                                                       testWebHostLanguageWorkerChannelManager,
                                                       jobHostLanguageWorkerChannelManager,
                                                       new OptionsWrapper <ManagedDependencyOptions>(new ManagedDependencyOptions()),
                                                       mockFunctionDispatcherLoadBalancer.Object));
        }
        private static RpcFunctionInvocationDispatcher GetTestFunctionDispatcher(int maxProcessCountValue = 1, bool addWebhostChannel = false,
                                                                                 Mock <IWebHostRpcWorkerChannelManager> mockwebHostLanguageWorkerChannelManager = null, bool throwOnProcessStartUp = false, TimeSpan?startupIntervals = null, string runtime = null, bool workerIndexing = false, bool placeholder = false)
        {
            var      eventManager            = new ScriptEventManager();
            var      metricsLogger           = new Mock <IMetricsLogger>();
            var      mockApplicationLifetime = new Mock <IApplicationLifetime>();
            var      testEnv   = new TestEnvironment();
            TimeSpan intervals = startupIntervals ?? TimeSpan.FromMilliseconds(100);

            testEnv.SetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerProcessCountSettingName, maxProcessCountValue.ToString());

            if (runtime != null)
            {
                testEnv.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, runtime);
            }
            if (workerIndexing)
            {
                testEnv.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, ScriptConstants.FeatureFlagEnableWorkerIndexing);
            }
            if (placeholder)
            {
                testEnv.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
            }

            var options = new ScriptJobHostOptions
            {
                RootLogPath = Path.GetTempPath()
            };

            IOptions <ScriptJobHostOptions> scriptOptions = new OptionsManager <ScriptJobHostOptions>(new TestOptionsFactory <ScriptJobHostOptions>(options));

            var workerConfigOptions = new LanguageWorkerOptions
            {
                WorkerConfigs = TestHelpers.GetTestWorkerConfigs(processCountValue: maxProcessCountValue, processStartupInterval: intervals,
                                                                 processRestartInterval: intervals, processShutdownTimeout: TimeSpan.FromSeconds(1), workerIndexing: workerIndexing)
            };
            IRpcWorkerChannelFactory        testLanguageWorkerChannelFactory        = new TestRpcWorkerChannelFactory(eventManager, _testLogger, scriptOptions.Value.RootScriptPath, throwOnProcessStartUp);
            IWebHostRpcWorkerChannelManager testWebHostLanguageWorkerChannelManager = new TestRpcWorkerChannelManager(eventManager, _testLogger, scriptOptions.Value.RootScriptPath, testLanguageWorkerChannelFactory);
            IJobHostRpcWorkerChannelManager jobHostLanguageWorkerChannelManager     = new JobHostRpcWorkerChannelManager(_testLoggerFactory);

            if (addWebhostChannel)
            {
                testWebHostLanguageWorkerChannelManager.InitializeChannelAsync("java");
            }
            if (mockwebHostLanguageWorkerChannelManager != null)
            {
                testWebHostLanguageWorkerChannelManager = mockwebHostLanguageWorkerChannelManager.Object;
            }

            var mockFunctionDispatcherLoadBalancer = new Mock <IRpcFunctionInvocationDispatcherLoadBalancer>();

            _javaTestChannel = new TestRpcWorkerChannel(Guid.NewGuid().ToString(), "java", eventManager, _testLogger, false);
            var optionsMonitor = TestHelpers.CreateOptionsMonitor(workerConfigOptions);

            return(new RpcFunctionInvocationDispatcher(scriptOptions,
                                                       metricsLogger.Object,
                                                       testEnv,
                                                       mockApplicationLifetime.Object,
                                                       eventManager,
                                                       _testLoggerFactory,
                                                       testLanguageWorkerChannelFactory,
                                                       optionsMonitor,
                                                       testWebHostLanguageWorkerChannelManager,
                                                       jobHostLanguageWorkerChannelManager,
                                                       new OptionsWrapper <ManagedDependencyOptions>(new ManagedDependencyOptions()),
                                                       mockFunctionDispatcherLoadBalancer.Object,
                                                       Options.Create(new WorkerConcurrencyOptions())));
        }