cultnet-rs: decode and drive the C#-written subscription session vector
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b34cb0293b
commit
678f058aae
3 changed files with 62 additions and 0 deletions
|
|
@ -318,3 +318,55 @@ fn changes_that_arrive_before_the_snapshot_follow_it_in_order() {
|
|||
let order: Vec<_> = events[1..].iter().map(keys).collect();
|
||||
assert_eq!(order, [vec!["first"], vec!["second-a", "second-b"], vec!["third"]]);
|
||||
}
|
||||
|
||||
/// The C#-written session: subscribe, snapshot, a single change, a two-change commit, a removal.
|
||||
fn session() -> Vec<Vec<u8>> {
|
||||
use serde::Deserialize;
|
||||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../../tests/vectors/database-subscription/session.msgpack");
|
||||
let bytes = std::fs::read(path).expect("the vector is committed");
|
||||
let mut cursor = std::io::Cursor::new(bytes.as_slice());
|
||||
let mut out = Vec::new();
|
||||
while (cursor.position() as usize) < bytes.len() {
|
||||
let start = cursor.position() as usize;
|
||||
let mut de = rmp_serde::Deserializer::new(&mut cursor);
|
||||
serde::de::IgnoredAny::deserialize(&mut de).expect("a msgpack value");
|
||||
out.push(bytes[start..cursor.position() as usize].to_vec());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_csharp_session_decodes_typed_and_its_server_messages_re_encode_byte_for_byte() {
|
||||
let messages = session();
|
||||
assert_eq!(messages.len(), 6);
|
||||
let decoded: Vec<_> = messages
|
||||
.iter()
|
||||
.map(|bytes| decode_cultnet_message_from_slice(bytes, CultNetWireContract::CultNetSchemaV0).unwrap())
|
||||
.collect();
|
||||
assert!(matches!(&decoded[0], CultNetMessage::DatabaseSubscribeV1 { subscription_id, .. } if subscription_id == "sub-1"));
|
||||
for (message, bytes) in decoded.iter().zip(&messages).skip(1) {
|
||||
assert_eq!(&encode_cultnet_message_to_vec(message, CultNetWireContract::CultNetSchemaV0).unwrap(), bytes);
|
||||
}
|
||||
let CultNetMessage::SnapshotResponseRawV1 { documents: Some(documents), .. } = &decoded[1] else { panic!("{:?}", decoded[1]) };
|
||||
assert!(documents.iter().all(|document| document.schema_content_hash.is_some() && document.schema_name.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_csharp_session_drives_the_assembler_to_a_snapshot_and_three_commits() {
|
||||
let messages = session();
|
||||
let decoded: Vec<_> = messages
|
||||
.iter()
|
||||
.map(|bytes| decode_cultnet_message_from_slice(bytes, CultNetWireContract::CultNetSchemaV0).unwrap())
|
||||
.collect();
|
||||
let mut assembler = SubscriptionAssembler::new();
|
||||
assembler.begin("sub-1", "subscribe-1").unwrap();
|
||||
let events: Vec<_> = decoded.into_iter().skip(1).flat_map(|message| assembler.accept(message)).collect();
|
||||
assert_eq!(events.len(), 4, "{events:?}");
|
||||
let SubscriptionEvent::Snapshot { documents, .. } = &events[0] else { panic!("{events:?}") };
|
||||
assert_eq!(documents.iter().map(|d| d.record_key.as_str()).collect::<Vec<_>>(), ["a", "b"]);
|
||||
assert_eq!(keys(&events[1]), ["c"]);
|
||||
assert_eq!(keys(&events[2]), ["a", "b"]);
|
||||
assert_eq!(keys(&events[3]), ["c"]);
|
||||
let SubscriptionEvent::Commit { changes, .. } = &events[3] else { unreachable!() };
|
||||
assert_eq!(changes[0].kind, DatabaseChangeKind::Removed);
|
||||
}
|
||||
|
|
|
|||
10
tests/vectors/database-subscription/README.md
Normal file
10
tests/vectors/database-subscription/README.md
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# database-subscription vectors
|
||||
|
||||
Written only by the C# reference, `ASubscriptionSessionIsWrittenAsTheSubscriptionVectorOtherRuntimesDecode` in
|
||||
`tests/GameCult.Networking.Tests/CultNetReplicaCommitTests.cs`, with `CULTCACHE_WRITE_VECTORS=1`. Every other run compares the bytes.
|
||||
|
||||
- `session.msgpack`: concatenated MessagePack messages of one subscription, in order: `cultnet.database_subscribe.v1`
|
||||
(`sub-1`, schema selection, document projection); `cultnet.snapshot_response_raw.v1` answering it with two records
|
||||
(`a`, `b`); one single `cultnet.database_change_raw.v0` (`updated`, `c`, no commit fields); a two-change commit
|
||||
(`updated` `a` and `b`, `commitId` `9f1c0f0e4b7a4c1f9d2e`, `commitCount` 2); one `removed` change (`c`). Message ids,
|
||||
the commit id and `storedAt` are fixed after the server wrote them.
|
||||
BIN
tests/vectors/database-subscription/session.msgpack
Normal file
BIN
tests/vectors/database-subscription/session.msgpack
Normal file
Binary file not shown.
Loading…
Reference in a new issue