Skip to content

Commit f03b0ec

Browse files
authored
Address unit test timing and setting Console.Out (#10808)
* Address unit test timing and setting Console.Out * Make methods internal * Use async overloads
1 parent b3392e0 commit f03b0ec

File tree

4 files changed

+94
-92
lines changed

4 files changed

+94
-92
lines changed

src/WebJobs.Script.WebHost/Diagnostics/BufferedConsoleWriter.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Channels;
@@ -50,6 +51,8 @@ internal BufferedConsoleWriter(int bufferSize, Action<Exception> exceptionHandle
5051
_exceptionhandler = exceptionHandler;
5152
}
5253

54+
public TextWriter Writer { get; init; } = Console.Out;
55+
5356
public void WriteHandler(string evt)
5457
{
5558
_writeEvent(evt);
@@ -65,7 +68,7 @@ private void WriteToConsoleBuffer(string evt)
6568
// If the wait failed, write to the console. Otherwise, try the writing again - if that fails, write to the console.
6669
if (waitFailed || !_consoleBuffer.Writer.TryWrite(evt))
6770
{
68-
Console.WriteLine(evt);
71+
Writer.WriteLine(evt);
6972
}
7073
}
7174
}
@@ -108,12 +111,12 @@ private async Task ProcessConsoleBufferAsync()
108111
}
109112

110113
_writeResetEvent.Set();
111-
Console.Write(builder.ToString());
114+
await Writer.WriteAsync(builder.ToString());
112115
builder.Clear();
113116
}
114117
else
115118
{
116-
Console.WriteLine(line1);
119+
await Writer.WriteLineAsync(line1);
117120
}
118121
}
119122
}
@@ -126,7 +129,7 @@ private async Task ProcessConsoleBufferAsync()
126129
finally
127130
{
128131
// if this has failed for any reason, fall everything back to console
129-
_writeEvent = Console.WriteLine;
132+
_writeEvent = Writer.WriteLine;
130133
_consoleBuffer.Writer.TryComplete();
131134
}
132135
}
@@ -147,5 +150,18 @@ public void Dispose()
147150

148151
GC.SuppressFinalize(this);
149152
}
153+
154+
/// <summary>
155+
/// Primarily for testing. Do not call in production.
156+
/// </summary>
157+
/// <remarks>
158+
/// Not called 'FlushAsync' because this does stop processing messages.
159+
/// </remarks>
160+
/// <returns>A task that completes when all buffered messages are drained.</returns>
161+
internal Task CompleteAsync()
162+
{
163+
_consoleBuffer.Writer.TryComplete();
164+
return _consoleBufferReadLoop;
165+
}
150166
}
151167
}

src/WebJobs.Script.WebHost/Diagnostics/LinuxContainerEventGenerator.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Threading.Tasks;
56
using Microsoft.Azure.WebJobs.Script.Config;
67
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Options;
89

910
namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
1011
{
11-
internal class LinuxContainerEventGenerator : LinuxEventGenerator, IDisposable
12+
internal sealed class LinuxContainerEventGenerator : LinuxEventGenerator, IDisposable
1213
{
1314
private const int MaxDetailsLength = 10000;
1415
private static readonly Lazy<LinuxContainerEventGenerator> _Lazy = new Lazy<LinuxContainerEventGenerator>(() => new LinuxContainerEventGenerator(SystemEnvironment.Instance, Console.WriteLine));
@@ -19,7 +20,6 @@ internal class LinuxContainerEventGenerator : LinuxEventGenerator, IDisposable
1920
private string _containerName;
2021
private string _stampName;
2122
private string _tenantId;
22-
private bool _disposed;
2323

2424
public LinuxContainerEventGenerator(IEnvironment environment, IOptions<ConsoleLoggingOptions> consoleLoggingOptions)
2525
{
@@ -29,11 +29,15 @@ public LinuxContainerEventGenerator(IEnvironment environment, IOptions<ConsoleLo
2929
}
3030
else if (!consoleLoggingOptions.Value.BufferEnabled)
3131
{
32-
_writeEvent = Console.WriteLine;
32+
_writeEvent = consoleLoggingOptions.Value.Writer.WriteLine;
3333
}
3434
else
3535
{
36-
_consoleWriter = new BufferedConsoleWriter(consoleLoggingOptions.Value.BufferSize, LogUnhandledException);
36+
_consoleWriter = new BufferedConsoleWriter(consoleLoggingOptions.Value.BufferSize, LogUnhandledException)
37+
{
38+
Writer = consoleLoggingOptions.Value.Writer
39+
};
40+
3741
_writeEvent = _consoleWriter.WriteHandler;
3842
}
3943

@@ -172,22 +176,21 @@ public static void LogEvent(string message, Exception e = null, LogLevel logLeve
172176
eventTimestamp: DateTime.UtcNow);
173177
}
174178

175-
protected virtual void Dispose(bool disposing)
179+
public void Dispose()
176180
{
177-
if (!_disposed)
178-
{
179-
if (disposing)
180-
{
181-
_consoleWriter?.Dispose();
182-
}
183-
_disposed = true;
184-
}
181+
_consoleWriter?.Dispose();
185182
}
186183

187-
public void Dispose()
184+
/// <summary>
185+
/// Primarily for testing. Do not call in production.
186+
/// </summary>
187+
/// <remarks>
188+
/// Not called 'FlushAsync' because this does stop processing messages.
189+
/// </remarks>
190+
/// <returns>A task that completes when all buffered messages are drained.</returns>
191+
internal Task CompleteAsync()
188192
{
189-
Dispose(disposing: true);
190-
GC.SuppressFinalize(this);
193+
return _consoleWriter is { } writer ? writer.CompleteAsync() : Task.CompletedTask;
191194
}
192195
}
193196
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.IO;
6+
47
namespace Microsoft.Azure.WebJobs.Script.Config
58
{
69
public class ConsoleLoggingOptions
@@ -10,18 +13,17 @@ public class ConsoleLoggingOptions
1013
// So in the extreme case, this is about 1 second of buffer and should be less than 3MB
1114
public const int DefaultBufferSize = 8000;
1215

13-
public ConsoleLoggingOptions()
14-
{
15-
LoggingDisabled = false;
16-
BufferEnabled = true;
17-
BufferSize = DefaultBufferSize;
18-
}
19-
2016
public bool LoggingDisabled { get; set; }
2117

2218
// Use BufferSize = 0 to disable the buffer
23-
public bool BufferEnabled { get; internal set; }
19+
public bool BufferEnabled { get; set; } = true;
20+
21+
public int BufferSize { get; set; } = DefaultBufferSize;
2422

25-
public int BufferSize { get; set; }
23+
/// <summary>
24+
/// Gets or sets the <see cref="TextWriter"/> to write logs to.
25+
/// IMPORTANT: this is primarily for unit tests to redirect logs to a different writer.
26+
/// </summary>
27+
internal TextWriter Writer { get; set; } = Console.Out;
2628
}
2729
}

0 commit comments

Comments
 (0)