54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using StellaOps.Signals.Parsing;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Signals.Reachability.Tests;
|
|
|
|
public sealed class RuntimeFactsNdjsonReaderTests
|
|
{
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public async Task ReadAsync_ParsesLines()
|
|
{
|
|
var ndjson = """
|
|
{"symbolId":"sym::foo","hitCount":2,"processId":10,"processName":"api"}
|
|
{"symbolId":"sym::bar","codeId":"elf:abcd","loaderBase":"0x1000","metadata":{"thread":"bg"}}
|
|
""";
|
|
|
|
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(ndjson));
|
|
var events = await RuntimeFactsNdjsonReader.ReadAsync(stream, gzipEncoded: false, CancellationToken.None);
|
|
|
|
events.Should().HaveCount(2);
|
|
events[0].SymbolId.Should().Be("sym::foo");
|
|
events[0].ProcessId.Should().Be(10);
|
|
events[0].ProcessName.Should().Be("api");
|
|
events[1].LoaderBase.Should().Be("0x1000");
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public async Task ReadAsync_HandlesGzip()
|
|
{
|
|
var ndjson = """
|
|
{"symbolId":"sym::foo"}
|
|
""";
|
|
await using var compressed = new MemoryStream();
|
|
await using (var gzip = new GZipStream(compressed, CompressionLevel.Optimal, leaveOpen: true))
|
|
await using (var writer = new StreamWriter(gzip, Encoding.UTF8, leaveOpen: true))
|
|
{
|
|
await writer.WriteAsync(ndjson);
|
|
using StellaOps.TestKit;
|
|
}
|
|
|
|
compressed.Position = 0;
|
|
|
|
var events = await RuntimeFactsNdjsonReader.ReadAsync(compressed, gzipEncoded: true, CancellationToken.None);
|
|
events.Should().HaveCount(1);
|
|
}
|
|
}
|