Beispiel #1
0
        public async Task HostHealthMonitor_RestartsSuccessfully_WhenHostRecovers()
        {
            Assert.Equal(ScriptHostState.Running, _scriptHostService.State);

            // crank this number up so we don't do a shutdown but instead
            // continue the retry loop
            _healthMonitorOptions.HealthCheckThreshold = 100;

            // now that host is running make host unhealthy and wait
            // for host shutdown
            _exceededCounters.Add("Connections");
            _underHighLoad = true;

            await TestHelpers.Await(() => _scriptHostService.State == ScriptHostState.Error);

            var lastError = _scriptHostService.LastError;

            Assert.Equal("Host thresholds exceeded: [Connections]. For more information, see https://aka.ms/functions-thresholds.", _scriptHostService.LastError.Message);

            // verify we are in a startup/retry loop
            await TestHelpers.Await(() =>
            {
                var allLogs = _testHost.GetLog();
                return(allLogs.Contains("Host initialization: ConsecutiveErrors=3"));
            });

            Assert.Equal(ScriptHostState.Error, _scriptHostService.State);
            _mockJobHostEnvironment.Verify(p => p.Shutdown(), Times.Never);

            // after a few retries, put the host back to health and verify
            // it starts successfully
            _exceededCounters.Clear();
            _underHighLoad = false;

            await TestHelpers.Await(() => _scriptHostService.State == ScriptHostState.Running);

            Assert.Null(_scriptHostService.LastError);

            var logMessages = _testHost.GetScriptHostLogMessages();

            Assert.Contains(logMessages, p => p.FormattedMessage == "Job host started");
        }
        public async Task HostStatusReturns_IfHostJsonError()
        {
            string hostJsonPath = Path.Combine(_hostPath, ScriptConstants.HostMetadataFileName);

            // Simulate a non-empty file without a 'version'
            JObject hostConfig = JObject.FromObject(new
            {
                functionTimeout = TimeSpan.FromSeconds(30)
            });

            await File.WriteAllTextAsync(hostJsonPath, hostConfig.ToString());

            string logPath = Path.Combine(Path.GetTempPath(), @"Functions");

            _host = new TestFunctionHost(_hostPath, logPath, _ => { });

            // Ping the status endpoint to ensure we see the exception
            HostStatus status = await _host.GetHostStatusAsync();

            Assert.Equal("Error", status.State);
            Assert.Equal("Microsoft.Azure.WebJobs.Script: The host.json file is missing the required 'version' property. See https://aka.ms/functions-hostjson for steps to migrate the configuration file.", status.Errors.Single());

            // Due to https://github.com/Azure/azure-functions-host/issues/1351, slow this down to ensure
            // we have a host running and watching for file changes.
            await TestHelpers.Await(() =>
            {
                return(_host.GetLog().Contains("[Microsoft.Extensions.Hosting.Internal.Host] Hosting started"));
            });

            // Now update the file and make sure it auto-restarts.
            hostConfig["version"] = "2.0";
            await File.WriteAllTextAsync(hostJsonPath, hostConfig.ToString());

            await TestHelpers.Await(async() =>
            {
                status = await _host.GetHostStatusAsync();
                return(status.State == $"{ScriptHostState.Running}");
            }, userMessageCallback : _host.GetLog);

            Assert.Null(status.Errors);
        }