Skip to content

Commit

Permalink
add errors and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
james-rms committed Jan 7, 2025
1 parent 74965a7 commit 6ded7b5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ pub enum McapError {
ChunkBufferTooLarge(u64),
#[error("length exceeds usize max: `{0}`")]
TooLong(u64),
#[error("cannot write more than 65335 channels to one MCAP")]
#[error("cannot write more than 65536 channels to one MCAP")]
TooManyChannels,
#[error("cannot write more than 65334 schemas to one MCAP")]
#[error("cannot write more than 65535 schemas to one MCAP")]
TooManySchemas,
}

Expand Down
80 changes: 74 additions & 6 deletions rust/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,6 @@ impl<W: Write + Seek> Writer<W> {
},
schema.id,
);
if schema.id >= self.next_schema_id {
self.next_schema_id = schema.id + 1;
}
if self.options.use_chunks {
self.chunkin_time()?.write_schema(schema)
} else {
Expand Down Expand Up @@ -386,9 +383,6 @@ impl<W: Write + Seek> Writer<W> {
},
channel.id,
);
if channel.id >= self.next_channel_id {
self.next_channel_id = channel.id + 1;
}
if self.options.use_chunks {
self.chunkin_time()?.write_channel(channel)
} else {
Expand Down Expand Up @@ -1306,3 +1300,77 @@ impl<W: Write + Seek> AttachmentWriter<W> {
))
}
}

#[cfg(test)]
mod tests {
use std::u16;

use super::*;
#[test]
fn writes_all_channel_ids() {
let file = std::io::Cursor::new(Vec::new());
let mut writer = Writer::new(file).expect("failed to construct writer");
let custom_channel = std::sync::Arc::new(crate::Channel {
id: u16::MAX,
topic: "chat".into(),
message_encoding: "json".into(),
metadata: BTreeMap::new(),
schema: None,
});
writer
.write(&crate::Message {
channel: custom_channel.clone(),
sequence: 0,
log_time: 0,
publish_time: 0,
data: Cow::Owned(Vec::new()),
})
.expect("could not write initial channel");
for i in 0..65535u16 {
let id = writer
.add_channel(0, &format!("{i}"), "json", &BTreeMap::new())
.expect("could not add channel");
assert_eq!(i, id);
}
let Err(too_many) = writer.add_channel(0, "last", "json", &BTreeMap::new()) else {
panic!("should not be able to add another channel");
};
assert!(matches!(too_many, McapError::TooManyChannels));
}
#[test]
fn writes_all_schema_ids() {
let file = std::io::Cursor::new(Vec::new());
let mut writer = Writer::new(file).expect("failed to construct writer");
let custom_channel = std::sync::Arc::new(crate::Channel {
id: 0,
topic: "chat".into(),
message_encoding: "json".into(),
metadata: BTreeMap::new(),
schema: Some(std::sync::Arc::new(crate::Schema {
id: u16::MAX,
name: "int".into(),
encoding: "jsonschema".into(),
data: Cow::Owned(Vec::new()),
})),
});
writer
.write(&crate::Message {
channel: custom_channel.clone(),
sequence: 0,
log_time: 0,
publish_time: 0,
data: Cow::Owned(Vec::new()),
})
.expect("could not write initial channel");
for i in 0..65534u16 {
let id = writer
.add_schema(&format!("{i}"), "jsonschema", &[])
.expect("could not add schema");
assert_eq!(id, i + 1);
}
let Err(too_many) = writer.add_schema("last", "jsonschema", &[]) else {
panic!("should not be able to add another channel");
};
assert!(matches!(too_many, McapError::TooManySchemas));
}
}

0 comments on commit 6ded7b5

Please sign in to comment.