78 lines
2.7 KiB
C#
78 lines
2.7 KiB
C#
using Omika.Diagnostics.Data;
|
|
using Omika.Diagnostics.Import;
|
|
using Omika.Diagnostics.Tests.Fakes;
|
|
|
|
namespace Omika.Diagnostics.Tests;
|
|
|
|
/// <summary>§21 — Checkpoint : reprise après restart / PostgreSQL down, jamais depuis zéro.</summary>
|
|
public class JsonlDiagnosticSourceCheckpointTests
|
|
{
|
|
[Fact]
|
|
public void ReadNext_Commit_ThenResumeFromCheckpoint()
|
|
{
|
|
string dir = CreateTempDir();
|
|
try
|
|
{
|
|
var r1 = DiagnosticTestData.Record(Guid.NewGuid(), "A");
|
|
var r2 = DiagnosticTestData.Record(Guid.NewGuid(), "B");
|
|
var r3 = DiagnosticTestData.Record(Guid.NewGuid(), "C");
|
|
|
|
string file = Path.Combine(dir, "omika-mobile-diagnostic-20250101-001.jsonl");
|
|
File.WriteAllLines(file, new[]
|
|
{
|
|
DiagnosticTestData.SerializeLine(r1),
|
|
DiagnosticTestData.SerializeLine(r2),
|
|
DiagnosticTestData.SerializeLine(r3)
|
|
});
|
|
|
|
var source = new JsonlDiagnosticSource(dir, "import-checkpoint.json");
|
|
|
|
DiagnosticJsonlReadResult first = source.ReadNext(null, 2);
|
|
Assert.Equal(2, first.Records.Count);
|
|
Assert.Equal(1, first.NextPosition!.LineIndex);
|
|
source.Commit(first.NextPosition);
|
|
|
|
DiagnosticImportCheckpoint? checkpoint = source.LoadCheckpoint();
|
|
Assert.NotNull(checkpoint);
|
|
Assert.Equal(1, checkpoint!.LineIndex);
|
|
|
|
DiagnosticJsonlReadResult second = source.ReadNext(checkpoint, 10);
|
|
Assert.Single(second.Records);
|
|
Assert.Equal(r3.Event.EventId, second.Records[0].Event.EventId);
|
|
}
|
|
finally
|
|
{
|
|
Directory.Delete(dir, recursive: true);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void PoisonLine_IsSkipped_CursorAdvances()
|
|
{
|
|
string dir = CreateTempDir();
|
|
try
|
|
{
|
|
var r1 = DiagnosticTestData.Record(Guid.NewGuid(), "A");
|
|
string file = Path.Combine(dir, "omika-mobile-diagnostic-20250101-001.jsonl");
|
|
File.WriteAllLines(file, new[] { "{ json invalide", DiagnosticTestData.SerializeLine(r1) });
|
|
|
|
var source = new JsonlDiagnosticSource(dir, "import-checkpoint.json");
|
|
DiagnosticJsonlReadResult read = source.ReadNext(null, 10);
|
|
|
|
Assert.Single(read.Records);
|
|
Assert.Equal(1, read.NextPosition!.LineIndex); // la ligne poison ne bloque pas
|
|
}
|
|
finally
|
|
{
|
|
Directory.Delete(dir, recursive: true);
|
|
}
|
|
}
|
|
|
|
private static string CreateTempDir()
|
|
{
|
|
string dir = Path.Combine(Path.GetTempPath(), "omika-diag-test-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(dir);
|
|
return dir;
|
|
}
|
|
}
|