Merged PR 2331: fix custom logging when using the TestingEngine

Fix logging when using TestEngine directory so the logger provided to the test engine contains the actual test output.

Related work items: #4066
This commit is contained in:
Chris Lovett 2020-06-06 02:21:36 +00:00
Родитель 4878e3a64c
Коммит 8306128061
4 изменённых файлов: 104 добавлений и 4 удалений

Просмотреть файл

@ -26,6 +26,11 @@ namespace Microsoft.Coyote.IO
/// </summary>
private readonly object Lock;
/// <summary>
/// Optional logger provided by the user to delegate logging to.
/// </summary>
internal TextWriter UserLogger { get; set; }
/// <summary>
/// When overridden in a derived class, returns the character encoding in which the
/// output is written.
@ -52,6 +57,10 @@ namespace Microsoft.Coyote.IO
lock (this.Lock)
{
this.Builder.Append(value);
if (this.UserLogger != null)
{
this.UserLogger.WriteLine(value);
}
}
}
catch (ObjectDisposedException)
@ -70,6 +79,10 @@ namespace Microsoft.Coyote.IO
lock (this.Lock)
{
this.Builder.Append(value);
if (this.UserLogger != null)
{
this.UserLogger.WriteLine(value);
}
}
}
catch (ObjectDisposedException)
@ -89,6 +102,10 @@ namespace Microsoft.Coyote.IO
lock (this.Lock)
{
this.Builder.AppendLine(value);
if (this.UserLogger != null)
{
this.UserLogger.WriteLine(value);
}
}
}
catch (ObjectDisposedException)

Просмотреть файл

@ -76,6 +76,11 @@ namespace Microsoft.Coyote.SystematicTesting
/// </remarks>
private TextWriter Logger;
/// <summary>
/// The default logger that is used during testing.
/// </summary>
private readonly TextWriter DefaultLogger;
/// <summary>
/// The profiler.
/// </summary>
@ -204,7 +209,8 @@ namespace Microsoft.Coyote.SystematicTesting
this.Configuration = configuration;
this.TestMethodInfo = testMethodInfo;
this.Logger = new ConsoleLogger();
this.DefaultLogger = new ConsoleLogger();
this.Logger = this.DefaultLogger;
this.ErrorReporter = new ErrorReporter(configuration, this.Logger);
this.Profiler = new Profiler();
@ -488,12 +494,21 @@ namespace Microsoft.Coyote.SystematicTesting
if (!this.Configuration.IsVerbose)
{
runtimeLogger = new InMemoryLogger();
if (this.Logger != this.DefaultLogger)
{
runtimeLogger.UserLogger = this.Logger;
}
runtime.SetLogger(runtimeLogger);
var writer = TextWriter.Null;
Console.SetOut(writer);
Console.SetError(writer);
}
else if (this.Logger != this.DefaultLogger)
{
runtime.SetLogger(this.Logger);
}
this.InitializeCustomLogging(runtime);
@ -949,9 +964,15 @@ namespace Microsoft.Coyote.SystematicTesting
/// <summary>
/// Installs the specified <see cref="TextWriter"/>.
/// </summary>
public void SetLogger(TextWriter logger)
/// <param name="logger">The logger to install.</param>
/// <returns>The previously installed logger.</returns>
public TextWriter SetLogger(TextWriter logger)
{
this.Logger.Dispose();
TextWriter oldLoger = null;
if (this.Logger != this.DefaultLogger)
{
oldLoger = this.Logger;
}
if (logger is null)
{
@ -963,6 +984,7 @@ namespace Microsoft.Coyote.SystematicTesting
}
this.ErrorReporter.Logger = logger;
return oldLoger;
}
}
}

Просмотреть файл

@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Coyote.Runtime;
using Microsoft.Coyote.Tests.Common;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Coyote.SystematicTesting.Tests.Tasks
{
public class CustomTaskRuntimeLogTests : BaseTest
{
public CustomTaskRuntimeLogTests(ITestOutputHelper output)
: base(output)
{
}
public override bool SystematicTest => true;
[Fact(Timeout = 5000)]
public void TestCustomLogger()
{
StringWriter log = new StringWriter();
var config = Configuration.Create().WithVerbosityEnabled().WithTestingIterations(3);
TestingEngine engine = TestingEngine.Create(config, (ICoyoteRuntime runtime) =>
{
runtime.Logger.WriteLine("Hi mom!");
});
engine.SetLogger(log);
engine.Run();
var result = log.ToString();
result = RemoveNonDeterministicValuesFromReport(result);
var expected = RemoveNonDeterministicValuesFromReport(@"... Task 0 is using 'random' strategy (seed:4005173804).
..... Iteration #1
<TestLog> Running test.
Hi mom!
..... Iteration #2
<TestLog> Running test.
Hi mom!
..... Iteration #3
<TestLog> Running test.
Hi mom!
");
Assert.Equal(expected, result);
}
}
}

Просмотреть файл

@ -9,9 +9,17 @@ permalink: /learn/ref/Microsoft.Coyote.SystematicTesting/TestingEngine/SetLogger
Installs the specified TextWriter.
```csharp
public void SetLogger(TextWriter logger)
public TextWriter SetLogger(TextWriter logger)
```
| parameter | description |
| --- | --- |
| logger | The logger to install. |
## Return Value
The previously installed logger.
## See Also
* class [TestingEngine](../TestingEngineType)