Skip to content

Commit

Permalink
Add async variant for protocol_v2 invariant test.
Browse files Browse the repository at this point in the history
  • Loading branch information
TSnake41 committed Aug 18, 2023
1 parent ad90226 commit 109940c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions xcp-metrics-common/src/rrdd/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ fn metadata_invariance() {
assert_eq!(metadata, metadata_reparsed);
}

/// Check if protocol v2 header stays the same after being encoded then decoded.
/// NOTE: Timestamp precision is not kept.
#[test]
fn invariance() {
fn generate_test_rrdd() -> (RrddMetadata, RrddMessageHeader, Vec<u8>) {
let metadata = RrddMetadata {
datasources: indexmap! {
"A".into() => Default::default()
Expand All @@ -48,6 +45,14 @@ fn invariance() {
let mut buffer = vec![];
header.write(&mut buffer).unwrap();
buffer.write_all(metadata_str.as_bytes()).unwrap();
(metadata, header, buffer)
}

/// Check if protocol v2 header stays the same after being encoded then decoded.
/// NOTE: Timestamp precision is not kept.
#[test]
fn invariance() {
let (metadata, header, buffer) = generate_test_rrdd();

let mut reader = buffer.as_slice();
let header_readed = RrddMessageHeader::parse_from(&mut reader).unwrap();
Expand All @@ -60,3 +65,25 @@ fn invariance() {

assert_eq!(metadata, metadata_readed);
}

#[test]
fn invariance_async() {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(async {
let (metadata, header, buffer) = generate_test_rrdd();

let mut reader = buffer.as_slice();
let header_readed = RrddMessageHeader::parse_async(&mut reader).await.unwrap();
assert_eq!(header, header_readed);

let mut metadata_buffer = vec![0u8; header_readed.metadata_length as usize];
reader.read_exact(&mut metadata_buffer).unwrap();
let metadata_raw_readed: RrddMetadataRaw =
serde_json::from_slice(&metadata_buffer).unwrap();
let metadata_readed = metadata_raw_readed.try_into().unwrap();

assert_eq!(metadata, metadata_readed);
});
}

0 comments on commit 109940c

Please sign in to comment.