Fix live log reads and timestamp service output
This commit is contained in:
1 parent
bb1f2d530d
commit
75ef1ed69c
17 files changed
+198
-6
No files matched your search
@@ -1,7 +1,9 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SmartB2B.Optima.Adapter;
|
||||
using SmartB2B.Sync.Contracts;
|
||||
using SmartB2B.Sync.Service.Diagnostics;
|
||||
using SmartB2B.Sync.Service.Orders;
|
||||
using SmartB2B.Sync.Service.Rpc;
|
||||
using WampSharp.V2.Core.Contracts;
|
||||
@@ -26,7 +28,9 @@ var tests = new (string Name, Func<Task> Run)[]
|
||||
("Wynik Optimy zachowuje kwargs WAMP", HandlerReturnsKeywordResult),
|
||||
("Operacje zamówień są serializowane", ConcurrentOrdersAreSerialized),
|
||||
("Fasada COM działa na jednym wątku STA", FacadeRunsOnSingleStaThread),
|
||||
("Adapter serializuje bezpośrednie wywołania COM", AdapterSerializesComCalls)
|
||||
("Adapter serializuje bezpośrednie wywołania COM", AdapterSerializesComCalls),
|
||||
("Diagnostyka czyta log podczas zapisu", DiagnosticsReadsOpenLogs),
|
||||
("Każdy wiersz logu ma czas ISO", ConsoleOutputHasIsoTimestamps)
|
||||
};
|
||||
|
||||
var failures = 0;
|
||||
@@ -111,6 +115,84 @@ static async Task HandlerMapsOptimaContract()
|
||||
AssertEqual(123m, request.Items[0].UnitPriceGross, "brutto");
|
||||
}
|
||||
|
||||
static async Task DiagnosticsReadsOpenLogs()
|
||||
{
|
||||
var directory = Path.Combine(Path.GetTempPath(), $"smartb2b-log-test-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directory);
|
||||
try
|
||||
{
|
||||
var diagnostics = new DiagnosticsHandler("test", "1", ".NET", "test", null, directory, "service");
|
||||
var invocation = new RpcInvocation([], new Dictionary<string, object?> { ["date"] = "2026-09-18" });
|
||||
|
||||
foreach (var stream in new[] { "out", "err" })
|
||||
{
|
||||
var path = Path.Combine(directory, $"service_20260918.{stream}.log");
|
||||
await using var writer = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
|
||||
var content = $"Log {stream} zażółć";
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(content);
|
||||
await writer.WriteAsync(bytes);
|
||||
await writer.FlushAsync();
|
||||
|
||||
var result = stream == "out"
|
||||
? await diagnostics.GetLogAsync(invocation, CancellationToken.None)
|
||||
: await diagnostics.GetErrorLogAsync(invocation, CancellationToken.None);
|
||||
AssertEqual(content, result, $"odczyt otwartego pliku {stream}");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(directory, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
static Task ConsoleOutputHasIsoTimestamps()
|
||||
{
|
||||
var originalOut = Console.Out;
|
||||
var originalError = Console.Error;
|
||||
using var output = new StringWriter();
|
||||
using var error = new StringWriter();
|
||||
try
|
||||
{
|
||||
Console.SetOut(output);
|
||||
Console.SetError(error);
|
||||
ConsoleLogger.InstallTimestampedOutput();
|
||||
|
||||
Console.Write("pierw");
|
||||
Console.WriteLine("szy\ndrugi");
|
||||
ConsoleLogger.Error("błąd\nszczegóły");
|
||||
|
||||
AssertTimestampedLines(output.ToString(), ["pierwszy", "drugi"]);
|
||||
AssertTimestampedLines(error.ToString(), ["błąd", "szczegóły"]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.SetOut(originalOut);
|
||||
Console.SetError(originalError);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
static void AssertTimestampedLines(string output, string[] expectedMessages)
|
||||
{
|
||||
var lines = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||
AssertEqual(expectedMessages.Length, lines.Length, "liczba wierszy logu");
|
||||
for (var index = 0; index < lines.Length; index++)
|
||||
{
|
||||
var line = lines[index].TrimEnd('\r');
|
||||
var separator = line.IndexOf(' ');
|
||||
if (separator < 0 ||
|
||||
!DateTimeOffset.TryParseExact(line[..separator], "O", CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None, out var timestamp) ||
|
||||
timestamp.Offset != TimeSpan.Zero)
|
||||
{
|
||||
throw new InvalidOperationException($"Brak czasu ISO UTC w wierszu: {line}");
|
||||
}
|
||||
|
||||
AssertEqual(expectedMessages[index], line[(separator + 1)..], "treść wiersza logu");
|
||||
}
|
||||
}
|
||||
|
||||
static async Task HandlerMapsOptionalExtraFields()
|
||||
{
|
||||
using var adapter = new CapturingAdapter();
|
||||
|
||||
Reference in new issue
Block a user