test: validate grpc data connections with local docker and opcua cli
This commit is contained in:
@@ -155,7 +155,8 @@ public sealed class OpcUaClientService : IOpcUaClientService
|
||||
// A new epoch first: a failover still connecting abandons itself instead of resurrecting the connection.
|
||||
lock (_subscriptionLock) _connectionEpoch++;
|
||||
|
||||
var endpointUrl = _session?.EndpointUrl ?? _settings?.EndpointUrl ?? string.Empty;
|
||||
// The SDK transport may already be disposed when teardown begins.
|
||||
var endpointUrl = CurrentConnectionInfo?.EndpointUrl ?? _settings?.EndpointUrl ?? string.Empty;
|
||||
|
||||
// Snapshot the subscription adapter references under the lock so a concurrent
|
||||
// RunFailoverAsync that nulls _dataSubscription / _alarmSubscription
|
||||
@@ -210,6 +211,18 @@ public sealed class OpcUaClientService : IOpcUaClientService
|
||||
return await _session!.ReadValueAsync(nodeId, ct);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<DataValue> ReadValueAsync(NodeId nodeId, TimeSpan maximumAge, CancellationToken ct = default)
|
||||
{
|
||||
if (maximumAge < TimeSpan.Zero || maximumAge.TotalMilliseconds > int.MaxValue)
|
||||
throw new ArgumentOutOfRangeException(nameof(maximumAge));
|
||||
if (maximumAge == TimeSpan.Zero)
|
||||
return await ReadValueAsync(nodeId, ct);
|
||||
ThrowIfDisposed();
|
||||
ThrowIfNotConnected();
|
||||
return await _session!.ReadValueAsync(nodeId, maximumAge, ct);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<StatusCode> WriteValueAsync(NodeId nodeId, object value, CancellationToken ct = default)
|
||||
{
|
||||
@@ -391,10 +404,17 @@ public sealed class OpcUaClientService : IOpcUaClientService
|
||||
var monitorNode = sourceNodeId ?? ObjectIds.Server;
|
||||
var subscription = await session.CreateSubscriptionAsync(intervalMs, ct);
|
||||
lock (_subscriptionLock) _alarmSubscription = subscription;
|
||||
|
||||
var filter = CreateAlarmEventFilter();
|
||||
await subscription.AddEventMonitoredItemAsync(
|
||||
monitorNode, intervalMs, filter, AlarmCallback(session, subscription), ct);
|
||||
try
|
||||
{
|
||||
var filter = CreateAlarmEventFilter();
|
||||
await subscription.AddEventMonitoredItemAsync(
|
||||
monitorNode, intervalMs, filter, AlarmCallback(session, subscription), ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await CleanupFailedAlarmSubscriptionAsync(subscription);
|
||||
throw;
|
||||
}
|
||||
|
||||
lock (_subscriptionLock)
|
||||
{
|
||||
@@ -871,7 +891,7 @@ public sealed class OpcUaClientService : IOpcUaClientService
|
||||
return;
|
||||
|
||||
var oldSession = _session;
|
||||
var oldEndpoint = oldSession?.EndpointUrl ?? string.Empty;
|
||||
var oldEndpoint = CurrentConnectionInfo?.EndpointUrl ?? _settings?.EndpointUrl ?? string.Empty;
|
||||
if (!TransitionStateIfCurrent(epoch, ConnectionState.Reconnecting, oldEndpoint)) return;
|
||||
Logger.Warning("Session lost on {EndpointUrl}. Attempting failover...", oldEndpoint);
|
||||
|
||||
@@ -1012,10 +1032,11 @@ public sealed class OpcUaClientService : IOpcUaClientService
|
||||
if (alarmSubscription.HasValue)
|
||||
{
|
||||
var (sourceNodeId, intervalMs) = alarmSubscription.Value;
|
||||
ISubscriptionAdapter? replayedAlarms = null;
|
||||
try
|
||||
{
|
||||
var monitorNode = sourceNodeId ?? ObjectIds.Server;
|
||||
var replayedAlarms = await session.CreateSubscriptionAsync(intervalMs, CancellationToken.None);
|
||||
replayedAlarms = await session.CreateSubscriptionAsync(intervalMs, CancellationToken.None);
|
||||
lock (_subscriptionLock) _alarmSubscription = replayedAlarms;
|
||||
var filter = CreateAlarmEventFilter();
|
||||
await replayedAlarms.AddEventMonitoredItemAsync(
|
||||
@@ -1028,11 +1049,32 @@ public sealed class OpcUaClientService : IOpcUaClientService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (replayedAlarms is not null) await CleanupFailedAlarmSubscriptionAsync(replayedAlarms);
|
||||
Logger.Warning(ex, "Failed to replay alarm subscription");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CleanupFailedAlarmSubscriptionAsync(ISubscriptionAdapter subscription)
|
||||
{
|
||||
lock (_subscriptionLock)
|
||||
{
|
||||
if (ReferenceEquals(_alarmSubscription, subscription))
|
||||
{
|
||||
_alarmSubscription = null;
|
||||
_activeAlarmSubscription = null;
|
||||
}
|
||||
}
|
||||
using var cleanup = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
try { await subscription.DeleteAsync(cleanup.Token); }
|
||||
catch (Exception ex) { Logger.Warning(ex, "Failed to delete rejected alarm subscription"); }
|
||||
finally
|
||||
{
|
||||
try { subscription.Dispose(); }
|
||||
catch (Exception ex) { Logger.Warning(ex, "Failed to dispose rejected alarm subscription"); }
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsCurrentNotification(ISessionAdapter session, ISubscriptionAdapter subscription, bool alarm)
|
||||
{
|
||||
lock (_subscriptionLock)
|
||||
|
||||
Reference in New Issue
Block a user