Files
UA-.NETStandard/tests/Opc.Ua.Redundancy.Client.Tests/Raft/RaftSharedKeyValueStoreTests.cs
romanettandClaude Opus 5.5 9b034f8291 Enable nullable reference types centrally; migrate all unset and annotations-only projects (#4700)
# Description

Nullable reference types are now enabled once, in `common.props`, and
every project in the repository builds warning-clean with them.

**Configuration**
- `<Nullable>enable</Nullable>` lives in `common.props`. All per-project
`<Nullable>` settings are removed: 178 project files (enable,
annotations, and the `disable` in the three Aot test projects).
- 419 per-file `#nullable enable`/`restore` directives and the 4
`#nullable disable` directives are removed.
- Kept on purpose:
- The 5 `<auto-generated>` schema files (`UANodeSet.cs`, `UA Type
Dictionary.cs`, `OPCBinarySchema.cs`, `SecuredApplication.cs`,
`PInvokeHelper.cs`) keep `#nullable enable`. The compiler ignores the
project setting in generated code, so the directive is required there.
- `tests/Opc.Ua.Interop.LegacyPeer` keeps `disable`. It builds against
the 1.5.378 packages and does not import the repository props.
- `docs/DeveloperGuide.md` and `.github/copilot-instructions.md` now
describe the central setting.

**Migration** (the 43 unset and 13 annotations-only projects, roughly
10k diagnostics)
- **Production code: annotated at the declarations.** Covers
`tools/Opc.Ua.SourceGeneration.Core`, `src/Opc.Ua.Lds.Server` and the
LDS sample.
- Members, parameters and returns are nullable where null actually
flows.
- Null-check helpers carry `[NotNullWhen]` (`IsNull`, `IsValidXmlName`,
`IsMethodTypeDesign`).
- `CoreUtils.Format`, `Utils.Format`, `ITemplateWriter.WriteLine` take
`params object?[]`. `Template.AddReplacement` takes `string?` (rendering
already skips null replacements).
- `IRegisteredServerStore.RegisterAsync` returns
`Task<RegistrationEntry?>`: it returns null when a registration is
removed.
- Dereferences backed by an invariant (for example, the validated model)
use `!`, so runtime behaviour is unchanged.
- **Behavioural changes in production code** (small, deliberate):
- `NodeSetToModelDesign` throws `InvalidOperationException` when the
file is not a NodeSet. Before, it threw a NullReferenceException on the
next line.
- `ModelDesignValidator` starts its lookup tables empty instead of null.
A redundant `if (m_nodes == null) return;` guard is gone.
- **Test code: minimal annotations.**
  - `?` on values that can be null, `!` where the test asserts non-null.
  - `= null!` for fixture members assigned in SetUp.
- `TryGetStructure<T>(out T? x)` where the inferred `T?` broke the
`IEncodeable` constraint.
- A few redundant `== null` checks on structs (`NodeId`, `ArrayOf<T>`)
folded into the adjacent `IsNull`/`Count` checks.

**Notes for reviewers**
- The diff is large (~1,500 files) but almost entirely mechanical: `?`,
`!` and directive removal. The non-annotation changes are listed above.
- Known follow-up, not done here:
`IModelDesign.Namespaces/TargetNamespace/NamespaceUris` stay
non-nullable even though the validator returns null before it loads a
dictionary. Making them nullable would ripple through every generator
call site.

## Related Issues

- Fixes #4439

## Checklist

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [ ] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

The "no new warnings" box covers net10.0 and net48: full non-incremental
builds of `UA.slnx`, `tools/SourceGeneration.slnx`,
`tools/MigrationAnalyzer.slnx` and the Aot test projects finished with 0
warnings and 0 errors under the repository's warnings-as-errors
settings. net9.0 and net8.0 also had no nullable diagnostics, but they
were not rebuilt after the final fixes. Tests have not been run locally
yet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 08:48:13 +02:00

547 lines
22 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
// IDE0230: byte-array literals below are opaque binary test vectors, not text; a
// UTF-8 "..."u8 literal would misrepresent their intent, so keep the explicit byte arrays.
#pragma warning disable IDE0230 // Use UTF-8 string literal
// CA2000: system-under-test disposables are created per test and released at teardown;
// there is no cross-test resource leak. Suppressed file-level for the suite.
#pragma warning disable CA2000 // Dispose objects before losing scope
// CA2007: tests run without a SynchronizationContext; ConfigureAwait(false)
// adds noise without a behavioural benefit. Disabled file-level for the suite.
#pragma warning disable CA2007
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Redundancy;
namespace Opc.Ua.Client.Redundancy.Tests
{
/// <summary>
/// Coverage for <see cref="RaftSharedKeyValueStore"/>: the linearizable CP store built on
/// <see cref="IRaftConsensus"/> (set/get/CAS/delete/scan/watch, proposal bounding, disposal).
/// </summary>
[TestFixture]
[Category("ClientRedundancy")]
public sealed class RaftSharedKeyValueStoreTests
{
/// <summary>
/// Verifies that a value written through the Raft store can be retrieved.
/// </summary>
[Test]
public async Task SetAndTryGetReturnsStoredValueAsync()
{
await using var store = new RaftSharedKeyValueStore();
var payload = ByteString.From(new byte[] { 1, 2, 3 });
await store.SetAsync("k1", payload).ConfigureAwait(false);
(bool found, ByteString value) = await store.TryGetAsync("k1").ConfigureAwait(false);
Assert.That(found, Is.True);
Assert.That(value.ToArray(), Is.EqualTo(payload.ToArray()));
}
/// <summary>
/// Verifies that an empty stored byte string round-trips without becoming null.
/// </summary>
[Test]
public async Task SetEmptyValueRoundTripsAsNonNullAsync()
{
await using var store = new RaftSharedKeyValueStore();
await store.SetAsync("empty", ByteString.Empty).ConfigureAwait(false);
(bool found, ByteString value) = await store.TryGetAsync("empty").ConfigureAwait(false);
Assert.That(found, Is.True);
Assert.That(value.IsNull, Is.False, "an empty value is stored and read back as a non-null empty string");
Assert.That(value.ToArray(), Is.Empty);
}
/// <summary>
/// Verifies that reading a missing Raft store key returns false.
/// </summary>
[Test]
public async Task TryGetMissingKeyReturnsFalseAsync()
{
await using var store = new RaftSharedKeyValueStore();
(bool found, ByteString value) = await store.TryGetAsync("missing").ConfigureAwait(false);
Assert.That(found, Is.False);
Assert.That(value.IsNull, Is.True);
}
/// <summary>
/// Verifies that reading a null Raft store key is rejected.
/// </summary>
[Test]
public async Task TryGetNullKeyThrowsAsync()
{
await using var store = new RaftSharedKeyValueStore();
Assert.That(
async () => await store.TryGetAsync(null!).ConfigureAwait(false),
Throws.TypeOf<ArgumentNullException>());
}
/// <summary>
/// Verifies that writing a null Raft store key is rejected.
/// </summary>
[Test]
public async Task SetNullKeyThrowsAsync()
{
await using var store = new RaftSharedKeyValueStore();
Assert.That(
async () => await store.SetAsync(null!, ByteString.From(new byte[] { 1 })).ConfigureAwait(false),
Throws.TypeOf<ArgumentNullException>());
}
/// <summary>
/// Verifies that compare-and-swap creates a Raft store entry when the key is absent.
/// </summary>
[Test]
public async Task CompareAndSwapCreatesWhenAbsentAsync()
{
await using var store = new RaftSharedKeyValueStore();
var value = ByteString.From(new byte[] { 9 });
bool created = await store.CompareAndSwapAsync("k", default, value).ConfigureAwait(false);
bool createdAgain = await store.CompareAndSwapAsync("k", default, value).ConfigureAwait(false);
Assert.That(created, Is.True);
Assert.That(createdAgain, Is.False, "second create-if-absent must fail because the key now exists");
}
/// <summary>
/// Verifies that compare-and-swap replaces an entry when its current value matches.
/// </summary>
[Test]
public async Task CompareAndSwapSwapsWhenValueMatchesAsync()
{
await using var store = new RaftSharedKeyValueStore();
var first = ByteString.From(new byte[] { 1 });
var second = ByteString.From(new byte[] { 2 });
await store.SetAsync("k", first).ConfigureAwait(false);
bool swapped = await store.CompareAndSwapAsync("k", first, second).ConfigureAwait(false);
(bool found, ByteString value) = await store.TryGetAsync("k").ConfigureAwait(false);
Assert.That(swapped, Is.True);
Assert.That(found, Is.True);
Assert.That(value.ToArray(), Is.EqualTo(second.ToArray()));
}
/// <summary>
/// Verifies that compare-and-swap deletes a matching entry when the replacement is null.
/// </summary>
[Test]
public async Task CompareAndSwapDeletesWhenReplacementIsNullAsync()
{
await using var store = new RaftSharedKeyValueStore();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var value = ByteString.From(new byte[] { 1 });
await store.SetAsync("k", value, cts.Token).ConfigureAwait(false);
await using IAsyncEnumerator<KeyValueChange> enumerator =
store.WatchAsync("k", cts.Token).GetAsyncEnumerator(cts.Token);
ValueTask<bool> next = enumerator.MoveNextAsync();
bool deleted = await store.CompareAndSwapAsync(
"k",
value,
default,
cts.Token).ConfigureAwait(false);
(bool found, _) = await store.TryGetAsync("k")
.ConfigureAwait(false);
Assert.That(deleted, Is.True);
Assert.That(found, Is.False);
Assert.That(await next.ConfigureAwait(false), Is.True);
Assert.That(enumerator.Current.Kind, Is.EqualTo(KeyValueChangeKind.Delete));
Assert.That(enumerator.Current.Key, Is.EqualTo("k"));
}
/// <summary>
/// Verifies that compare-and-swap fails when the current value does not match.
/// </summary>
[Test]
public async Task CompareAndSwapFailsWhenValueMismatchAsync()
{
await using var store = new RaftSharedKeyValueStore();
var actual = ByteString.From(new byte[] { 1 });
var wrongExpected = ByteString.From(new byte[] { 7 });
var desired = ByteString.From(new byte[] { 2 });
await store.SetAsync("k", actual).ConfigureAwait(false);
bool swapped = await store.CompareAndSwapAsync("k", wrongExpected, desired).ConfigureAwait(false);
(bool found, ByteString value) = await store.TryGetAsync("k").ConfigureAwait(false);
Assert.That(swapped, Is.False);
Assert.That(found, Is.True);
Assert.That(value.ToArray(), Is.EqualTo(actual.ToArray()), "value must be unchanged on a failed CAS");
}
/// <summary>
/// Verifies that deletion removes a committed Raft store entry.
/// </summary>
[Test]
public async Task DeleteRemovesKeyAsync()
{
await using var store = new RaftSharedKeyValueStore();
await store.SetAsync("k", ByteString.From(new byte[] { 1 })).ConfigureAwait(false);
bool deleted = await store.DeleteAsync("k").ConfigureAwait(false);
bool deletedAgain = await store.DeleteAsync("k").ConfigureAwait(false);
(bool found, _) = await store.TryGetAsync("k").ConfigureAwait(false);
Assert.That(deleted, Is.True);
Assert.That(deletedAgain, Is.False);
Assert.That(found, Is.False);
}
/// <summary>
/// Verifies that a Raft store scan returns only keys matching the requested prefix.
/// </summary>
[Test]
public async Task ScanReturnsMatchingPrefixOnlyAsync()
{
await using var store = new RaftSharedKeyValueStore();
await store.SetAsync("a/1", ByteString.From(new byte[] { 1 })).ConfigureAwait(false);
await store.SetAsync("a/2", ByteString.From(new byte[] { 2 })).ConfigureAwait(false);
await store.SetAsync("b/1", ByteString.From(new byte[] { 3 })).ConfigureAwait(false);
var keys = new List<string>();
await foreach (KeyValuePair<string, ByteString> entry in store.ScanAsync("a/").ConfigureAwait(false))
{
keys.Add(entry.Key);
}
Assert.That(keys, Is.EquivalentTo(["a/1", "a/2"]));
}
/// <summary>
/// Verifies that a Raft store scan with a null prefix returns every entry.
/// </summary>
[Test]
public async Task ScanWithNullPrefixReturnsEverythingAsync()
{
await using var store = new RaftSharedKeyValueStore();
await store.SetAsync("x", ByteString.From(new byte[] { 1 })).ConfigureAwait(false);
await store.SetAsync("y", ByteString.From(new byte[] { 2 })).ConfigureAwait(false);
var keys = new List<string>();
await foreach (KeyValuePair<string, ByteString> entry in store.ScanAsync(null!).ConfigureAwait(false))
{
keys.Add(entry.Key);
}
Assert.That(keys, Is.EquivalentTo(["x", "y"]));
}
/// <summary>
/// Verifies that prefix watchers observe committed set and delete operations.
/// </summary>
[Test]
public async Task WatchObservesSetAndDeleteForPrefixAsync()
{
await using var store = new RaftSharedKeyValueStore();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
// Pre-start the store so the watcher registers synchronously on the
// first MoveNextAsync, before any mutation is proposed.
await store.TryGetAsync("warmup", cts.Token).ConfigureAwait(false);
await using IAsyncEnumerator<KeyValueChange> enumerator =
store.WatchAsync("a/", cts.Token).GetAsyncEnumerator(cts.Token);
ValueTask<bool> first = enumerator.MoveNextAsync();
await store.SetAsync("b/ignored", ByteString.From(new byte[] { 0 }), cts.Token).ConfigureAwait(false);
await store.SetAsync("a/1", ByteString.From(new byte[] { 1 }), cts.Token).ConfigureAwait(false);
Assert.That(await first.ConfigureAwait(false), Is.True);
Assert.That(enumerator.Current.Kind, Is.EqualTo(KeyValueChangeKind.Set));
Assert.That(enumerator.Current.Key, Is.EqualTo("a/1"));
ValueTask<bool> second = enumerator.MoveNextAsync();
await store.DeleteAsync("a/1", cts.Token).ConfigureAwait(false);
Assert.That(await second.ConfigureAwait(false), Is.True);
Assert.That(enumerator.Current.Kind, Is.EqualTo(KeyValueChangeKind.Delete));
Assert.That(enumerator.Current.Key, Is.EqualTo("a/1"));
}
/// <summary>
/// Verifies that two replicas converge on the same shared key-value state.
/// </summary>
[Test]
public async Task TwoReplicasConvergeOnSharedClusterAsync()
{
var cluster = new InProcessRaftCluster();
await using InProcessRaftConsensus consensus1 = cluster.CreateNode(1);
await using InProcessRaftConsensus consensus2 = cluster.CreateNode(2);
await using var store1 = new RaftSharedKeyValueStore(consensus1);
await using var store2 = new RaftSharedKeyValueStore(consensus2);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
// Start both replicas (registers each consensus node with the
// cluster) before writing, so the broadcast reaches both. The write
// on store1 completes locally (originator matches) while store2
// applies the same command with a non-matching originator.
await store1.TryGetAsync("warmup", cts.Token).ConfigureAwait(false);
await store2.TryGetAsync("warmup", cts.Token).ConfigureAwait(false);
var payload = ByteString.From(new byte[] { 42 });
await store1.SetAsync("shared", payload, cts.Token).ConfigureAwait(false);
ByteString observed = await WaitForValueAsync(store2, "shared", cts.Token).ConfigureAwait(false);
Assert.That(observed.ToArray(), Is.EqualTo(payload.ToArray()));
}
/// <summary>
/// Verifies that a proposal times out when consensus never commits it.
/// </summary>
[Test]
public void ProposalTimesOutWhenNoCommitOccurs()
{
// A proposal must never hang when there is no leader / quorum to
// commit it; the commit timeout fails it with a TimeoutException.
Assert.That(async () =>
{
await using var consensus = new NeverCommitsConsensus();
await using var store = new RaftSharedKeyValueStore(
consensus, ownsConsensus: false, commitTimeout: TimeSpan.FromMilliseconds(200));
await store.SetAsync("k", ByteString.From(new byte[] { 1 })).ConfigureAwait(false);
}, Throws.TypeOf<TimeoutException>());
}
/// <summary>
/// Verifies that caller cancellation aborts a pending proposal.
/// </summary>
[Test]
public async Task ProposalCanceledByCallerTokenThrowsAsync()
{
await using var consensus = new NeverCommitsConsensus();
await using var store = new RaftSharedKeyValueStore(
consensus, ownsConsensus: false, commitTimeout: Timeout.InfiniteTimeSpan);
using var cts = new CancellationTokenSource();
cts.Cancel();
Assert.That(
async () => await store.SetAsync("k", ByteString.From(new byte[] { 1 }), cts.Token)
.ConfigureAwait(false),
Throws.InstanceOf<OperationCanceledException>());
}
/// <summary>
/// Verifies that proposal failures from consensus propagate to the caller.
/// </summary>
[Test]
public async Task ProposePropagatesConsensusFailureAsync()
{
await using var consensus = new ThrowingProposeConsensus();
await using var store = new RaftSharedKeyValueStore(consensus, ownsConsensus: false);
Assert.That(
async () => await store.SetAsync("k", ByteString.From(new byte[] { 1 })).ConfigureAwait(false),
Throws.TypeOf<InvalidOperationException>());
}
/// <summary>
/// Verifies that disposing the store cancels pending proposals.
/// </summary>
[Test]
public async Task PendingProposalCanceledOnDisposeAsync()
{
var consensus = new NeverCommitsConsensus();
var store = new RaftSharedKeyValueStore(
consensus, ownsConsensus: true, commitTimeout: Timeout.InfiniteTimeSpan);
Task pending = store.SetAsync("k", ByteString.From(new byte[] { 1 })).AsTask();
await consensus.WaitForProposalAsync().ConfigureAwait(false);
await store.DisposeAsync().ConfigureAwait(false);
Assert.That(async () => await pending.ConfigureAwait(false),
Throws.InstanceOf<OperationCanceledException>());
}
/// <summary>
/// Verifies that the store rejects read barriers after disposal.
/// </summary>
[Test]
public async Task ReadBarrierRejectedAfterDisposeAsync()
{
var consensus = new NeverCommitsConsensus();
var store = new RaftSharedKeyValueStore(
consensus,
ownsConsensus: false,
commitTimeout: Timeout.InfiniteTimeSpan);
Task pending = store.TryGetAsync("k").AsTask();
await consensus.WaitForProposalAsync().ConfigureAwait(false);
await store.DisposeAsync().ConfigureAwait(false);
Assert.That(
async () => await pending.ConfigureAwait(false),
Throws.InstanceOf<OperationCanceledException>());
Assert.That(
async () => await store.TryGetAsync("k").ConfigureAwait(false),
Throws.TypeOf<ObjectDisposedException>());
}
/// <summary>
/// Verifies that repeated Raft store disposal is safe.
/// </summary>
[Test]
public async Task DisposeIsIdempotentAsync()
{
var store = new RaftSharedKeyValueStore();
await store.SetAsync("k", ByteString.From(new byte[] { 1 })).ConfigureAwait(false);
await store.DisposeAsync().ConfigureAwait(false);
Assert.That(async () => await store.DisposeAsync().ConfigureAwait(false), Throws.Nothing);
}
private static async Task<ByteString> WaitForValueAsync(
RaftSharedKeyValueStore store,
string key,
CancellationToken ct)
{
while (true)
{
(bool found, ByteString value) = await store.TryGetAsync(key, ct).ConfigureAwait(false);
if (found)
{
return value;
}
await Task.Delay(10, ct).ConfigureAwait(false);
}
}
/// <summary>
/// A consensus replica that accepts proposals but never commits them (it never yields on
/// <see cref="IRaftConsensus.Committed"/>), modelling a no-leader / lost-quorum window.
/// </summary>
private sealed class NeverCommitsConsensus : IRaftConsensus
{
public bool IsLeader => true;
public event Action<bool>? LeadershipChanged
{
add { }
remove { }
}
public ChannelReader<ReadOnlyMemory<byte>> Committed => m_committed.Reader;
public ValueTask StartAsync(CancellationToken ct = default)
{
return default;
}
public ValueTask ProposeAsync(ReadOnlyMemory<byte> command, CancellationToken ct = default)
{
m_proposed.TrySetResult(true);
return default;
}
public Task<bool> WaitForProposalAsync()
{
return m_proposed.Task;
}
public ValueTask CampaignAsync(CancellationToken ct = default)
{
return default;
}
public ValueTask DisposeAsync()
{
m_committed.Writer.TryComplete();
return default;
}
private readonly Channel<ReadOnlyMemory<byte>> m_committed =
Channel.CreateUnbounded<ReadOnlyMemory<byte>>();
private readonly TaskCompletionSource<bool> m_proposed =
new(TaskCreationOptions.RunContinuationsAsynchronously);
}
/// <summary>
/// A consensus replica whose <see cref="IRaftConsensus.ProposeAsync"/> always faults, so the store must
/// propagate the failure and drop the pending proposal.
/// </summary>
private sealed class ThrowingProposeConsensus : IRaftConsensus
{
public bool IsLeader => true;
public event Action<bool>? LeadershipChanged
{
add { }
remove { }
}
public ChannelReader<ReadOnlyMemory<byte>> Committed => m_committed.Reader;
public ValueTask StartAsync(CancellationToken ct = default)
{
return default;
}
public ValueTask ProposeAsync(ReadOnlyMemory<byte> command, CancellationToken ct = default)
{
throw new InvalidOperationException("propose failed");
}
public ValueTask CampaignAsync(CancellationToken ct = default)
{
return default;
}
public ValueTask DisposeAsync()
{
m_committed.Writer.TryComplete();
return default;
}
private readonly Channel<ReadOnlyMemory<byte>> m_committed =
Channel.CreateUnbounded<ReadOnlyMemory<byte>>();
}
}
}