using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.SqlServer.Tests;
///
/// The recorder's contract: a cancelled drain pass rethrows out of the writer —
/// swallowed as false it would be booked as a tag failure and could tip a healthy tag over its attempt budget.
/// Any other failure is still false. The connection points at a closed port, so nothing reaches a server.
///
public sealed class SqlServerValueWriterCancellationTests
{
private static SqlServerHistorianValueWriter Writer()
{
var opts = new SqlServerHistorianOptions { ConnectionString = "Server=127.0.0.1,1;Connect Timeout=1;TrustServerCertificate=True" };
var conn = new SqlHistorianConnection(opts);
var boot = new SqlServerSchemaBootstrapper(conn, opts);
return new SqlServerHistorianValueWriter(conn, boot, new SqlServerTagCatalog(conn, boot));
}
private static readonly HistorizationValue[] One = [new(DateTime.UtcNow, HistorianValue.FromDouble(1), 0)];
[Fact]
public async Task A_cancelled_pass_rethrows()
{
using var cts = new CancellationTokenSource();
await cts.CancelAsync();
await Should.ThrowAsync(() => Writer().WriteLiveValuesAsync("T", One, cts.Token));
}
[Fact]
public async Task An_unreachable_server_is_a_failed_write_not_a_throw()
{
(await Writer().WriteLiveValuesAsync("T", One, TestContext.Current.CancellationToken)).ShouldBeFalse();
}
}