Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Historian/ServerHistorianOptionsTests.cs
T
Joseph Doherty 6669fae7e5 feat(historian): ServerHistorian:MaxRawValuesPerRead caps raw pages and backend reads
Review R08:1.1: an uncapped HistoryRead-Raw (NumValuesPerNode = 0) read a
tag's whole window into memory on every backend (SQL TOP(int.MaxValue),
QuestDB LIMIT, Gateway max_values = int.MaxValue + a second snapshot copy).

New ServerHistorian:MaxRawValuesPerRead (default 10 000; <= 0 unbounded with
a Validate() warning; 1 raised to 2). Each backend data source takes it,
reports it as IHistorianDataSource.MaxRawValuesPerRead (the Gateway: min
with a strict gateway's learned ceiling), so the node manager's existing
RawPageLimit paging serves N = 0 and N > cap as pages of at most the cap
with a continuation point. Independently, a backend raw read asking for 0
or more than the cap reads cap + 1 rows and is REFUSED
(HistoryReadLimitExceededException => BadTooManyOperations) when the window
holds more - never answered truncated, since the server's paging reads a
short answer as the last page. Bound through the Host registry, the
SqlServer/QuestDb providers and GatewayHistorian.CreateDataSource.

Tests: options (default/binding/warnings), provider binding, Gateway unit +
through a real SDK node manager (uncapped -> 999/998/998/605 + CPs), and a
conformance case for the SQL backends.

Refs #736

Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
2026-10-22 06:29:10 -04:00

232 lines
10 KiB
C#

using Microsoft.Extensions.Configuration;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.Historian;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Historian;
/// <summary>
/// Covers the gateway-shaped <see cref="ServerHistorianOptions.Validate"/> warnings: a disabled or
/// correctly-configured historian is silent; an enabled one with a blank <c>Endpoint</c> or blank
/// <c>ApiKey</c> surfaces an operator-facing warning (carrying no secret value text).
/// </summary>
public sealed class ServerHistorianOptionsTests
{
[Fact]
public void Disabled_yields_no_warnings()
=> Assert.Empty(new ServerHistorianOptions { Enabled = false }.Validate());
[Fact]
public void Enabled_without_endpoint_warns()
{
var w = new ServerHistorianOptions { Enabled = true, Endpoint = "", ApiKey = "histgw_x_y" }.Validate();
Assert.Contains(w, m => m.Contains("Endpoint"));
}
[Fact]
public void Enabled_with_malformed_endpoint_warns()
{
// archreview 06/S-11 warning parity: a non-empty but unparseable / non-absolute Endpoint warns
// too, so the Runtime-side registration logs describe malformed endpoints (not just empty ones),
// keeping this surface consistent with the Host's fail-fast ServerHistorianOptionsValidator.
var w = new ServerHistorianOptions { Enabled = true, Endpoint = "not a uri", ApiKey = "histgw_x_y" }.Validate();
Assert.Contains(w, m => m.Contains("Endpoint"));
}
[Fact]
public void Enabled_without_apikey_warns()
{
var w = new ServerHistorianOptions { Enabled = true, Endpoint = "https://h:5222", ApiKey = "" }.Validate();
Assert.Contains(w, m => m.Contains("ApiKey"));
}
[Fact]
public void Valid_config_is_clean()
=> Assert.Empty(new ServerHistorianOptions { Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y" }.Validate());
[Fact]
public void Enabled_with_nonpositive_MaxTieClusterOverfetch_warns()
{
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", MaxTieClusterOverfetch = 0 }
.Validate();
Assert.Contains(w, m => m.Contains("MaxTieClusterOverfetch"));
}
// ── S3 HistoryRead concurrency knobs (arch-review 03/S3) ────────────────────────────────────
[Fact]
public void HistoryRead_knob_defaults()
{
var o = new ServerHistorianOptions();
Assert.Equal(4, o.HistoryReadBatchParallelism);
Assert.Equal(16, o.MaxConcurrentHistoryReadBatches);
Assert.Equal(TimeSpan.FromSeconds(60), o.HistoryReadDeadline);
}
[Fact]
public void Enabled_with_nonpositive_HistoryReadBatchParallelism_warns()
{
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", HistoryReadBatchParallelism = 0 }
.Validate();
Assert.Contains(w, m => m.Contains("HistoryReadBatchParallelism"));
}
[Fact]
public void Enabled_with_nonpositive_MaxConcurrentHistoryReadBatches_warns()
{
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", MaxConcurrentHistoryReadBatches = 0 }
.Validate();
Assert.Contains(w, m => m.Contains("MaxConcurrentHistoryReadBatches"));
}
[Fact]
public void Enabled_with_nonpositive_HistoryReadDeadline_warns()
{
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", HistoryReadDeadline = TimeSpan.Zero }
.Validate();
Assert.Contains(w, m => m.Contains("HistoryReadDeadline"));
}
[Fact]
public void MaxTimeWeightedRawRows_defaults_to_one_million_and_warns_when_nonpositive()
{
Assert.Equal(1_000_000, new ServerHistorianOptions().MaxTimeWeightedRawRows);
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", MaxTimeWeightedRawRows = 0 }
.Validate();
Assert.Contains(w, m => m.Contains("MaxTimeWeightedRawRows"));
}
/// <summary>
/// #674: the HistoryUpdate future-skew bound defaults to one day and binds from configuration as a TimeSpan;
/// a non-positive value is a deliberate "disabled", not a misconfiguration, so it does not warn.
/// </summary>
/// <summary>#691: the ReturnBounds kill switch defaults on and binds off; neither value warns.</summary>
[Fact]
public void HonorReturnBounds_defaults_to_true_and_binds_false_silently()
{
Assert.True(new ServerHistorianOptions().HonorReturnBounds);
var bound = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { ["ServerHistorian:HonorReturnBounds"] = "false" })
.Build()
.GetSection(ServerHistorianOptions.SectionName)
.Get<ServerHistorianOptions>();
Assert.False(bound!.HonorReturnBounds);
Assert.Empty(new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", HonorReturnBounds = false }.Validate());
}
/// <summary>#694: the events horizon defaults off, binds as a TimeSpan, and only a negative value warns.</summary>
[Fact]
public void EventReadHorizon_defaults_off_binds_and_only_a_negative_value_warns()
{
Assert.Equal(TimeSpan.Zero, new ServerHistorianOptions().EventReadHorizon);
var bound = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { ["ServerHistorian:EventReadHorizon"] = "3650.00:00:00" })
.Build()
.GetSection(ServerHistorianOptions.SectionName)
.Get<ServerHistorianOptions>();
Assert.Equal(TimeSpan.FromDays(3650), bound!.EventReadHorizon);
Assert.Empty(new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", EventReadHorizon = TimeSpan.FromDays(30) }.Validate());
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", EventReadHorizon = TimeSpan.FromDays(-1) }.Validate();
Assert.Contains(w, m => m.Contains("EventReadHorizon"));
}
[Fact]
public void HistoryUpdateMaxFutureSkew_defaults_to_one_day_binds_and_zero_is_silent()
{
Assert.Equal(TimeSpan.FromDays(1), new ServerHistorianOptions().HistoryUpdateMaxFutureSkew);
var bound = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["ServerHistorian:HistoryUpdateMaxFutureSkew"] = "00:05:00",
})
.Build()
.GetSection(ServerHistorianOptions.SectionName)
.Get<ServerHistorianOptions>();
Assert.Equal(TimeSpan.FromMinutes(5), bound!.HistoryUpdateMaxFutureSkew);
var w = new ServerHistorianOptions
{ Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", HistoryUpdateMaxFutureSkew = TimeSpan.Zero }
.Validate();
Assert.Empty(w);
}
[Fact]
public void Disabled_with_nonpositive_HistoryRead_knobs_is_silent()
{
var w = new ServerHistorianOptions
{
Enabled = false,
HistoryReadBatchParallelism = 0,
MaxConcurrentHistoryReadBatches = 0,
HistoryReadDeadline = TimeSpan.Zero,
}.Validate();
Assert.Empty(w);
}
// ── MaxRawValuesPerRead (R08:1.1, #736) ─────────────────────────────────────────────────────
[Fact]
public void MaxRawValuesPerRead_defaults_to_ten_thousand_and_binds()
{
var o = new ServerHistorianOptions();
Assert.Equal(10_000, o.MaxRawValuesPerRead);
Assert.Equal(10_000, ServerHistorianOptions.DefaultMaxRawValuesPerRead);
Assert.Equal(10_000, o.EffectiveMaxRawValuesPerRead);
var bound = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { ["ServerHistorian:MaxRawValuesPerRead"] = "2500" })
.Build()
.GetSection(ServerHistorianOptions.SectionName)
.Get<ServerHistorianOptions>()!;
Assert.Equal(2500, bound.MaxRawValuesPerRead);
Assert.Equal(2500, bound.EffectiveMaxRawValuesPerRead);
Assert.Empty(new ServerHistorianOptions
{
Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", MaxRawValuesPerRead = 2500,
}.Validate());
}
[Theory]
[InlineData(0)]
[InlineData(-5)]
public void Nonpositive_MaxRawValuesPerRead_is_unbounded_and_warns_when_enabled(int value)
{
var o = new ServerHistorianOptions
{
Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", MaxRawValuesPerRead = value,
};
Assert.Equal(0, o.EffectiveMaxRawValuesPerRead);
var w = o.Validate();
var m = Assert.Single(w);
Assert.Contains("MaxRawValuesPerRead", m);
Assert.Contains("unbounded", m);
}
[Fact]
public void A_MaxRawValuesPerRead_of_one_is_raised_to_two_and_warns_when_enabled()
{
// One row per read cannot carry a page AND the lookahead row that says "more follows" — a fresh page would be refused.
var o = new ServerHistorianOptions
{
Enabled = true, Endpoint = "https://h:5222", ApiKey = "histgw_x_y", MaxRawValuesPerRead = 1,
};
Assert.Equal(2, o.EffectiveMaxRawValuesPerRead);
Assert.Equal(ServerHistorianOptions.MinMaxRawValuesPerRead, o.EffectiveMaxRawValuesPerRead);
var m = Assert.Single(o.Validate());
Assert.Contains("MaxRawValuesPerRead", m);
}
[Fact]
public void Disabled_with_an_odd_MaxRawValuesPerRead_is_silent()
{
Assert.Empty(new ServerHistorianOptions { Enabled = false, MaxRawValuesPerRead = 0 }.Validate());
Assert.Empty(new ServerHistorianOptions { Enabled = false, MaxRawValuesPerRead = 1 }.Validate());
}
}