Communication Layer (WP-1–5): - 8 message patterns with correlation IDs, per-pattern timeouts - Central/Site communication actors, transport heartbeat config - Connection failure handling (no central buffering, debug streams killed) Data Connection Layer (WP-6–14, WP-34): - Connection actor with Become/Stash lifecycle (Connecting/Connected/Reconnecting) - OPC UA + LmxProxy adapters behind IDataConnection - Auto-reconnect, bad quality propagation, transparent re-subscribe - Write-back, tag path resolution with retry, health reporting - Protocol extensibility via DataConnectionFactory Site Runtime (WP-15–25, WP-32–33): - ScriptActor/ScriptExecutionActor (triggers, concurrent execution, blocking I/O dispatcher) - AlarmActor/AlarmExecutionActor (ValueMatch/RangeViolation/RateOfChange, in-memory state) - SharedScriptLibrary (inline execution), ScriptRuntimeContext (API) - ScriptCompilationService (Roslyn, forbidden API enforcement, execution timeout) - Recursion limit (default 10), call direction enforcement - SiteStreamManager (per-subscriber bounded buffers, fire-and-forget) - Debug view backend (snapshot + stream), concurrency serialization - Local artifact storage (4 SQLite tables) Health Monitoring (WP-26–28): - SiteHealthCollector (thread-safe counters, connection state) - HealthReportSender (30s interval, monotonic sequence numbers) - CentralHealthAggregator (offline detection 60s, online recovery) Site Event Logging (WP-29–31): - SiteEventLogger (SQLite, 6 event categories, ISO 8601 UTC) - EventLogPurgeService (30-day retention, 1GB cap) - EventLogQueryService (filters, keyword search, keyset pagination) 541 tests pass, zero warnings.
144 lines
5.1 KiB
C#
144 lines
5.1 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace ScadaLink.SiteEventLogging.Tests;
|
|
|
|
public class SiteEventLoggerTests : IDisposable
|
|
{
|
|
private readonly SiteEventLogger _logger;
|
|
private readonly SqliteConnection _verifyConnection;
|
|
private readonly string _dbPath;
|
|
|
|
public SiteEventLoggerTests()
|
|
{
|
|
_dbPath = Path.Combine(Path.GetTempPath(), $"test_events_{Guid.NewGuid()}.db");
|
|
var options = Options.Create(new SiteEventLogOptions { DatabasePath = _dbPath });
|
|
_logger = new SiteEventLogger(options, NullLogger<SiteEventLogger>.Instance);
|
|
|
|
// Separate connection for verification queries
|
|
_verifyConnection = new SqliteConnection($"Data Source={_dbPath}");
|
|
_verifyConnection.Open();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_verifyConnection.Dispose();
|
|
_logger.Dispose();
|
|
if (File.Exists(_dbPath)) File.Delete(_dbPath);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_InsertsRecord()
|
|
{
|
|
await _logger.LogEventAsync("script", "Error", "inst-1", "ScriptActor:Monitor", "Script failed", "{\"stack\":\"...\"}");
|
|
|
|
using var cmd = _verifyConnection.CreateCommand();
|
|
cmd.CommandText = "SELECT COUNT(*) FROM site_events";
|
|
var count = (long)cmd.ExecuteScalar()!;
|
|
Assert.Equal(1, count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_StoresAllFields()
|
|
{
|
|
await _logger.LogEventAsync("alarm", "Warning", "inst-2", "AlarmActor:TempHigh", "Alarm triggered", "{\"value\":95}");
|
|
|
|
using var cmd = _verifyConnection.CreateCommand();
|
|
cmd.CommandText = "SELECT event_type, severity, instance_id, source, message, details FROM site_events LIMIT 1";
|
|
using var reader = cmd.ExecuteReader();
|
|
Assert.True(reader.Read());
|
|
Assert.Equal("alarm", reader.GetString(0));
|
|
Assert.Equal("Warning", reader.GetString(1));
|
|
Assert.Equal("inst-2", reader.GetString(2));
|
|
Assert.Equal("AlarmActor:TempHigh", reader.GetString(3));
|
|
Assert.Equal("Alarm triggered", reader.GetString(4));
|
|
Assert.Equal("{\"value\":95}", reader.GetString(5));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_NullableFieldsAllowed()
|
|
{
|
|
await _logger.LogEventAsync("deployment", "Info", null, "DeploymentManager", "Deployed instance");
|
|
|
|
using var cmd = _verifyConnection.CreateCommand();
|
|
cmd.CommandText = "SELECT instance_id, details FROM site_events LIMIT 1";
|
|
using var reader = cmd.ExecuteReader();
|
|
Assert.True(reader.Read());
|
|
Assert.True(reader.IsDBNull(0));
|
|
Assert.True(reader.IsDBNull(1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_StoresIso8601UtcTimestamp()
|
|
{
|
|
await _logger.LogEventAsync("connection", "Info", null, "DCL", "Connected");
|
|
|
|
using var cmd = _verifyConnection.CreateCommand();
|
|
cmd.CommandText = "SELECT timestamp FROM site_events LIMIT 1";
|
|
var ts = (string)cmd.ExecuteScalar()!;
|
|
var parsed = DateTimeOffset.Parse(ts);
|
|
Assert.Equal(TimeSpan.Zero, parsed.Offset);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_ThrowsOnEmptyEventType()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
_logger.LogEventAsync("", "Info", null, "Source", "Message"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_ThrowsOnEmptySeverity()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
_logger.LogEventAsync("script", "", null, "Source", "Message"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_ThrowsOnEmptySource()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
_logger.LogEventAsync("script", "Info", null, "", "Message"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_ThrowsOnEmptyMessage()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
_logger.LogEventAsync("script", "Info", null, "Source", ""));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LogEventAsync_MultipleEvents_AutoIncrementIds()
|
|
{
|
|
await _logger.LogEventAsync("script", "Info", null, "S1", "First");
|
|
await _logger.LogEventAsync("script", "Info", null, "S2", "Second");
|
|
await _logger.LogEventAsync("script", "Info", null, "S3", "Third");
|
|
|
|
using var cmd = _verifyConnection.CreateCommand();
|
|
cmd.CommandText = "SELECT id FROM site_events ORDER BY id";
|
|
using var reader = cmd.ExecuteReader();
|
|
var ids = new List<long>();
|
|
while (reader.Read()) ids.Add(reader.GetInt64(0));
|
|
|
|
Assert.Equal(3, ids.Count);
|
|
Assert.True(ids[0] < ids[1] && ids[1] < ids[2]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AllEventTypes_Accepted()
|
|
{
|
|
var types = new[] { "script", "alarm", "deployment", "connection", "store_and_forward", "instance_lifecycle" };
|
|
foreach (var t in types)
|
|
{
|
|
await _logger.LogEventAsync(t, "Info", null, "Test", $"Event type: {t}");
|
|
}
|
|
|
|
using var cmd = _verifyConnection.CreateCommand();
|
|
cmd.CommandText = "SELECT COUNT(DISTINCT event_type) FROM site_events";
|
|
var count = (long)cmd.ExecuteScalar()!;
|
|
Assert.Equal(6, count);
|
|
}
|
|
}
|