Skip to content

Commit

Permalink
Address unit test timing and setting Console.Out
Browse files Browse the repository at this point in the history
  • Loading branch information
jviau committed Feb 7, 2025
1 parent 22d7430 commit 46c925c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 90 deletions.
24 changes: 20 additions & 4 deletions src/WebJobs.Script.WebHost/Diagnostics/BufferedConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Channels;
Expand Down Expand Up @@ -50,6 +51,21 @@ internal BufferedConsoleWriter(int bufferSize, Action<Exception> exceptionHandle
_exceptionhandler = exceptionHandler;
}

public TextWriter Writer { get; init; } = Console.Out;

/// <summary>
/// Primarily for testing. Do not call in production.
/// </summary>
/// <remarks>
/// Not called 'FlushAsync' because this does stop processing messages.
/// </remarks>
/// <returns>A task that completes when all buffered messages are drained.</returns>
public Task CompleteAsync()
{
_consoleBuffer.Writer.TryComplete();
return _consoleBufferReadLoop;
}

public void WriteHandler(string evt)
{
_writeEvent(evt);
Expand All @@ -65,7 +81,7 @@ private void WriteToConsoleBuffer(string evt)
// If the wait failed, write to the console. Otherwise, try the writing again - if that fails, write to the console.
if (waitFailed || !_consoleBuffer.Writer.TryWrite(evt))
{
Console.WriteLine(evt);
Writer.WriteLine(evt);
}
}
}
Expand Down Expand Up @@ -108,12 +124,12 @@ private async Task ProcessConsoleBufferAsync()
}

_writeResetEvent.Set();
Console.Write(builder.ToString());
Writer.Write(builder.ToString());
builder.Clear();
}
else
{
Console.WriteLine(line1);
Writer.WriteLine(line1);
}
}
}
Expand All @@ -126,7 +142,7 @@ private async Task ProcessConsoleBufferAsync()
finally
{
// if this has failed for any reason, fall everything back to console
_writeEvent = Console.WriteLine;
_writeEvent = Writer.WriteLine;
_consoleBuffer.Writer.TryComplete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
{
internal class LinuxContainerEventGenerator : LinuxEventGenerator, IDisposable
internal sealed class LinuxContainerEventGenerator : LinuxEventGenerator, IDisposable
{
private const int MaxDetailsLength = 10000;
private static readonly Lazy<LinuxContainerEventGenerator> _Lazy = new Lazy<LinuxContainerEventGenerator>(() => new LinuxContainerEventGenerator(SystemEnvironment.Instance, Console.WriteLine));
Expand All @@ -29,11 +30,15 @@ public LinuxContainerEventGenerator(IEnvironment environment, IOptions<ConsoleLo
}
else if (!consoleLoggingOptions.Value.BufferEnabled)
{
_writeEvent = Console.WriteLine;
_writeEvent = consoleLoggingOptions.Value.Writer.WriteLine;
}
else
{
_consoleWriter = new BufferedConsoleWriter(consoleLoggingOptions.Value.BufferSize, LogUnhandledException);
_consoleWriter = new BufferedConsoleWriter(consoleLoggingOptions.Value.BufferSize, LogUnhandledException)
{
Writer = consoleLoggingOptions.Value.Writer
};

_writeEvent = _consoleWriter.WriteHandler;
}

Expand Down Expand Up @@ -172,22 +177,21 @@ public static void LogEvent(string message, Exception e = null, LogLevel logLeve
eventTimestamp: DateTime.UtcNow);
}

protected virtual void Dispose(bool disposing)
/// <summary>
/// Primarily for testing. Do not call in production.
/// </summary>
/// <remarks>
/// Not called 'FlushAsync' because this does stop processing messages.
/// </remarks>
/// <returns>A task that completes when all buffered messages are drained.</returns>
public Task CompleteAsync()
{
if (!_disposed)
{
if (disposing)
{
_consoleWriter?.Dispose();
}
_disposed = true;
}
return _consoleWriter is { } writer ? writer.CompleteAsync() : Task.CompletedTask;
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
_consoleWriter?.Dispose();
}
}
}
20 changes: 11 additions & 9 deletions src/WebJobs.Script/Config/ConsoleLoggingOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.IO;

namespace Microsoft.Azure.WebJobs.Script.Config
{
public class ConsoleLoggingOptions
Expand All @@ -10,18 +13,17 @@ public class ConsoleLoggingOptions
// So in the extreme case, this is about 1 second of buffer and should be less than 3MB
public const int DefaultBufferSize = 8000;

public ConsoleLoggingOptions()
{
LoggingDisabled = false;
BufferEnabled = true;
BufferSize = DefaultBufferSize;
}

public bool LoggingDisabled { get; set; }

// Use BufferSize = 0 to disable the buffer
public bool BufferEnabled { get; internal set; }
public bool BufferEnabled { get; set; } = true;

public int BufferSize { get; set; } = DefaultBufferSize;

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

0 comments on commit 46c925c

Please sign in to comment.