Skip to content

Commit

Permalink
TypeTester logging. (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun authored Feb 1, 2024
1 parent e60f8bf commit 1d28425
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v4.1.1
- *Fixed:* The `TypeTester` was not correctly capturing and outputting any of the logging, and also (as a result) the `ExpectLogContains` was not functioning as expected.

## v4.1.0
- *Enhancement:* Removed the `FunctionsStartup` constraint for `TEntryPoint` to enable more generic usage.
- *Enhancement:* Enable `Microsoft.Azure.Functions.Worker.HttpTriggerAttribute` (new [_isolated_](https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide) function support), in addition to existing `Microsoft.Azure.WebJobs.HttpTriggerAttribute` (existing [_in-process_](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library) function support), within `HttpTriggerTester`.
Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>4.1.0</Version>
<Version>4.1.1</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
28 changes: 21 additions & 7 deletions src/UnitTestEx/Hosting/TypeTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using UnitTestEx.Abstractions;
using UnitTestEx.Assertors;
Expand Down Expand Up @@ -81,10 +83,11 @@ public async Task<VoidAssertor> RunAsync(Func<T, Task> function)
}

await Task.Delay(TestSetUp.TaskDelayMilliseconds).ConfigureAwait(false);
LogResult(ex, sw.Elapsed.TotalMilliseconds);
var logs = Owner.SharedState.GetLoggerMessages();
LogResult(ex, sw.Elapsed.TotalMilliseconds, logs);
LogTrailer();

await ExpectationsArranger.AssertAsync(null, ex).ConfigureAwait(false);
await ExpectationsArranger.AssertAsync(logs, ex).ConfigureAwait(false);

return new VoidAssertor(Owner, ex);
}
Expand Down Expand Up @@ -128,7 +131,8 @@ public async Task<ValueAssertor<TValue>> RunAsync<TValue>(Func<T, Task<TValue>>
}

await Task.Delay(TestSetUp.TaskDelayMilliseconds).ConfigureAwait(false);
LogResult(ex, sw.Elapsed.TotalMilliseconds);
var logs = Owner.SharedState.GetLoggerMessages();
LogResult(ex, sw.Elapsed.TotalMilliseconds, logs);

if (ex == null)
{
Expand All @@ -146,7 +150,7 @@ public async Task<ValueAssertor<TValue>> RunAsync<TValue>(Func<T, Task<TValue>>

LogTrailer();

await ExpectationsArranger.AssertAsync(null, ex).ConfigureAwait(false);
await ExpectationsArranger.AssertValueAsync(logs, result, ex).ConfigureAwait(false);

return new ValueAssertor<TValue>(Owner, result, ex);
}
Expand All @@ -159,15 +163,25 @@ private void LogHeader()
Implementor.WriteLine("");
Implementor.WriteLine("TYPE TESTER...");
Implementor.WriteLine($"Type: {typeof(T).Name} [{typeof(T).FullName}]");
Implementor.WriteLine("");
Implementor.WriteLine("LOGGING >");
}

/// <summary>
/// Log the elapsed execution time.
/// </summary>
private void LogResult(Exception? ex, double ms)
private void LogResult(Exception? ex, double ms, IEnumerable<string?>? logs)
{
Implementor.WriteLine("");
Implementor.WriteLine("LOGGING >");
if (logs is not null && logs.Any())
{
foreach (var msg in logs)
{
Implementor.WriteLine(msg);
}
}
else
Implementor.WriteLine("None.");

Implementor.WriteLine("");
Implementor.WriteLine("RESULT >");
Implementor.WriteLine($"Elapsed (ms): {ms}");
Expand Down
8 changes: 6 additions & 2 deletions tests/UnitTestEx.Function/ProductFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ namespace UnitTestEx.Function
public class ProductFunction
{
private readonly HttpClient _httpClient;
private readonly ILogger<ProductFunction> _logger;

public ProductFunction(IHttpClientFactory clientFactory)
public ProductFunction(IHttpClientFactory clientFactory, ILogger<ProductFunction> logger)
{
_httpClient = clientFactory.CreateClient("XXX");
_logger = logger;
}

[FunctionName("ProductFunction")]
Expand All @@ -45,8 +47,10 @@ private async Task<IActionResult> Logic(string id, ILogger log)
}

[FunctionName("TimerTriggered")]
public Task DailyRun([TimerTrigger("0 0 0 */1 * *", RunOnStartup = true)] TimerInfo _)
public Task DailyRun([TimerTrigger("0 0 0 */1 * *", RunOnStartup = true)] TimerInfo _, ILogger otherLogger)
{
_logger.LogInformation("C# Timer trigger function executed (DI).");
otherLogger.LogInformation("C# Timer trigger function executed (method).");
return Task.CompletedTask;
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/UnitTestEx.IsolatedFunction/TimerFunction.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace UnitTestEx.IsolatedFunction
{
public class TimerFunction
{
private readonly HttpClient _httpClient;
private readonly ILogger _logger;

public TimerFunction(IHttpClientFactory clientFactory)
public TimerFunction(IHttpClientFactory clientFactory, ILogger<TimerFunction> logger)
{
_httpClient = clientFactory.CreateClient("XXX");
_logger = logger;
}

[Function("TimerTriggerFunction")]
public async Task Run([TimerTrigger("0 */5 * * * *" /*, RunOnStartup = true */)] string _)
{
_logger.LogInformation("C# Timer trigger function executed.");
var hr = await _httpClient.GetAsync($"products/123").ConfigureAwait(false);
hr.EnsureSuccessStatusCode();
}
Expand Down
4 changes: 3 additions & 1 deletion tests/UnitTestEx.NUnit.Test/ProductFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public void TimerTriggered()
{
using var test = FunctionTester.Create<Startup>();
test.Type<ProductFunction>()
.Run(f => f.DailyRun(new TimerInfo(new DailySchedule("2:00:00"), It.IsAny<ScheduleStatus>(), false)))
.ExpectLogContains("(DI)")
.ExpectLogContains("(method)")
.Run(f => f.DailyRun(new TimerInfo(new DailySchedule("2:00:00"), It.IsAny<ScheduleStatus>(), false), test.Logger))
.AssertSuccess();
}
}
Expand Down

0 comments on commit 1d28425

Please sign in to comment.