CultNet C#: write the database-subscription session vector from a live subscription server

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 04:35:10 +01:00
commit b34cb0293b

View file

@ -65,6 +65,8 @@ namespace GameCult.Networking.Tests
{
if (message is CultNetDatabaseSubscribeMessage subscribe)
_server.Dispatch(subscribe, _peer).GetAwaiter().GetResult();
else if (message is CultNetDatabaseSubscribeV1Message subscribeV1)
_server.Dispatch(subscribeV1, _peer).GetAwaiter().GetResult();
else if (message is CultNetDatabaseUnsubscribeMessage unsubscribe)
_server.Dispatch(unsubscribe, _peer).GetAwaiter().GetResult();
}
@ -108,6 +110,8 @@ namespace GameCult.Networking.Tests
private readonly CultNetDatabaseSubscriptionServer _server;
public void Reconcile() => _server.Reconcile();
public CultNetDatabaseSubscriptionServer Server => _server;
private LoopbackServer Wire { get; }
public void SubscribeV1(CultNetDatabaseSubscribeV1Message message) => Wire.Dispatch(message, Peer).GetAwaiter().GetResult();
public Task Subscribe(string id) =>
Client.SubscribeAsync(id, schemaIds: new[] { Source.Registry.GetRequired<ReplicaItem>().SchemaId });
public object Gate => typeof(CultNetDatabaseSubscriptionServer)
@ -122,6 +126,7 @@ namespace GameCult.Networking.Tests
seedReplica?.Invoke(Replica);
Database = new CultNetDatabase(Source);
var wire = new LoopbackServer();
Wire = wire;
_server = new CultNetDatabaseSubscriptionServer(wire, Database, authorizeRecord: authorizeRecord, authorizeRequest: authorizeRequest);
var documents = new CultNetDocumentRegistry(Replica.Registry)
.Register(CultNetDocumentBinding.ForDocument<ReplicaItem>(Replica.Registry));
@ -262,6 +267,73 @@ namespace GameCult.Networking.Tests
Assert.That(Convert.ToHexString(bytes), Is.EqualTo(Convert.ToHexString(System.IO.File.ReadAllBytes(path))));
}
[Test]
public async Task ASubscriptionSessionIsWrittenAsTheSubscriptionVectorOtherRuntimesDecode()
{
// tests/vectors/database-subscription/session.msgpack: one v1 subscribe and what a live subscription server sends back
// (a snapshot of two records, one single change, one two-change commit, one removal), each as the C# serializer
// writes it, concatenated. Message ids, commit ids and timestamps are made deterministic after the server wrote
// them; everything else is the server's. Written with CULTCACHE_WRITE_VECTORS=1; every other run compares bytes.
using var run = new Replication(source => Stage(source, ("a", "one"), ("b", "two")), subscribe: false);
var subscribe = new CultNetDatabaseSubscribeV1Message
{
MessageId = "subscribe-1",
SubscriptionId = "sub-1",
Selection = new CultNetSelection
{
Schemas = new[] { run.Source.Registry.GetRequired<ReplicaItem>().SchemaId },
Projection = CultNetSelectionProjections.Document
}
};
run.SubscribeV1(subscribe);
Stage(run.Source, ("c", "three"));
await Until(() => run.Peer.Sent.Count >= 2);
Stage(run.Source, ("a", "two"), ("b", "one"));
await Until(() => run.Peer.Sent.Count >= 4);
Assert.That(run.Source.Commit(batch => batch.Remove(new CultRecordKey("c"))), Is.True);
await Until(() => run.Peer.Sent.Count >= 5);
const string storedAt = "2026-11-28T00:00:00.0000000Z";
var index = 0;
var stream = new List<byte[]> { CultNetSchemaMessageSerialization.Serialize(subscribe) };
foreach (var bytes in run.Peer.Sent)
{
switch (CultNetSchemaMessageSerialization.Deserialize(bytes))
{
case CultNetSnapshotResponseRawV1Message snapshot:
snapshot.MessageId = subscribe.MessageId;
foreach (var document in snapshot.Documents!) document.StoredAt = storedAt;
stream.Add(CultNetSchemaMessageSerialization.Serialize(snapshot));
break;
case CultNetDatabaseChangeRawMessage change:
change.MessageId = $"change-{index++}";
if (change.CommitId != null) change.CommitId = "9f1c0f0e4b7a4c1f9d2e";
if (change.Document != null) change.Document.StoredAt = storedAt;
stream.Add(CultNetSchemaMessageSerialization.Serialize(change));
break;
default:
Assert.Fail("The server sent a message the vector does not name.");
break;
}
}
var all = stream.SelectMany(bytes => bytes).ToArray();
var directory = TestContext.CurrentContext.TestDirectory;
while (directory != null && !System.IO.Directory.Exists(System.IO.Path.Combine(directory, "tests", "vectors")))
directory = System.IO.Path.GetDirectoryName(directory);
Assert.That(directory, Is.Not.Null, "tests/vectors is above the test directory");
var path = System.IO.Path.Combine(directory!, "tests", "vectors", "database-subscription", "session.msgpack");
if (Environment.GetEnvironmentVariable("CULTCACHE_WRITE_VECTORS") == "1")
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)!);
System.IO.File.WriteAllBytes(path, all);
return;
}
Assert.That(System.IO.File.Exists(path), Is.True, "the vector is committed");
Assert.That(Convert.ToHexString(all), Is.EqualTo(Convert.ToHexString(System.IO.File.ReadAllBytes(path))));
}
[Test]
public void AnOlderPeerReadsTheChangeAndIgnoresTheCommitFields()
{