|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics; |
| 11 | +using Microsoft.Azure.WebJobs.Script.WebHost.Models; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Newtonsoft.Json; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.WebHostEndToEnd |
| 17 | +{ |
| 18 | + public class DiagnosticsEndToEndTests |
| 19 | + { |
| 20 | + private const string _scriptRoot = @"TestScripts\CSharp"; |
| 21 | + private readonly string _testLogPath = Path.Combine(TestHelpers.FunctionsTestDirectory, "Logs", Guid.NewGuid().ToString(), @"Functions"); |
| 22 | + |
| 23 | + private TestEventGenerator _eventGenerator = new TestEventGenerator(); |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task FileLogger_IOExceptionDuringInvocation_Recovers() |
| 27 | + { |
| 28 | + var fileWriterFactory = new TestFileWriterFactory(onAppendLine: null, |
| 29 | + onFlush: () => |
| 30 | + { |
| 31 | + // The below function will fail, causing an immediate flush. This exception |
| 32 | + // simulates the disk being full. ExecutionEvents should be logged as expected |
| 33 | + // and the "Finished" event should get logged. |
| 34 | + throw new IOException(); |
| 35 | + }); |
| 36 | + |
| 37 | + using (var host = new TestFunctionHost(_scriptRoot, _testLogPath, |
| 38 | + configureWebHostServices: s => |
| 39 | + { |
| 40 | + s.AddSingleton<IEventGenerator>(_ => _eventGenerator); |
| 41 | + }, |
| 42 | + configureScriptHostServices: s => |
| 43 | + { |
| 44 | + s.AddSingleton<IFileWriterFactory>(_ => fileWriterFactory); |
| 45 | + |
| 46 | + s.PostConfigure<ScriptJobHostOptions>(o => |
| 47 | + { |
| 48 | + o.FileLoggingMode = FileLoggingMode.Always; |
| 49 | + o.Functions = new[] { "HttpTrigger-Scenarios" }; |
| 50 | + }); |
| 51 | + })) |
| 52 | + { |
| 53 | + // Issue an invalid request that fails. |
| 54 | + var content = new StringContent(JsonConvert.SerializeObject(new { scenario = "invalid" })); |
| 55 | + var response = await host.HttpClient.PostAsync("/api/HttpTrigger-Scenarios", content); |
| 56 | + Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); |
| 57 | + |
| 58 | + await TestHelpers.Await(() => |
| 59 | + { |
| 60 | + var executionEvents = _eventGenerator.GetFunctionExecutionEvents(); |
| 61 | + return executionEvents.SingleOrDefault(p => p.ExecutionStage == ExecutionStage.Finished) != null; |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private class TestFileWriterFactory : IFileWriterFactory |
| 67 | + { |
| 68 | + private readonly Action<string> _onAppendLine; |
| 69 | + private readonly Action _onFlush; |
| 70 | + |
| 71 | + public TestFileWriterFactory(Action<string> onAppendLine, Action onFlush) |
| 72 | + { |
| 73 | + _onAppendLine = onAppendLine; |
| 74 | + _onFlush = onFlush; |
| 75 | + } |
| 76 | + |
| 77 | + public IFileWriter Create(string filePath) => new TestFileWriter(_onAppendLine, _onFlush); |
| 78 | + } |
| 79 | + |
| 80 | + private class TestFileWriter : IFileWriter |
| 81 | + { |
| 82 | + private readonly Action<string> _onAppendLine; |
| 83 | + private readonly Action _onFlush; |
| 84 | + |
| 85 | + public TestFileWriter(Action<string> onAppendLine, Action onFlush) |
| 86 | + { |
| 87 | + _onAppendLine = onAppendLine; |
| 88 | + _onFlush = onFlush; |
| 89 | + } |
| 90 | + |
| 91 | + public void AppendLine(string line) |
| 92 | + { |
| 93 | + _onAppendLine?.Invoke(line); |
| 94 | + } |
| 95 | + |
| 96 | + public void Flush() |
| 97 | + { |
| 98 | + _onFlush(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments