feat(batch2): verify memstore remainder features

This commit is contained in:
Joseph Doherty
2026-02-28 07:17:01 -05:00
parent 1a0ac59df8
commit c2a73b49f7
6 changed files with 147 additions and 17 deletions

View File

@@ -103,6 +103,23 @@ public sealed class JetStreamMemStore : IStreamStore
_scheduling = new MsgScheduling(RunMsgScheduling);
}
/// <summary>
/// Factory that mirrors Go <c>newMemStore</c> mapping semantics.
/// </summary>
public static JetStreamMemStore NewMemStore(StreamConfig cfg)
{
var ms = new JetStreamMemStore(cfg);
if (cfg.FirstSeq > 0)
{
var (_, err) = ms.PurgeInternal(cfg.FirstSeq);
if (err != null)
throw err;
}
return ms;
}
// -----------------------------------------------------------------------
// IStreamStore — store / load
// -----------------------------------------------------------------------
@@ -1133,17 +1150,7 @@ public sealed class JetStreamMemStore : IStreamStore
_mu.EnterReadLock();
try
{
if (_msgs == null || _msgs.Count == 0) return (Array.Empty<ulong>(), null);
var seqs = new List<ulong>(_fss.Size());
_fss.IterFast((subj, ss) =>
{
if (ss.LastNeedsUpdate)
RecalculateForSubj(Encoding.UTF8.GetString(subj), ss);
seqs.Add(ss.Last);
return true;
});
seqs.Sort();
return (seqs.ToArray(), null);
return AllLastSeqsLocked();
}
finally
{
@@ -1151,6 +1158,27 @@ public sealed class JetStreamMemStore : IStreamStore
}
}
/// <summary>
/// Returns sorted per-subject last sequences without taking locks.
/// Mirrors Go <c>allLastSeqsLocked</c>.
/// </summary>
private (ulong[] Seqs, Exception? Error) AllLastSeqsLocked()
{
if (_msgs == null || _msgs.Count == 0) return (Array.Empty<ulong>(), null);
var seqs = new List<ulong>(_fss.Size());
_fss.IterFast((subj, ss) =>
{
if (ss.LastNeedsUpdate)
RecalculateForSubj(Encoding.UTF8.GetString(subj), ss);
seqs.Add(ss.Last);
return true;
});
seqs.Sort();
return (seqs.ToArray(), null);
}
/// <inheritdoc/>
public (ulong[] Seqs, Exception? Error) MultiLastSeqs(string[] filters, ulong maxSeq, int maxAllowed)
{

View File

@@ -32,7 +32,7 @@ public class JetStreamMemoryStoreTests
private static JetStreamMemStore NewMemStore(StreamConfig? cfg = null)
{
cfg ??= new StreamConfig { Storage = StorageType.MemoryStorage, Name = "test" };
return new JetStreamMemStore(cfg);
return JetStreamMemStore.NewMemStore(cfg);
}
private static byte[] Bytes(string s) => Encoding.UTF8.GetBytes(s);
@@ -41,6 +41,71 @@ public class JetStreamMemoryStoreTests
private static ulong MsgSize(string subj, byte[]? hdr, byte[]? msg)
=> (ulong)(subj.Length + (hdr?.Length ?? 0) + (msg?.Length ?? 0) + 16);
[Fact]
public void NewMemStore_FactoryMethod_InitializesFirstSequence()
{
var method = typeof(JetStreamMemStore).GetMethod(
"NewMemStore",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
method.ShouldNotBeNull();
var cfg = new StreamConfig
{
Name = "factory-init",
Storage = StorageType.MemoryStorage,
FirstSeq = 1000,
};
var created = method!.Invoke(null, new object?[] { cfg });
created.ShouldBeOfType<JetStreamMemStore>();
var ms = (JetStreamMemStore)created!;
var state = ms.State();
state.FirstSeq.ShouldBe(1000UL);
state.LastSeq.ShouldBe(999UL);
ms.Stop();
}
[Fact]
public void AllLastSeqsLocked_MatchesPublicAllLastSeqsOrdering()
{
var cfg = new StreamConfig
{
Name = "locked-all-last-seqs",
Subjects = new[] { "*.*" },
MaxMsgsPer = 50,
Storage = StorageType.MemoryStorage,
};
var ms = NewMemStore(cfg);
var subjs = new[] { "foo.foo", "foo.bar", "foo.baz", "bar.foo", "bar.bar", "bar.baz" };
var msg = Bytes("abc");
var rng = new Random(11);
for (var i = 0; i < 1_000; i++)
{
var subj = subjs[rng.Next(subjs.Length)];
ms.StoreMsg(subj, null, msg, 0);
}
var method = typeof(JetStreamMemStore).GetMethod(
"AllLastSeqsLocked",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
method.ShouldNotBeNull();
var result = method!.Invoke(ms, Array.Empty<object>());
result.ShouldNotBeNull();
var (lockedSeqs, lockedErr) = ((ValueTuple<ulong[], Exception?>)result!);
var (publicSeqs, publicErr) = ms.AllLastSeqs();
lockedErr.ShouldBeNull();
publicErr.ShouldBeNull();
lockedSeqs.SequenceEqual(publicSeqs).ShouldBeTrue();
ms.Stop();
}
// -----------------------------------------------------------------------
// TestMemStoreBasics (T:2023)
// -----------------------------------------------------------------------

View File

@@ -33,7 +33,7 @@ public class StorageEngineTests
private static JetStreamMemStore NewMemStore(StreamConfig cfg)
{
cfg.Storage = StorageType.MemoryStorage;
return new JetStreamMemStore(cfg);
return JetStreamMemStore.NewMemStore(cfg);
}
private static byte[] Bytes(string s) => Encoding.UTF8.GetBytes(s);

Binary file not shown.

View File

@@ -1,6 +1,6 @@
# NATS .NET Porting Status Report
Generated: 2026-02-28 12:11:43 UTC
Generated: 2026-02-28 12:17:02 UTC
## Modules (12 total)
@@ -12,10 +12,10 @@ Generated: 2026-02-28 12:11:43 UTC
| Status | Count |
|--------|-------|
| deferred | 2361 |
| deferred | 2359 |
| n_a | 24 |
| stub | 1 |
| verified | 1287 |
| verified | 1289 |
## Unit Tests (3257 total)
@@ -34,4 +34,4 @@ Generated: 2026-02-28 12:11:43 UTC
## Overall Progress
**2489/6942 items complete (35.9%)**
**2491/6942 items complete (35.9%)**

37
reports/report_1a0ac59.md Normal file
View File

@@ -0,0 +1,37 @@
# NATS .NET Porting Status Report
Generated: 2026-02-28 12:17:02 UTC
## Modules (12 total)
| Status | Count |
|--------|-------|
| verified | 12 |
## Features (3673 total)
| Status | Count |
|--------|-------|
| deferred | 2359 |
| n_a | 24 |
| stub | 1 |
| verified | 1289 |
## Unit Tests (3257 total)
| Status | Count |
|--------|-------|
| deferred | 2091 |
| n_a | 187 |
| verified | 979 |
## Library Mappings (36 total)
| Status | Count |
|--------|-------|
| mapped | 36 |
## Overall Progress
**2491/6942 items complete (35.9%)**